Har*_*esh 0 java ms-word alignment apache-poi
如何在 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");
Run Code Online (Sandbox Code Playgroud)
在Word文本格式存储在文本运行XWPFRun。段落对齐方式存储在段落中XWPFParagraph。对于表也是如此。所以你需要从段落中获取XWPFParagraphs XWPFTableCell,然后XWPFRun从段落中获取s 。然后您可以设置段落对齐方式和文本格式。
有关获取s 的方法,请参阅XWPFTableCellXWPFParagraph。
完整示例:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
注意:这适用于当前的apache poi 5.0.0. 以前的版本存在错误,XWPFTableCell.setText因此XWPFTableCell.setText调用后不存在段落和运行。
| 归档时间: |
|
| 查看次数: |
332 次 |
| 最近记录: |