apache poi 修改docx表格_Apache POI Word - 表格

本文档介绍了如何使用Apache POI库在Java中创建docx文档的表格。通过实例代码展示了如何创建表格,添加行和单元格,最终生成包含三行三列数据的Word文件。
摘要由CSDN通过智能技术生成

在本章中,您将学习如何在文档中创建数据表。 您可以使用XWPFTable类创建表数据。 将每个行添加到表格中,并将单元格添加到行,您将获得表格数据。

创建表

以下代码用于在文档中创建表: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");

}

}

将上述代码保存在名为CreateTable.java的文件中。从命令提示符处编译并执行它,如下所示:$javac CreateTable.java

$java CreateTable

它将在当前目录中生成名为createtable.docx的Word文件,并在命令提示符下显示以下输出:createtable.docx written successfully

createtable.docx文件如下所示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值