我正在处理 Microsoft Word 2007 文档。
我的目标是填写:
- 表格单元格。
- 段落行。
所以,我的代码可以做到这一点,但问题是当我使用 FileOutputStream 编写文件时,它只写了我的一个目标(仅限最后一次修改)。
这是标题的图像:
这是我使用的代码:
try{
InputStream input = new FileInputStream("c:\\doslot.docx");
XWPFDocument document=new XWPFDocument(input);
//*********************inserting the 2nd line**************************
XWPFHeader head = document.getHeaderList().get(0);
List<XWPFParagraph> para= head.getParagraphs();
XWPFRun pararun=para.get(0).createRun();
pararun.setText("DOSSIER DE LOT GLUSCAN® N°FG-4040400A");
//*********************inserting the header thrid table cell*************************
XWPFHeader headd = document.getHeaderList().get(1);
List<XWPFTable> tables = headd.getTables();
List<XWPFTableRow> rows = tables.get(0).getRows();
XWPFTableCell cell = rows.get(0).getTableCell(rows.get(0).getTableCells().get(3).getCTTc());
XWPFParagraph p =cell.addParagraph();
XWPFRun pararuno=p.createRun();
pararuno.setText("some text");
FileOutputStream out = new FileOutputStream("c:\\fin.docx");
document.write(out);
out.close();
}catch(Exception ex){
ex.printStackTrace();
}