【问题标题】:How to increase width of column of table in word file by JAVA如何通过JAVA增加word文件中表格列的宽度
【发布时间】:2017-07-04 06:24:40
【问题描述】:

我正在做一个小项目,我正在通过 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 列)的宽度。

请帮忙。

【问题讨论】:

    标签: java apache-poi column-width


    【解决方案1】:

    据我所知,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(点的二十分之一)。

    【讨论】:

    • 了解Word中的表格宽度和单元格宽度有些奇怪。也就是说,它们只是建议的宽度。 Word 可以根据单元格内容的长度在必要时更改它们的显示。
    • @jmarkmurphy:你的意思是什么?使用我上面的代码设置的宽度正是我在代码中注释的宽度。
    • 我想我不清楚。您可以为列设置宽度,但如果文本占用更多空间,Word 可以呈现具有更大列的表格。您可以在ecma-international.org/publications/standards/Ecma-376.htm POI 使用第一版规范中阅读有关设置表格和单元格宽度的信息。第 4 部分对表格布局的工作方式进行了严格的描述。它调用宽度preferred width,因为布局引擎使用它作为多个因素的一个组成部分来决定单元格的显示宽度。
    • 无法重现。获取我的代码并将tableRowOne.getCell(0).setText("CLientID"); 行替换为tableRowOne.getCell(0).setText("CLientID lorem ipsum semit dolor lorem ipsum semit dolor lorem ipsum semit dolor lorem ipsum semit dolor lorem ipsum semit dolor lorem ipsum semit dolor");。然后 Word 将增加行高,但不会增加列宽。当然,我的代码只为第 1 行设置了列宽。您必须为所有行中的所有单元格设置它以使其完全修复。
    • 你的答案没有错。但请注意,您已明确指定表格宽度和所有单元格宽度。行中的单元格宽度加起来就是表格宽度。并且表格网格定义为与所有行中的单元格宽度匹配的列宽。这使得表格看起来是固定的,即使它使用的是自动调整布局。人们只需要小心。您不能只在其中一个单元格上选择不同的单元格宽度并使用它。人们已经尝试过,但结果是问题。
    猜你喜欢
    • 2011-01-17
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 2015-10-22
    • 2019-04-28
    相关资源
    最近更新 更多