【问题标题】:How to set table dimensions and spacing in word using apache poi in java如何在java中使用apache poi在word中设置表格尺寸和间距
【发布时间】:2020-08-28 01:14:52
【问题描述】:

我正在尝试生成带有表格的 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));
    }

我正在寻找的是如何设置Horizontal and Vertical Spacing 也有没有办法使用Apache POI 设置Horizontal and Vertical Pitch

【问题讨论】:

    标签: java ms-word apache-poi


    【解决方案1】:

    当前的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%");
    
      //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"
       ));
    
      paragraph = document.createParagraph();
    
      FileOutputStream out = new FileOutputStream("CreateWordTableCellSpacing.docx");
      document.write(out);
      out.close();
      document.close();
    
     }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多