Apache POI Word 文档中 table 个单元格的内容居中并加粗

Center and bold the contents of table cells in Apache POI Word document

如何使用 Apache POI 在 Word 文档中将 table 单元格的内容居中并加粗?这是我用来构建 table:

的代码
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 中。段落对齐存储在段落 XWPFParagraph 中。表格也是如此。因此,您需要从 XWPFTableCell 中获取 XWPFParagraphs,然后从段落中获取 XWPFRuns。然后您可以设置段落对齐方式和文本格式。

有关获取 XWPFParagraphs 的方法,请参阅 XWPFTableCell

完整示例:

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 后段落和运行不存在。