【发布时间】:2020-07-08 06:35:51
【问题描述】:
我有 Java 代码来使用 Apache POI 在 Word 文档中创建表格和一些文本,但它在最后一个文档中添加了表格。我想写一些文字,然后添加表格并再次写一些文字。
目前它添加表格第一个和最后一个文档添加2个文本(Hi&Bye)
我的代码:
public static void main(String[] args)throws Exception {
//Blank Document
XWPFDocument document= new XWPFDocument();
//Write the Document in file system
FileOutputStream out = new FileOutputStream(
new File("create_table.docx"));
//create table
XWPFTable table = document.createTable();
XWPFParagraph para = document.createParagraph();
XWPFRun run = para.createRun();
run.setText("Hi");
//create first row
XWPFTableRow tableRowOne = table.getRow(0);
tableRowOne.getCell(0).setText("col one, row one");
tableRowOne.addNewTableCell().setText("col two, row one");
tableRowOne.addNewTableCell().setText("col three, row one");
//create second row
XWPFTableRow tableRowTwo = table.createRow();
tableRowTwo.getCell(0).setText("col one, row two");
tableRowTwo.getCell(1).setText("col two, row two");
tableRowTwo.getCell(2).setText("col three, row two");
//create third row
XWPFTableRow tableRowThree = table.createRow();
tableRowThree.getCell(0).setText("col one, row three");
tableRowThree.getCell(1).setText("col two, row three");
tableRowThree.getCell(2).setText("col three, row three");
run.setText("Bye");
document.write(out);
out.close();
System.out.println("create_table.docx written successully");
}
如何打印Hi 的第一页并添加表格并在表格之后打印Bye?
每次我想添加内容并最终写入并打开它时,我如何保存文档?
谢谢
【问题讨论】:
标签: java swing report apache-poi