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

如何用JAVA增加word文件中表的列宽度,java,apache-poi,column-width,Java,Apache Poi,Column Width,我正在做一个小项目,我正在用Java创建word文件,并在这个word文件中输入一些细节。 我可以创建word文件,也可以将数据输入其中。我还将一个表写入word文件并输入一些详细信息 现在,我想增加特定列的宽度。 有没有办法做到这一点?我使用ApachePOI驱动程序创建word文件并将数据写入其中 我正在使用以下代码: XWPFDocument document= new XWPFDocument(); try{ FileOutputStream out = new FileOutputSt

我正在做一个小项目,我正在用Java创建word文件,并在这个word文件中输入一些细节。 我可以创建word文件,也可以将数据输入其中。我还将一个表写入word文件并输入一些详细信息

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

有没有办法做到这一点?我使用ApachePOI驱动程序创建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 final开始)。因此,我们必须使用底层的低级对象

例如:

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

 }
}
import java.io.FileOutputStream;
导入org.apache.poi.xwpf.usermodel.*;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
导入java.math.biginger;
公共类CreateWordTableColumnWidth{
公共静态void main(字符串[]args)引发异常{
XWPFDocument document=新的XWPFDocument();
XWPFParagraph paragraph paragraph=document.createParagraph();
XWPFRun=段落.createRun();
run.setText(“主体:”);
段落=document.create段落();
XWPFTable table=document.createTable(1,2);
//数值单位为点的二十分之一(1/1440英寸)
table.setWidth(5*1440);//应为5英寸宽
//为此表创建宽度为2列的CTTblGrid。
//Libreoffice/Openoffice需要接受列宽。
//第一列=2英寸宽
table.getCTTbl().addnewtbgrid().addNewGridCol().setW(biginger.valueOf(2*1440));
//其他列(本例中仅一列)=3英寸宽
for(int col=1;col<2;col++){
table.getCTTbl().getTblGrid().addNewGridCol().setW(biginger.valueOf(3*1440));
}
//设置第一列的宽度=2英寸
CTTblWidth-tblWidth=table.getRow(0).getCell(0).getCTTc().addNewTcPr().addNewTcW();
tblWidth.setW(BigInteger.valueOf(2*1440));
//STTblWidth.DXA用于指定点的二十分之一宽度。
设置类型(STTblWidth.DXA);
//设置第二列的宽度=3英寸
tblWidth=table.getRow(0).getCell(1).getCTTc().addNewTcPr().addNewTcW();
tblWidth.setW(BigInteger.valueOf(3*1440));
设置类型(STTblWidth.DXA);
XWPFTableRow tableRowOne=table.getRow(0);
tableRowOne.getCell(0.setText(“CLientID”);
tableRowOne.getCell(1.setText(“CID001”);
//创建第二行
XWPFTableRow tableRow2=table.createRow();
tableRow2.getCell(0.setText(“AccountID”);
tableRow2.getCell(1.setText(“ACCID001”);
段落=document.create段落();
write(新文件输出流(“CreateWordTableColumnWidth.docx”);
document.close();
}
}

代码被注释以描述它的功能。特别要提到的是特殊的测量单位(二十分之一点)。

了解Word中的表格宽度和单元格宽度有些奇怪。也就是说,它们只是建议的宽度。Word可以根据单元格内容的长度,在显示时对其进行必要的更改。@jmarkmurphy:你的观点是什么?上面使用我的代码设置的宽度正是我在代码中注释的宽度。我想我不清楚。可以为列设置宽度,但如果文本占用更多空间,Word可以使用更大的列呈现表格。您可以在POI使用第一版规范时阅读有关在规范中设置表格和单元格宽度的内容。第4部分对表格布局的工作方式进行了严格描述。它调用width
preferred width
,因为布局引擎将其用作决定单元格显示宽度的多个因素中的一个组成部分。无法复制此内容。获取我的代码并替换行
tableRowOne.getCell(0.setText(“CLientID”)带有
表格rowone.getCell(0).setText(“CLientID lorem ipsum semi t dolor lorem ipsum semi t dolor lorem ipsum semi t dolor lorem ipsum semi t dolor lorem ipsum semi t dolor lorem ipsum semi t dolor lorem ipsum semi t dolore”)。Word随后将增加行高,但不会增加列宽。当然,我的代码只为第1行设置列宽。您必须为所有行中的所有单元格设置它,以使其完全固定。您的答案没有错。但是请注意,您已经明确指定了表宽度和所有单元格宽度。行中的单元格宽度加起来等于表格宽度。表格网格的列宽与所有行中的单元格宽度相匹配。这使得表格看起来是固定的,即使它使用的是自动调整布局。人们只要小心就行了。您不能在其中一个单元格上选择不同的单元格宽度并使用它。人们已经尝试过了,结果却产生了疑问。