[英]Center and bold the contents of table cells in Apache POI Word document
如何在 Word 文檔中使用 Apache POI 將表格單元格的內容居中並加粗? 這是我用來構建表的代碼:
XWPFDocument document = new XWPFDocument();
XWPFTable table = document.createTable();
XWPFTableRow tableRowOne = table.getRow(0);
tableRowOne.getCell(0).setText("CUSTOMER_NAME");
tableRowOne.addNewTableCell().setText("Kumar");
tableRowOne.addNewTableCell().setText("CUSTOMER_ID");
tableRowOne.addNewTableCell().setText("123");
XWPFTableRow tableRowTwo = table.createRow();
tableRowTwo.getCell(0).setText("AGE_GENDER");
tableRowTwo.getCell(1).setText("25/M");
tableRowTwo.getCell(2).setText("VISIT_DATE");
tableRowTwo.getCell(3).setText("11/02/2021");
XWPFTableRow tableRowThree = table.createRow();
tableRowThree.getCell(0).setText("REFERRED_BY");
tableRowThree.getCell(1).setText("Self");
在Word
中,文本格式存儲在文本運行XWPFRun
中。 段落 alignment 存儲在段落XWPFParagraph
中。 這也適用於表格。 因此,您需要從XWPFTableCell
XWPFParagraph
然后從段落中獲取XWPFRun
。 然后您可以設置段落 alignment 和文本格式。
有關獲取XWPFParagraph的方法,請參閱XWPFParagraph
。
完整示例:
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
public class CreateWordTable {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("The table:");
XWPFTable table = document.createTable();
table.setWidth("100%");
XWPFTableRow tableRow = table.getRow(0);
tableRow.getCell(0).setText("CUSTOMER_NAME");
tableRow.getCell(0).getParagraphs().get(0).getRuns().get(0).setBold(true);
tableRow.addNewTableCell().setText("Kumar");
tableRow.getCell(1).getParagraphs().get(0).setAlignment(ParagraphAlignment.CENTER);
tableRow.addNewTableCell().setText("CUSTOMER_ID");
tableRow.getCell(2).getParagraphs().get(0).getRuns().get(0).setBold(true);
tableRow.addNewTableCell().setText("123");
tableRow.getCell(3).getParagraphs().get(0).setAlignment(ParagraphAlignment.RIGHT);
tableRow = table.createRow();
tableRow.getCell(0).setText("AGE_GENDER");
tableRow.getCell(0).getParagraphs().get(0).getRuns().get(0).setBold(true);
tableRow.getCell(1).setText("25/M");
tableRow.getCell(1).getParagraphs().get(0).setAlignment(ParagraphAlignment.CENTER);
tableRow.getCell(2).setText("VISIT_DATE");
tableRow.getCell(2).getParagraphs().get(0).getRuns().get(0).setBold(true);
tableRow.getCell(3).setText("11/02/2021");
tableRow.getCell(3).getParagraphs().get(0).setAlignment(ParagraphAlignment.RIGHT);
tableRow = table.createRow();
tableRow.getCell(0).setText("REFERRED_BY");
tableRow.getCell(0).getParagraphs().get(0).getRuns().get(0).setBold(true);
tableRow.getCell(1).setText("Self");
tableRow.getCell(1).getParagraphs().get(0).setAlignment(ParagraphAlignment.CENTER);
tableRow.getCell(2).setText("");
tableRow.getCell(3).setText("");
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("CreateWordTable.docx");
document.write(out);
out.close();
document.close();
}
}
注意:這適用於當前apache poi 5.0.0
。 以前的版本在XWPFTableCell.setText
中存在錯誤,因此在調用XWPFTableCell.setText
后段落和運行不存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.