如何使用Apache POI在Java中设置Word文档中的表格尺寸和间距

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

How to set table dimensions and spacing in word using apache poi in java

问题

我正在尝试生成一个带有表格的Word文档。文档只有一页,包含5行2列。我使用的纸张类型是信纸,尺寸为8.5英寸 x 11英寸。我已经给程序设置了页边距。

以下是我的代码,

XWPFDocument xWPFDocument = new XWPFDocument();

CTSectPr cTSectPr = xWPFDocument.getDocument().getBody().addNewSectPr();
CTPageMar cTPageMar = cTSectPr.addNewPgMar();
cTPageMar.setLeft(BigInteger.valueOf(475));
cTPageMar.setTop(BigInteger.valueOf(720));
cTPageMar.setRight(BigInteger.valueOf(446));
cTPageMar.setBottom(BigInteger.valueOf(605));

XWPFTable xWPFTable = xWPFDocument.createTable(5, 2);
xWPFTable.getCTTbl().getTblPr().unsetTblBorders();

xWPFTable.setTableAlignment(TableRowAlign.CENTER);
xWPFTable.setWidth("100%");

我正在使用以下代码设置单元格宽度和行高。但我没有注意到任何变化。

XWPFTableRow xWPFTableRow;

for (int i = 0; i < 5; i++) {
    xWPFTableRow = xWPFTable.getRow(i);

    xWPFTableRow.setHeight(2880);
    xWPFTableRow.getCell(i).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(6033));
}

我想知道如何设置“水平和垂直间距”,另外是否有办法使用Apache POI设置“水平和垂直间距”?

英文:

I am trying to generate a word document with a table. There is only one page page and it has 5 rows and 2 columns. I am using the letter page and the size is 8.5" x 11". And I gave the margins to the program.

Here is my code,

    XWPFDocument xWPFDocument = new XWPFDocument();

    CTSectPr cTSectPr = xWPFDocument.getDocument().getBody().addNewSectPr();
    CTPageMar cTPageMar = cTSectPr.addNewPgMar();
    cTPageMar.setLeft(BigInteger.valueOf(475));
    cTPageMar.setTop(BigInteger.valueOf(720));
    cTPageMar.setRight(BigInteger.valueOf(446));
    cTPageMar.setBottom(BigInteger.valueOf(605));

    XWPFTable xWPFTable = xWPFDocument.createTable(5, 2);
    xWPFTable.getCTTbl().getTblPr().unsetTblBorders();

    xWPFTable.setTableAlignment(TableRowAlign.CENTER);
    xWPFTable.setWidth(&quot;100%&quot;);

And I am setting cell width and row height using the following code. But I am not noticing any change.

    XWPFTableRow xWPFTableRow;

    for (int i = 0; i &lt; 5; i++) {
        xWPFTableRow = xWPFTable.getRow(i);

        xWPFTableRow.setHeight(2880);
        xWPFTableRow.getCell(i).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(6033));
    }

What I am looking for is how to set Horizontal and Vertical Spacing also is there a way to set Horizontal and Vertical Pitch using Apache POI?

答案1

得分: 2

以下是您提供的内容的翻译部分:

目前的 apache poi 4.1.2 版本提供了一个方法 setWidth(java.lang.String widthValue),其中 widthValue 可以是一个 String,表示在 XWPFTableXWPFTableCell 中的百分比宽度。

直到现在为止,尚不直接支持设置单元格间距。因此,必须使用底层的 ooxml-schemas 类来实现这一点。

完整示例代码:

import java.io.FileOutputStream;

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

public class CreateWordTableCellSpacing {

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

        XWPFDocument document = new XWPFDocument();

        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();  
        run.setText("The table");

        int cols = 3;
        int rows = 3;
        XWPFTable table = document.createTable(rows, cols);

        table.setWidth("100%");
        table.getRow(0).getCell(0).setWidth("20%");
        table.getRow(0).getCell(1).setWidth("30%");
        table.getRow(0).getCell(2).setWidth("50%");

        // 设置单元格之间的间距
        table.getCTTbl()
            .getTblPr()
            .addNewTblCellSpacing()
            .setType(org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth.DXA);
        table.getCTTbl()
            .getTblPr()
            .getTblCellSpacing()
            .setW(java.math.BigInteger.valueOf(
                180 // 180 二十分之一英寸点(Twips)= 180/20 = 9 磅 = 9/72 = 0.125"
            ));

        paragraph = document.createParagraph();

        FileOutputStream out = new FileOutputStream("CreateWordTableCellSpacing.docx");
        document.write(out);
        out.close();
        document.close();

    }
}

(图片在这里)

英文:

The current apache poi 4.1.2 provides a method setWidth(java.lang.String widthValue) where widthValue can be a String giving a percentage width in XWPFTable as well as in XWPFTableCell.

Setting the cell spacing is not supported directly until now. So underlying ooxml-schemas classes must be used for this.

Complete example:

import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
public class CreateWordTableCellSpacing {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();  
run.setText(&quot;The table&quot;);
int cols = 3;
int rows = 3;
XWPFTable table = document.createTable(rows, cols);
table.setWidth(&quot;100%&quot;);
table.getRow(0).getCell(0).setWidth(&quot;20%&quot;);
table.getRow(0).getCell(1).setWidth(&quot;30%&quot;);
table.getRow(0).getCell(2).setWidth(&quot;50%&quot;);
//set spacing between cells
table.getCTTbl()
.getTblPr()
.addNewTblCellSpacing()
.setType(
org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth.DXA
);
table.getCTTbl()
.getTblPr()
.getTblCellSpacing()
.setW(java.math.BigInteger.valueOf(
180 // 180 TWentieths of an Inch Point (Twips) = 180/20 = 9 pt = 9/72 = 0.125&quot;
));
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream(&quot;CreateWordTableCellSpacing.docx&quot;);
document.write(out);
out.close();
document.close();
}
}

如何使用Apache POI在Java中设置Word文档中的表格尺寸和间距

huangapple
  • 本文由 发表于 2020年8月21日 13:24:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63516969.html
匿名

发表评论

匿名网友

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

确定