java - JAVA如何增加word文件中表格的列宽

标签 java apache-poi column-width

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

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

有什么办法吗?我正在使用 Apache POI 驱动程序来创建 word 文件并将数据写入其中。

我正在使用下面的代码:

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 列)的宽度。

请帮忙。

最佳答案

据我所知,XWPFTable 中未实现列宽设置(截至 POI 3.15 最终版)。所以我们必须使用底层的低级对象。

例子:

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 (二十分之一点)。

关于java - JAVA如何增加word文件中表格的列宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42251975/

相关文章:

java - 两个特定年份内的闰年数

java - iText 支持 OCR 吗?

java - 如何以编程方式取消 Java/Swing 中的拖放操作?

java - 如何从另一个文件或命令提示符将值传递给类

excel - 为什么 Range.ColumnWidth 的单位与点或厘米不匹配(使用默认单位)

java - Metastore db hive - 另一个实例已经在运行

java - 使用 apache POI 限制 Excel 不使用 Java 中的操作系统默认日期格式

java - 读取 Apache POI 文件并通过 JPA 进行持久化,但实体的持久化直到方法结束时才会发生

r - 如何将pdf文件中的数据转换成数据框

css - 无法重新定义 bootstrap col-md 宽度