poi设置word表格单元格宽度_如何通过JAVA增加Word文件中表格的列宽

本文介绍如何通过JAVA使用Apache POI库创建Word文件并设置表格的列宽。由于POI版本3.15不直接支持列宽设置,因此需要使用底层的低级对象来实现。示例代码展示了如何创建一个两列的表格,并分别设置第一列和第二列的宽度为2英寸和3英寸。
摘要由CSDN通过智能技术生成

I working on a small project where I am creating word file by Java and enter some detail in this word file.

I am able to create word file and also able to enter data into it. I also write a table into word file and enter some details.

Now what I want, I want to increase width of specific column.

Is there any way to do this? I am using Apache POI drivers for creating word file and writing data into it.

I am using below code:

XWPFDocument document= new XWPFDocument();

try{

FileOutputStream out = new FileOutputStream(

new File("d:\\createparagraph.docx"));

XWPFParagraph paragraph = document.createParagraph();

XWPFTable table = document.createTable();

XWPFTableRow tableRowOne = table.getRow(0);

tableRowOne.getCell(0).setText("CLientID");

tableRowOne.addNewTableCell().setText(txtCID.getText());

//create second row

XWPFTableRow tableRow2 = table.createRow();

tableRow2.getCell(0).setText("AccountID");

tableRow2.getCell(1).setText(txtAID.getText());

document.write(out);

out.close();

}

This code working fine and generate normal table but I want to increase width of specific column (Column 2).

Please help.

解决方案

As far as I see, the column width settings are not implemented (as of POI version 3.15 final) in XWPFTable. So we must use the underlying low level objects.

Example:

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

import java.math.BigInteger;

public class CreateWordTableColumnWidth {

public static void main(String[] args) throws Exception {

XWPFDocument document= new XWPFDocument();

XWPFParagraph paragraph = document.createParagraph();

XWPFRun run=paragraph.createRun();

run.setText("The Body:");

paragraph = document.createParagraph();

XWPFTable table = document.createTable(1, 2);

//values are in unit twentieths of a point (1/1440 of an inch)

table.setWidth(5*1440); //should be 5 inches width

//create CTTblGrid for this table with widths of the 2 columns.

//necessary for Libreoffice/Openoffice to accept the column widths.

//first column = 2 inches width

table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(2*1440));

//other columns (only one in this case) = 3 inches width

for (int col = 1 ; col < 2; col++) {

table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(3*1440));

}

//set width for first column = 2 inches

CTTblWidth tblWidth = table.getRow(0).getCell(0).getCTTc().addNewTcPr().addNewTcW();

tblWidth.setW(BigInteger.valueOf(2*1440));

//STTblWidth.DXA is used to specify width in twentieths of a point.

tblWidth.setType(STTblWidth.DXA);

//set width for second column = 3 inches

tblWidth = table.getRow(0).getCell(1).getCTTc().addNewTcPr().addNewTcW();

tblWidth.setW(BigInteger.valueOf(3*1440));

tblWidth.setType(STTblWidth.DXA);

XWPFTableRow tableRowOne = table.getRow(0);

tableRowOne.getCell(0).setText("CLientID");

tableRowOne.getCell(1).setText("CID001");

//create second row

XWPFTableRow tableRow2 = table.createRow();

tableRow2.getCell(0).setText("AccountID");

tableRow2.getCell(1).setText("ACCID001");

paragraph = document.createParagraph();

document.write(new FileOutputStream("CreateWordTableColumnWidth.docx"));

document.close();

}

}

The code is commented to describe what it does. Especially mentioned should be the special measurement unit Twip (twentieth of a point).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值