我将Microsoft Word .docx文档上传到Sharepoint。在我的java代码中,我已将此文档下载到byte []中。好。现在,我想要的是处理这个byte []以获取XWPFDocument并能够将一些变量替换为文档。
拜托,有人可以帮帮我吗?
谢谢!
答案 0 :(得分:3)
您可以使用XWPFDocument的构造函数中指定的InputStream(ByteArrayInputStream)从byte []读取XWPFDocument,您可以从XWPFDocument获取段落和运行。 之后,您可以进行如下编辑。
byte[] byteData = ....
// read as XWPFDocument from byte[]
XWPFDocument doc = new XWPFDocument(new ByteArrayInputStream(byteData));
int numberToPrint = 0;
// you can edit paragraphs
for (XWPFParagraph para : doc.getParagraphs()) {
List<XWPFRun> runs = para.getRuns();
numberToPrint++;
for (XWPFRun run : runs) {
// read text
String text = run.getText(0);
// edit text and update it
run.setText(numberToPrint + " " + text, 0);
}
}
// save it and you can get the updated .docx
FileOutputStream fos = new FileOutputStream(new File("updated.docx"));
doc.write(fos);
答案 1 :(得分:0)
ByteArrayInputStream bis = new ByteArrayInputStream(bytebuffer);
POIXMLTextExtractor extractor = (POIXMLTextExtractor) ExtractorFactory.createExtractor(bis);
POIXMLDocument document = extractor.getDocument();
if (document instanceof XWPFDocument)
XWPFDocument xDocument = (XWPFDocument) document;