apache poi在word文档中添加表
我有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
?
每当我想要添加内容并最终编写并打开它时,我怎样才能保存文档?
谢谢
您必须保持正确的顺序,更重要的是: 您必须创建一个新段落 。
这是代码,您需要:
public static void main(String[] args) throws IOException { //Blank Document XWPFDocument document = new XWPFDocument(); //Write the Document in file system FileOutputStream out = new FileOutputStream(new File("create_table.docx")); //Write first Text in the beginning XWPFParagraph para = document.createParagraph(); XWPFRun run = para.createRun(); run.setText("Hi"); //create table XWPFTable table = document.createTable(); //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"); //Write second Text after the table (by creating a new paragraph) XWPFParagraph para2 = document.createParagraph(); XWPFRun run2 = para2.createRun(); run2.setText("Bye"); document.write(out); out.close(); System.out.println("create_table.docx written successully"); }
这是输出,你会得到:
- POI 3.2图像高度/宽度控制
- 无法在Apache WorkbookFactory上找到close()方法
- 如何在一个.docx中将一些内容复制到另一个.docx,使用POI而不丢失格式?
- 从java POI创建excel文件时出错
- 如何使用HSSFSheet Apache POI在Excel中创建具有多个样式的单元格?
- 在Apache POI中打开EXISTING xls
- 编写XSSFWorkbook时,使用JDBC在Spring Boot应用程序中创建损坏的.xlsx文档
- 多个样式到Excel单元格POI
- java.lang.ClassNotFoundException:org.apache.xmlbeans.XmlException