问题
I want to create a table in my word document. In this table, I need something like this
In this image, third row's third column has different height from first and second columns. I want to create something like this with apache poi but I don't find any solution for this.
My codes is here
table.getRow(6).setHeight(630);
table.getRow(6).getCtRow().getTrPr().getTrHeightArray(0).setHRule(STHeightRule.EXACT);
table.getRow(6).getCell(0).removeParagraph(0); //Cell0
XWPFParagraph row6Paragraph=table.getRow(6).getCell(0).addParagraph();
row6Paragraph.setAlignment(ParagraphAlignment.LEFT);
row6Paragraph.setVerticalAlignment(TextAlignment.CENTER);
XWPFRun row6Run=row6Paragraph.createRun();
row6Run.setFontFamily("Arial");
row6Run.setFontSize(10);
row6Run.setText("5) Engine Induction Date");
row6Run.addBreak();
String indDate="12-09-2018";
row6Run.setText(" "+indDate);
table.getRow(6).getCell(0).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(4985));
table.getRow(6).getCell(1).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(365));//cell1
table.getRow(6).getCell(2).removeParagraph(0); //Cell2
row6Paragraph=table.getRow(6).getCell(2).addParagraph();
row6Paragraph.setAlignment(ParagraphAlignment.LEFT);
row6Paragraph.setVerticalAlignment(TextAlignment.CENTER);
XWPFRun row6Run1=row6Paragraph.createRun();
row6Run1.setFontFamily("Arial");
row6Run1.setFontSize(10);
row6Run1.setBold(true);
row6Run1.setText("PART INFORMATION");
table.getRow(6).getCell(2).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(4985));
回答1:
You should switch on showing non-printing characters in Word
(¶). Without seeing those characters it is not really clear from your screen shot. But creating this using only one table by merging cells would be really hard, not only using apache poi
but also using Word
's GUI
. So I suspect this are three tables.
Table one is in a one column section and only contains one row having one cell containing the header.
Table two then is placed in a two columns section of the word document. Then the table breaks to the second column if needed. It has only one column and each row is as height as the content.
Table three is in a one column section again and only contains one row having one cell containing the footer.
Example using current apache poi 4.1.2
:
import java.io.FileOutputStream;
import java.math.BigInteger;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
public class Word2ColumnPageWithTable {
public static void main(String[] args) throws Exception {
XWPFDocument document= new XWPFDocument();
//one column section
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("One column on top.");
//table over full width of one column section
XWPFTable table = document.createTable();
table.setWidth("100%");
XWPFTableRow row = table.getRow(0);
row.getCell(0).setColor("808080");
row.getCell(0).getParagraphArray(0).setAlignment(ParagraphAlignment.CENTER);
row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
row.getCell(0).getParagraphArray(0).createRun().setText("Table Header");
row.getCell(0).getParagraphArray(0).getRuns().get(0).setBold(true);
row.getCell(0).getParagraphArray(0).getRuns().get(0).setColor("FFFFFF");
//paragraph with section setting for one column section above
paragraph = document.createParagraph();
CTSectPr ctSectPr = paragraph.getCTP().addNewPPr().addNewSectPr();
CTColumns ctColumns = ctSectPr.addNewCols();
ctColumns.setNum(BigInteger.valueOf(1));
//two column section
//table over full width of one column in two column section
table = document.createTable();
table.setWidth("100%");
row = table.getRow(0);
row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
row.getCell(0).getParagraphArray(0).createRun().setText("SHORT CONTENT");
row.getCell(0).getParagraphArray(0).getRuns().get(0).setBold(true);
table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
table.createRow().getCell(0).setText("Long content, which increases the row height.");
table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
table.createRow().getCell(0).setText("Long content, which increases the row height.");
row = table.createRow();
row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
row.getCell(0).getParagraphArray(0).createRun().setText("SHORT CONTENT");
row.getCell(0).getParagraphArray(0).getRuns().get(0).setBold(true);
table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
row = table.createRow();
row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
row.getCell(0).getParagraphArray(0).createRun().setText("SHORT CONTENT");
row.getCell(0).getParagraphArray(0).getRuns().get(0).setBold(true);
table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
row = table.createRow();
row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
row.getCell(0).getParagraphArray(0).createRun().setText("SHORT CONTENT");
row.getCell(0).getParagraphArray(0).getRuns().get(0).setBold(true);
table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
table.createRow().getCell(0).setText("Long content, which increases the row height.");
table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
table.createRow().getCell(0).setText("Long content, which increases the row height.");
//paragraph with section break continuous for two column section above
paragraph = document.createParagraph();
ctSectPr = paragraph.getCTP().addNewPPr().addNewSectPr();
ctSectPr.addNewType().setVal(STSectionMark.CONTINUOUS);
ctColumns = ctSectPr.addNewCols();
ctColumns.setNum(BigInteger.valueOf(2));
ctColumns.setEqualWidth(STOnOff.ON);
//one column section again
//table over full width of one column section
table = document.createTable();
table.setWidth("100%");
row = table.getRow(0);
row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
row.getCell(0).getParagraphArray(0).createRun().setText("TABLE FOOTER");
paragraph = document.createParagraph();
paragraph = document.createParagraph();
run=paragraph.createRun();
run.setText("One column on bottom.");
//section setting continuous for one column section above and whole document from here on
CTDocument1 ctDocument = document.getDocument();
CTBody ctBody = ctDocument.getBody();
ctSectPr = ctBody.addNewSectPr();
ctSectPr.addNewType().setVal(STSectionMark.CONTINUOUS);
ctColumns = ctSectPr.addNewCols();
ctColumns.setNum(BigInteger.valueOf(1));
FileOutputStream out = new FileOutputStream("Word2ColumnPageWithTable.docx");
document.write(out);
out.close();
document.close();
}
}
This code produces a Word2ColumnPageWithTable.docx
which looks like so:
来源:https://stackoverflow.com/questions/61500639/how-can-i-set-table-rows-height-different-each-column-in-word-via-apache-poi