java - Apache POI Word 文档中表格单元格的内容居中并加粗

标签 java ms-word apache-poi alignment

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

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 中获取 XWPFParagraph,然后从段落中获取 XWPFRun。然后您可以设置段落对齐方式和文本格式。

参见 XWPFTableCell用于获取 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 后段落和运行不存在。

关于java - Apache POI Word 文档中表格单元格的内容居中并加粗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66208377/

相关文章:

java - 如何检测 Scanner.next() 的第一个标记是否是字符串的整数?

java - JDBC VS Hibernate

vb.net - 使用 VB.NET 检查 Word 文档中的字体样式

linux - 捕获一张 Excel 工作表

java - x x x 输入的 JFrame setsize 不是正方形

用于获取大小和删除元素模式的 Java 线程安全集合

powershell - 从 Powershell 读取丢失的 Word 模板

c# - 段落列表不包含完整内容

android - 使用 Apache Poi 将 doc 文件转换为 html

java - Apache poi : images in spreadsheet are associated with workbook and can not be retrieved/linked with individual worksheet . 。