POI-HWPF (doc 形式など) を使用して Word で表を作成したいと考えています。私のコード例は次のとおりです。
Table table = document.getRange().insertTableBefore((short) 2, 2);
テーブルが挿入されましたが、テーブルの幅が 0 のように見えません。
POI-HWPF (doc 形式など) を使用して Word で表を作成したいと考えています。私のコード例は次のとおりです。
Table table = document.getRange().insertTableBefore((short) 2, 2);
テーブルが挿入されましたが、テーブルの幅が 0 のように見えません。
この古いバグ レポートにリンクされているファイルを参照すると、その方法がわかります。
つまり、本質的には、Word にレンダリングするものがあるように、コンテンツ (つまり、セル内の段落) を追加する必要がある可能性があります。
バグ レポートで使用されているサンプル コードは次のとおりです。
private static void test (int rows, int columns) throws Exception {
// POI apparently can't create a document from scratch,
// so we need an existing empty dummy document
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("empty.doc"));
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
Table table = range.insertBefore(new TableProperties(columns), rows);
for (int rowIdx=0; rowIdx<table.numRows(); rowIdx++) {
TableRow row = table.getRow(rowIdx);
System.out.println("row "+rowIdx);
for (int colIdx=0; colIdx<row.numCells(); colIdx++) {
TableCell cell = row.getCell(colIdx);
System.out.println("column "+colIdx+", num paragraphs "+cell.numParagraphs());
try {
Paragraph par = cell.getParagraph(0);
par.insertBefore(""+(rowIdx*row.numCells()+colIdx));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}