java添加表,Java 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");

}

我该如何在页面的第一行打印嗨并添加表格并在表格之后打印再见?

当我想向其中添加内容并最终写入并打开它时,如何每次保存文档?

谢谢

解决方法:

您必须保持正确的顺序,更重要的是:您必须创建一个新段落.

这是代码,您需要:

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

}

这是输出,您将获得:

689653984375732c497f86c23b51e04c.png

标签:swing,apache-poi,report,java

来源: https://codeday.me/bug/20191027/1944352.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值