Apache POI 5 生成具有 30 多个列的 Word table

Apache POI 5 generate Word table having 30+ columns

我有一个 JDBC 结果集,我试图将其导出为 Word 文档 (docx) 中的 table。 JDBC 结果集有 30 多列;当生成 Word 文档时,它只显示 10 列,其余的被隐藏。如何显示剩余的 20 多列?有没有办法让 Word 文档方向横向或设置自定义页面大小?

这是我的 Java 代码:

@Override
public Void extractData(ResultSet rs) throws SQLException, DataAccessException {

    ResultSetMetaData rsmd = rs.getMetaData();
    int columnsNumber = rsmd.getColumnCount();
    System.out.println("Columns : " + columnsNumber);
    XWPFDocument document = new XWPFDocument();
    // Creating Table with 1 row and as many columns as in the result set
    XWPFTable table = document.createTable(1, columnsNumber);
    table.setWidth("100%");
    //Get header Row
    XWPFTableRow header = table.getRow(0);
    long c = header.getTableCells().stream().count();
    System.out.println("Count of cells : "+ c);
    //Set header columns
    for (int col = 0; col < columnsNumber; col++) {
        System.out.println("header col :  " + col);
        header.getCell(col).setText(rsmd.getColumnLabel(col + 1));
    }
    //Set data rows
    while (rs.next()) {
        System.out.println("RS row :  " + rs.getRow());
        XWPFTableRow desRow = table.createRow();
        for (int col = 0; col < columnsNumber; col++) {
            final var value = rs.getObject(col + 1);
            String v = value == null ? "" : value.toString();
            desRow.getCell(col).setText(v);
            System.out.println("RS row col : "+ col);
        }
    }
    try {
        document.write(os);
        document.close();
    } catch (IOException e) {
        log.error("Error occurred: {0}", e);
    }
    return null;
}

正在使用 Maven 依赖项

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.0.0</version>
</dependency>

已生成 table 的快照

可以使用 Apache POI 为页面设置设置景观。有关完整示例,请参阅 Avoid Word displaying blank page at the beginning of .doc generated by Apache POI

但是使用纸张尺寸 Letter,具有 30 列的 table 即使在横向模式下也可能不适合页面。一个Wordtable的列宽不能小于其中最宽的不可分词。因此,适应 table 的唯一方法可能是缩小字体大小。

在下面的完整示例中,当使用字体大小 6 时,table 仅适合纸张大小 Letter in landscape。

另一种可能性是使用更大的纸张格式。示例中显示了如何设置 A3 纸张格式。 table 也适合横向使用更大的字体大小。

import java.io.FileOutputStream;

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

import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

public class CreateWordTableLandscape {
    
 static void setFontSize(XWPFTableCell cell, int size) {
  for (XWPFParagraph paragraph : cell.getParagraphs()) {
   for (XWPFRun run : paragraph.getRuns()) {
    run.setFontSize(size);  
   }
  }
 } 
        
 public static void main(String[] args) throws Exception {

  int columnsNumber = 30;
  
  String[] columLabels = new String[columnsNumber];
  for (int c = 0; c < columnsNumber; c++) {
   columLabels[c] = "Column name " + (c+1);  
  }

  XWPFDocument document= new XWPFDocument();

  CTDocument1 ctDocument = document.getDocument();
  CTBody ctBody = ctDocument.getBody();
  CTSectPr ctSectPr = (ctBody.isSetSectPr())?ctBody.getSectPr():ctBody.addNewSectPr();
  CTPageSz ctPageSz = (ctSectPr.isSetPgSz())?ctSectPr.getPgSz():ctSectPr.addNewPgSz();
  ctPageSz.setOrient(STPageOrientation.LANDSCAPE);
  
  //paper size letter
  ctPageSz.setW(java.math.BigInteger.valueOf(Math.round(11 * 1440))); //11 inches
  ctPageSz.setH(java.math.BigInteger.valueOf(Math.round(8.5 * 1440))); //8.5 inches
  
  //paper size A3
  //ctPageSz.setW(java.math.BigInteger.valueOf(Math.round(16.5 * 1440))); //16.5 inches
  //ctPageSz.setH(java.math.BigInteger.valueOf(Math.round(11.7 * 1440))); //11.7 inches

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("The table");
  
  // Creating Table with 1 row and as many columns as in the result set
  XWPFTable table = document.createTable(1, columnsNumber);
  table.setWidth("100%");
  //Get header Row
  XWPFTableRow header = table.getRow(0);
  //Set header columns
  for (int col = 0; col < columnsNumber; col++) {
   header.getCell(col).setText(columLabels[col]);
   //setFontSize(header.getCell(col), 6);
  }
  
  FileOutputStream out = new FileOutputStream("./CreateWordTableLandscape.docx");  
  document.write(out);
  out.close();
  document.close();

 }
}

要 运行 此代码,您需要 poi-ooxml-full-5.0.0.jar 在 class 路径中。参见 https://poi.apache.org/help/faq.html#faq-N10025

并且您必须在class路径中有任何旧的ooxml-schemas-*.jar