如何使用Apache POI和Java来修复位于另一个表格单元格中的表格列的宽度?

huangapple go评论56阅读模式
英文:

How to fix the width of the columns in a table that is within the cell of another table with apache poi and Java?

问题

我正在解析一个包含HTML标记的XML。解析XML后,我尝试在另一个表格的单元格内创建一个表格,并使用这些内容创建一个Word文档。我根据这个问题的答案实现了表格中的嵌套表格。但是,我无法设置嵌套表格列的宽度。生成的Word文档中,生成的嵌套表格的列非常窄,必须手动调整以获得所需的列宽。我想设置一些默认值作为所需的列宽。生成的带有嵌套表格的Word文档已在图片中提供。我使用以下代码来设置列宽,但到目前为止没有成功。

for (int i = 0; i < nestedtable.getNumberOfRows(); i++) {
  XWPFTableRow row = nestedtable.getRow(i);
  int numCells = row.getTableCells().size();
  for (int j = 0; j < numCells; j++) {
    XWPFTableCell cell = row.getCell(j);
    CTTblWidth cellWidth = cell.getCTTc().addNewTcPr().addNewTcW();
    CTTcPr pr = cell.getCTTc().addNewTcPr();
    pr.addNewNoWrap();
    cellWidth.setW(BigInteger.valueOf(400));
  }
}

我该如何解决这个问题?由于这个嵌套表格结构,生成的Word文档看起来非常不美观。

英文:

I am parsing a XML that has HTML tags in it. After parsing the XML I am trying to create a table that is within the cell of another table and create a word document with these contents. I implemented the table within a table with the help of the answer of this Question. However, I am unable to set the width of the columns of the nested table. When the word document is generated the generated nested table is so ugly with super narrow columns that has to be manually adjusted to have a desired column width. I would like to set some default values as the desired column width. The generated word document with the nested table has been provided in the picture. I am using the following code to set the column width but no luck so far. 如何使用Apache POI和Java来修复位于另一个表格单元格中的表格列的宽度?

  for (int i = 0; i &lt; nestedtable.getNumberOfRows(); i++) {
  XWPFTableRow row = nestedtable.getRow(i);
  int numCells = row.getTableCells().size();
  for (int j = 0; j &lt; numCells; j++) {
    XWPFTableCell cell = row.getCell(j);
    CTTblWidth cellWidth = cell.getCTTc().addNewTcPr().addNewTcW();
    CTTcPr pr = cell.getCTTc().addNewTcPr();
    pr.addNewNoWrap();
    cellWidth.setW(BigInteger.valueOf(400));
  }
}

How should I get out of this problem? The generated word document is looking so ugly due to this nested table structure.

答案1

得分: 3

你链接的问题是关于如何使用Apache POI在Word表格中插入表格单元格,而不是设置列宽度。并且,由于这是2017年的问题,它使用的是2017年的Apache POI版本。但是Apache POI在不断发展中,所以当前版本会提供更直接的方法来执行操作。

尤其是在设置XWPFTable和/或XWPFTableCell的宽度方面,当前的Apache POI版本5.2.3提供了XWPFTableXWPFTableCell中的setWidth方法。最直接的是public void setWidth(java.lang.String widthValue)。在这里,String widthValue可以是值为"auto"、整数值(以点的20分之一为单位)或百分比("nn.nn%")。

此外,现在设置边框也更加直接,因为现在有XWPFTable.set*Border方法。

所以,使用当前的Apache POI,代码应该如下所示:

import java.io.*;
import org.apache.poi.xwpf.usermodel.*;

public class CreateWordTableInTable {

    static void setAllBorders(XWPFTable table, XWPFTable.XWPFBorderType borderType, int size, int space, String rgbColor) {
        table.setTopBorder(borderType, size, space, rgbColor);
        table.setRightBorder(borderType, size, space, rgbColor);
        table.setBottomBorder(borderType, size, space, rgbColor);
        table.setLeftBorder(borderType, size, space, rgbColor);
        table.setInsideHBorder(borderType, size, space, rgbColor);
        table.setInsideVBorder(borderType, size, space, rgbColor);
    }

    public static void main(String[] args) throws Exception {

        XWPFDocument document = new XWPFDocument();

        XWPFTable tableOne = document.createTable(2, 2);

        tableOne.setWidth("100%");

        XWPFTableRow tablerow = tableOne.getRow(0);

        tablerow.getCell(0).setWidth("40%");
        tablerow.getCell(1).setWidth("60%");

        tablerow.getCell(0).setText("Test");
        tablerow.getCell(1).setText("Test");

        tablerow = tableOne.getRow(1);
        tablerow.getCell(0).setText("Test");

        XWPFParagraph paragraph = tablerow.getCell(1).getParagraphArray(0);
        XWPFTable tableTwo = tablerow.getCell(1).insertNewTbl(paragraph.getCTP().newCursor());

        tableTwo.setWidth(0); // 这是必需的,因为通过insertNewTbl创建的XWPFTable似乎没有完整的内部结构。在这种情况下,它缺少单元格宽度字段。
        tableTwo.setWidth("100%");

        setAllBorders(tableTwo, XWPFTable.XWPFBorderType.SINGLE, 4, 0, "000000");

        tablerow = tableTwo.createRow();
        tablerow.createCell().setText("aaaaaaaaaa");
        tablerow.createCell().setText("jjjjjjjj");
        tablerow = tableTwo.createRow();
        tablerow.getCell(0).setText("bbbbbbbbbb");
        tablerow.getCell(1).setText("gggggggggg");

        try (FileOutputStream out = new FileOutputStream("./CreateWordTableInTable.docx")) {
            document.write(out);
        }
        document.close();
    }
}

注意:通过insertNewTbl创建的XWPFTable似乎没有完整的内部结构。例如,它没有默认的边框设置,缺少内部的单元格宽度字段。这就是为什么需要明确设置边框。在调用setWidth(java.lang.String widthValue)之前,需要先调用setWidth(int width),因为第一个方法设置了内部的单元格宽度字段,而第二个方法依赖于该字段的存在,如果不存在,则会失败。

英文:

The question you linked was about how to insert a table into a table cell in a Word-table using Apache POI. Nothing about how to set column widths. And, since it is from 2017, it answers this using Apache POI versions of 2017. But Apache POI is highly in development. So current versions will have more straightforward methods to do things.

Especially to set widths of XWPFTable and/or XWPFTableCell, current Apache POI version 5.2.3 provides setWidth methods in XWPFTable as well as in XWPFTableCell. The most straightforward is public void setWidth(java.lang.String widthValue). There the String widthValue may be the width to the value "auto", an integer value (20ths of a point), or a percentage ("nn.nn%").

Also setting borders is much more straightforward now as there are XWPFTable.set*Border methods now.

So the code should look like so now using current Apache POI.

import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
public class CreateWordTableInTable {
static void setAllBorders(XWPFTable table, XWPFTable.XWPFBorderType borderType, int size, int space, java.lang.String rgbColor) {
table.setTopBorder(borderType, size, space, rgbColor);
table.setRightBorder(borderType, size, space, rgbColor);
table.setBottomBorder(borderType, size, space, rgbColor);
table.setLeftBorder(borderType, size, space, rgbColor);
table.setInsideHBorder(borderType, size, space, rgbColor);
table.setInsideVBorder(borderType, size, space, rgbColor);	 
}
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFTable tableOne = document.createTable(2,2);
tableOne.setWidth(&quot;100%&quot;);
XWPFTableRow tablerow = tableOne.getRow(0);
tablerow.getCell(0).setWidth(&quot;40%&quot;);
tablerow.getCell(1).setWidth(&quot;60%&quot;);
tablerow.getCell(0).setText(&quot;Test&quot;);
tablerow.getCell(1).setText(&quot;Test&quot;);
tablerow = tableOne.getRow(1);
tablerow.getCell(0).setText(&quot;Test&quot;);
XWPFParagraph paragraph = tablerow.getCell(1).getParagraphArray(0);
XWPFTable tableTwo = tablerow.getCell(1).insertNewTbl(paragraph.getCTP().newCursor());
tableTwo.setWidth(0); // This is necessary because a XWPFTable created by insertNewTbl seems not to have full internally structure. It lacks the cell width field in this case.
tableTwo.setWidth(&quot;100%&quot;);
setAllBorders(tableTwo, XWPFTable.XWPFBorderType.SINGLE, 4, 0, &quot;000000&quot;);
tablerow = tableTwo.createRow();
tablerow.createCell().setText(&quot;aaaaaaaaaa&quot;);
tablerow.createCell().setText(&quot;jjjjjjjj&quot;); 
tablerow = tableTwo.createRow(); 
tablerow.getCell(0).setText(&quot;bbbbbbbbbb&quot;); 
tablerow.getCell(1).setText(&quot;gggggggggg&quot;);
try (FileOutputStream out = new FileOutputStream(&quot;./CreateWordTableInTable.docx&quot;)) {
document.write(out);
}
document.close();
}
}

Note: A XWPFTable created by insertNewTbl seems not to have full internally structure. It has no default border setting for example and it lacks the internally cell width field. That's why explicit border setting is necessary. And calling setWidth(int width) is necessary before calling setWidth(java.lang.String widthValue) as the first sets the internally cell width field while the second relies on the presence of that field and fails if it is not present.

Result looks like so for me:

如何使用Apache POI和Java来修复位于另一个表格单元格中的表格列的宽度?

huangapple
  • 本文由 发表于 2023年2月14日 22:05:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定