如何使用Apache poi将文本添加到word文件中的特定位置?

时间:2015-09-21 12:39:53

标签: java ms-word apache-poi

我有一个在线列表,我需要根据学生是否在场而改变。如果学生在场,我需要寻找学生姓名和推杆'确定',如果学生不在,我需要'/'。所以我想知道如何在doc文件中搜索文本以及如何将文本放在特定位置。

这是我使用的Word文件

Here is the Word file i use

我可以阅读docx文件,但我仍然不知道如何在表格中添加一些文字。

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 个答案:

答案 0 :(得分:1)

好的,我找到了我需要的东西。

 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);
    }
  }
}