java apache poi-xwpfparagraph转换为字符串

时间:2018-07-18 10:30:34

标签: java apache-poi xwpf

读取docx文件并尝试将其存储在字符串变量中 下面是我的代码

try {
            File file = new File(fileName);
            FileInputStream fileInputStream = new FileInputStream(file.getAbsolutePath());
            XWPFDocument document = new XWPFDocument(fileInputStream);
            List<XWPFParagraph> paragraphs = document.getParagraphs();
            for (XWPFParagraph para : paragraphs) {
                System.out.println(para.getText());
                docContent = para.getText();
            }
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

它成功读取了内容,但没有将整个内容存储在字符串变量(docContent)中,

1 个答案:

答案 0 :(得分:2)

您尚未串联字符串docContent += para.getText();,请使用+进行串联