poi导出word(可以使表格)简单样例

public void newWordDoc(String filename, String fileContent) throws Exception {
    XWPFDocument document = new XWPFDocument();
    XWPFParagraph tmpParagraph = document.createParagraph();
    XWPFRun tmpRun = tmpParagraph.createRun();
    tmpRun.setText(fileContent);    
    tmpRun.setFontSize(18);

    XWPFParagraph tmpParagraph1 = document.createParagraph();
    XWPFRun tmpRun1 = tmpParagraph1.createRun();
    tmpRun1.setText("At tutorialspoint.com, we strive hard to provide quality tutorials for self-learning purpose in the domains of Academics, Information Technology, Management and Computer");
    tmpRun1.setFontSize(20);

    FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\SUN\\Desktop\\" + filename + ".doc"));
    document.write(fos);
    fos.close();
}

导出成简单表格

public void exportTable() throws IOException {
    // Blank Document
    XWPFDocument document = new XWPFDocument();

    // Write the Document in file system
    FileOutputStream out = new FileOutputStream(new File("C:\\Users\\SUN\\Desktop\\create_table.docx"));

    // create table
    XWPFTable table = document.createTable();
    CTTblWidth width = table.getCTTbl().addNewTblPr().addNewTblW();
    width.setType(STTblWidth.DXA);
    width.setW(BigInteger.valueOf(5000));

    // 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");
}

这些例子的来源地址https://www.tutorialspoint.com/apache_poi_word/apache_poi_word_tables.htm

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值