如何通过JAVA增加word文件中表格的列宽 [英] How to increase width of column of table in word file by JAVA

查看:36
本文介绍了如何通过JAVA增加word文件中表格的列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个小项目中工作,我正在用 Java 创建 word 文件并在这个 word 文件中输入一些细节.我能够创建word文件,也能够向其中输入数据.我还写了一个表格到word文件中并输入了一些细节.

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.

现在我想要的是增加特定列的宽度.

有没有办法做到这一点?我正在使用 Apache POI 驱动程序来创建 word 文件并将数据写入其中.

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

我正在使用以下代码:

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();
 }

此代码工作正常并生成普通表格,但我想增加特定列(第 2 列)的宽度.

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

请帮忙.

推荐答案

据我所知,XWPFTable 中没有实现列宽设置(从 POI 版本 3.15 final 开始).所以我们必须使用底层的低级对象.

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.

示例:

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

 }
}

代码被注释以描述它的作用.特别提到的应该是特殊的测量单位Twip(二十分之一点).

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

这篇关于如何通过JAVA增加word文件中表格的列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆