1

I have a presence list that I need to change depending if the student is present or not. I need to look for student names and putt 'OK' if the student is present and ' / ' if the student is absent. So I wonder how to search for text in the doc file and how to put text in a particular position.

Here is the Word file i use

Here is the Word file i use

I can read the docx file, but i still don't know how to add some text to a table.

public class WordFile {
    public static void main(String[] args) {
        try {

            String FilePath = System.getProperty("user.home") + "\\Desktop\\Admin dokument\\EM.docx";
            FileInputStream fis = new FileInputStream(FilePath);
            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) {
                        System.out.println("Total Number of Rows of Table:" + table.getNumberOfRows());
                        System.out.println(table.getText());
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

1 Answer 1

1

Ok, i have found what i need.

 public class TableTest {

    public TableTest() throws IOException {
        String fileName = System.getProperty("user.home") + "\\Desktop\\Admin dokument\\EM.docx";
        InputStream fis = new FileInputStream(fileName);
        XWPFDocument document = new XWPFDocument(fis);
        List<XWPFParagraph> paragraphs = document.getParagraphs();

        for (int x=0; x<paragraphs.size();x++)
        {
            XWPFParagraph paragraph = paragraphs.get(x);
            System.out.println(paragraph.getParagraphText());
        }
        List<XWPFTable> tables = document.getTables();
        for (int x=0; x<tables.size();x++) {
            XWPFTable table = tables.get(x);
            List<XWPFTableRow> tableRows = table.getRows();
            tableRows.remove(x);
            for (int r=0; r<tableRows.size();r++) {
                System.out.println("Row " + (r+1) + ":");
                XWPFTableRow tableRow = tableRows.get(r);
                List<XWPFTableCell> tableCells = tableRow.getTableCells();
                for (int c=0; c<tableCells.size(); c++) {
                    System.out.print("Column "+ (c+1)+ ": ");
                    XWPFTableCell tableCell = tableCells.get(c);

                    String tableCellVal = tableCell.getText();

                    if(tableCellVal.equals("David Tomasson")) {
                        String s2 = "/";
                        tableCell = table.getRow(r).getCell(c);
                        System.out.print("Column "+ (c+1));
                        System.out.print("Row "+ (r+1));
                        tableCell = tableCells.get(c+2);// move one step to the right
                        if(!tableCellVal.isEmpty()) {
                            removeParagraphs(tableCell);
                        }
                        tableCell.setText(s2);
                        tableCell = tableCells.get(c+3);

                        if(!tableCellVal.isEmpty()) {
                            removeParagraphs(tableCell);//delete old values in the cell
                        }
                        tableCell.setText(s2);
                    }

                    if ((c+1)==4){
                        if (!(tableCellVal.equals("Hans Hansson"))) {
                        //if (tableCellVal.length()>0){
                        //char c1 = tableCellVal.charAt(0);
                        String s2 = "OK";
                        //char c2 = s2.charAt(0);
                        //String test = tableCell.getText().replace("O"," ");
                        if(!tableCellVal.isEmpty()) {
                            removeParagraphs(tableCell);
                        }
                        tableCell.setText(s2);
                        }
                        //else{
                        //tableCell.setText("NULL");
                        // }
                        //}
                    }
                    System.out.println("tableCell.getText(" + (c) + "):" + tableCellVal);
                }
            }
            System.out.println("\n");
        }
        OutputStream out = new FileOutputStream(fileName);
        document.write(out);
        out.close();
    }

    private static void removeParagraphs(XWPFTableCell tableCell) {
    int count = tableCell.getParagraphs().size();
    for(int i = 0; i < count; i++){
      tableCell.removeParagraph(i);
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.