处理.docx文档时表中的换行问题

时间:2013-06-18 12:49:44

标签: java apache-poi docx

以下代码采用.docx文档的路径,遍历它并打印所有表中每个单元格的内容。

public void parse(String path) throws IOException {
    FileInputStream fis = new FileInputStream(path);
    XWPFDocument ex = new XWPFDocument(fis);
    XWPFWordExtractor extractor = new XWPFWordExtractor(ex);
    List<IBodyElement> docIter = ex.getBodyElements();
    Iterator<IBodyElement> iter = docIter.iterator();
    for (IBdyElement iBodyElement2 : docIter) {
        if (iBodyElement2 instanceof XWPFTable) {
            XWPFTable table = (XWPFTable) iBodyElement2;
                for (int i = 0; i < table.getNumberOfRows(); i++) {
                    XWPFTableRow row = table.getRow(i);
                        List<XWPFTableCell> rowcells = row.getTableCells();
                        for (XWPFTableCell xwpfTableCell : rowcells) {
                            System.out.print(xwpfTableCell.getText());
                    }
            }
        }
    }

当我在带有表格的.docx文档上运行此代码时,将从没有换行符的单元格中打印字符串。 例如,如果一个单元格填充了字符串“Foo
Bar”,它将被打印为“FooBar”。这对我来说是个大问题。

有没有办法读取保留换行符的单元格?

0 个答案:

没有答案
相关问题