Java Apache POI 在Word 中生成收据表

问题描述 投票:0回答:1

我需要制作一个采用基本表单条目的java应用程序,以标题(收据编号)、子标题(名称、地址等)的形式生成包含这些条目的收据,并在Word文件中生成包含所述条目的表格。

现在我已经弄清楚了标头部分,但我似乎找不到任何有关如何操作表的 Apache POI 文档。知道如何创建 n 行 m 列吗?

java ms-word apache-poi
1个回答
0
投票

参见这里

import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

public class CreateTable {

   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();
        
      //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");
    
      document.write(out);
      out.close();
      System.out.println("create_table.docx written successully");
   }
}

创造了

© www.soinside.com 2019 - 2024. All rights reserved.