Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. Importer:
  2.  
  3.  
  4. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  5. import org.apache.poi.openxml4j.opc.OPCPackage;
  6. import org.apache.poi.util.SystemOutLogger;
  7. import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
  8. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  9. import org.apache.poi.xwpf.usermodel.XWPFTable;
  10. import org.apache.poi.xwpf.usermodel.XWPFTableCell;
  11. import org.apache.poi.xwpf.usermodel.XWPFTableRow;
  12.  
  13. import java.io.*;
  14. import java.util.List;
  15.  
  16. public class Importer {
  17. public Importer(){
  18.  
  19. XWPFDocument worddoc = readFile("C:\\Users\\user\\IdeaProjects\\modulhandbuch_import\\src\\main\\resources\\MHB_Ma_Allgemeine Informatik_TM_240719_sw_eg.docx");
  20. List<XWPFTable> tablelist = worddoc.getTables();
  21. System.out.println("amt tables: " + tablelist.size() );
  22. for(XWPFTable table : tablelist){
  23. int rowSize = table.getNumberOfRows();
  24. for(int i=0;i<rowSize;i++){
  25. XWPFTableRow row = table.getRow(i);
  26. List<XWPFTableCell> cells = row.getTableCells();
  27. for(int j=0;j<cells.size();j++){
  28. cells.get(j).
  29. System.out.println(cells.get(j).getText());
  30. }
  31. }
  32. }
  33. }
  34.  
  35. public XWPFDocument readFile(String filename) {
  36. System.out.println("Reading file...");
  37. try {
  38. if (filename.endsWith(".doc") || filename.endsWith(".docx")) {
  39. FileInputStream fis = new FileInputStream(filename);
  40. XWPFDocument document = new XWPFDocument(OPCPackage.open(fis));
  41. XWPFWordExtractor extractor = new XWPFWordExtractor(document);
  42. System.out.println("File read.");
  43. return document;
  44. } else {
  45. System.out.println("Couldn't import file: " + filename);
  46. }
  47. } catch (FileNotFoundException e) {
  48. e.printStackTrace();
  49. } catch (InvalidFormatException e) {
  50. e.printStackTrace();
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. return null;
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement