如何正确地拆分表中列具有不同宽度的行。 我通过Java Apache POI生成Word文档。目的是获得下表correct output table format(其中列具有不同的宽度)
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import java.io.FileOutputStream;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
try {
XWPFDocument docx = new XWPFDocument();
XWPFParagraph paragraph = docx.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
paragraph.createRun().setText("Header");
XWPFTable table1 = createTable(docx, 1, 3);
tableParagraph(table1.getRow(0).getCell(0), "One");
widthCellsAcrossRow(table1, 0, 0, 1);
addGridSpan(table1, 0, 1, 2);
table1.getRow(0).getCell(2).getCTTc().newCursor().removeXml();
tableParagraph(table1.getRow(0).getCell(1), "Paragraph");
XWPFTable table2 = createTable(docx, 1, 3);
addGridSpan(table2, 0, 0, 3);
table2.getRow(0).getCell(2).getCTTc().newCursor().removeXml();
table2.getRow(0).getCell(1).getCTTc().newCursor().removeXml();
XWPFTable table3 = createTable(docx, 1, 3);
tableParagraph(table3.getRow(0).getCell(0), "One Two");
addGridSpan(table3, 0, 1, 2);
table3.getRow(0).getCell(2).getCTTc().newCursor().removeXml();
tableParagraph(table3.getRow(0).getCell(1), "Paragraph");
XWPFTable table4 = createTable(docx, 1, 3);
addGridSpan(table4, 0, 0, 3);
table4.getRow(0).getCell(2).getCTTc().newCursor().removeXml();
table4.getRow(0).getCell(1).getCTTc().newCursor().removeXml();
XWPFTable table5 = createTable(docx, 1, 3);
tableParagraph(table5.getRow(0).getCell(0), "One Two Three");
addGridSpan(table5, 0, 1, 2);
table5.getRow(0).getCell(2).getCTTc().newCursor().removeXml();
tableParagraph(table5.getRow(0).getCell(1), "Paragraph");
XWPFTable table6 = createTable(docx, 1, 3);
addGridSpan(table6, 0, 0, 3);
table6.getRow(0).getCell(2).getCTTc().newCursor().removeXml();
table6.getRow(0).getCell(1).getCTTc().newCursor().removeXml();
XWPFTable table7 = createTable(docx, 1, 3);
tableParagraph(table7.getRow(0).getCell(0), "One");
tableParagraph(table7.getRow(0).getCell(1), "Two");
tableParagraph(table7.getRow(0).getCell(2), "Three");
FileOutputStream outStream = new FileOutputStream("SplitTable.docx");
docx.write(outStream);
outStream.close();
} catch (Exception e) {
System.out.println(e);
}
}
private static void widthCellsAcrossRow(XWPFTable table, int rowNum, int colNum, int width) {
XWPFTableCell cell = table.getRow(rowNum).getCell(colNum);
if (cell.getCTTc().getTcPr() == null)
cell.getCTTc().addNewTcPr();
if (cell.getCTTc().getTcPr().getTcW() == null)
cell.getCTTc().getTcPr().addNewTcW();
cell.getCTTc().getTcPr().getTcW().setW(BigInteger.valueOf((long) width));
}
private static void addGridSpan(XWPFTable table, int rowNum, int colNum, int val) throws Exception {
XWPFTableCell cell = table.getRow(rowNum).getCell(colNum);
cell.getCTTc().addNewTcPr();
cell.getCTTc().getTcPr().addNewGridSpan();
cell.getCTTc().getTcPr().getGridSpan().setVal(BigInteger.valueOf(val));
}
private static XWPFTable createTable(XWPFDocument docx, int rows, int cols) {
XWPFTable tbl;
if (rows != 0 && cols != 0)
tbl = docx.createTable(rows, cols);
else
tbl = docx.createTable();
CTTbl table = tbl.getCTTbl();
CTTblPr pr = table.getTblPr();
CTTblWidth tableWidth = pr.getTblW();
tableWidth.setW(BigInteger.valueOf(5000));
tableWidth.setType(STTblWidth.PCT);
pr.setTblW(tableWidth);
table.setTblPr(pr);
return tbl;
}
private static XWPFParagraph tableParagraph(XWPFTableCell cell, String text) throws Exception {
XWPFParagraph paragraph = cell.addParagraph();
cell.removeParagraph(0);
paragraph.createRun().setText(text);
return paragraph;
}
}
我的代码生成一个具有固定列宽的表。 now that I have我哪里错了?我可以使用什么方法?