Hareesh
Hareesh

Reputation: 43

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

How can I center and bold the content of the table cells using Apache POI in a Word document? This is the code I use to build the 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");

Upvotes: 2

Views: 5297

Answers (1)

Axel Richter
Axel Richter

Reputation: 61945

In Word text formatting is stored in text runs XWPFRun. Paragraph alignment is stored in paragraphs XWPFParagraph. This also is true for tables. So you need get XWPFParagraphs from XWPFTableCell and then XWPFRuns from the paragraphs. Then you can set paragraph alignment and text formatting.

See XWPFTableCell for methods to get XWPFParagraphs.

Complete Example:

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();

 }
}

Note: This works using current apache poi 5.0.0. Former versions had bugs in XWPFTableCell.setText so paragraphs and runs were not present after XWPFTableCell.setText was called.

Upvotes: 11

Related Questions