如何用JAVA增加word文件表格列的宽度

var*_*ngi 1 java column-width apache-poi

我正在开发一个小项目,我正在通过 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();
 }
Run Code Online (Sandbox Code Playgroud)

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

请帮忙。

Axe*_*ter 5

据我所知,列宽设置在XWPFTable. 所以我们必须使用底层的低级对象。

例子:

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

 }
}
Run Code Online (Sandbox Code Playgroud)

代码被注释以描述它的作用。特别值得一提的是特殊的测量单位Twip(二十分之一)。