Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Importer:
- import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
- import org.apache.poi.openxml4j.opc.OPCPackage;
- import org.apache.poi.util.SystemOutLogger;
- import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
- import org.apache.poi.xwpf.usermodel.XWPFDocument;
- import org.apache.poi.xwpf.usermodel.XWPFTable;
- import org.apache.poi.xwpf.usermodel.XWPFTableCell;
- import org.apache.poi.xwpf.usermodel.XWPFTableRow;
- import java.io.*;
- import java.util.List;
- public class Importer {
- public Importer(){
- XWPFDocument worddoc = readFile("C:\\Users\\user\\IdeaProjects\\modulhandbuch_import\\src\\main\\resources\\MHB_Ma_Allgemeine Informatik_TM_240719_sw_eg.docx");
- List<XWPFTable> tablelist = worddoc.getTables();
- System.out.println("amt tables: " + tablelist.size() );
- for(XWPFTable table : tablelist){
- int rowSize = table.getNumberOfRows();
- for(int i=0;i<rowSize;i++){
- XWPFTableRow row = table.getRow(i);
- List<XWPFTableCell> cells = row.getTableCells();
- for(int j=0;j<cells.size();j++){
- cells.get(j).
- System.out.println(cells.get(j).getText());
- }
- }
- }
- }
- public XWPFDocument readFile(String filename) {
- System.out.println("Reading file...");
- try {
- if (filename.endsWith(".doc") || filename.endsWith(".docx")) {
- FileInputStream fis = new FileInputStream(filename);
- XWPFDocument document = new XWPFDocument(OPCPackage.open(fis));
- XWPFWordExtractor extractor = new XWPFWordExtractor(document);
- System.out.println("File read.");
- return document;
- } else {
- System.out.println("Couldn't import file: " + filename);
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (InvalidFormatException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement