summaryrefslogtreecommitdiff
path: root/src/main/java/com/mavlushechka/quiz/App.java
blob: d83d8102c28a7119e317ea6147d1c7505daeac0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.mavlushechka.quiz;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;

public class App {
    public static void main(String[] args) throws Exception {
        FileInputStream fis = new FileInputStream("src/main/resources/test.docx");
        FileWriter fw = new FileWriter("src/main/resources/test.txt", false);
        XWPFDocument xdoc =  new XWPFDocument(OPCPackage.open(fis));
        Iterator<IBodyElement> bodyElementIterator = xdoc.getBodyElementsIterator();
            
        while (bodyElementIterator.hasNext()) {
            IBodyElement element = bodyElementIterator.next();

            if ("TABLE".equalsIgnoreCase(element.getElementType().name())) {
                List<XWPFTable> tableList = element.getBody().getTables();

                for (XWPFTable table : tableList) {
                    for (int i = 0; i < table.getRows().size(); i++) {
                        if (i > 0 && i < 5) {
                            fw.write((char) (64 + i) + ". ");
                        }
                        for (int j = 0; j < table.getRow(i).getTableCells().size(); j++) {
                            fw.write(table.getRow(i).getCell(j).getText());
                        }
                       fw.write(i == 0 ? "?\n" : "\n"); 
                    }
                    fw.write("ANSWER: A\n\n"); 
                }
            }
        } 
    }
}