java - java - 如何使用java apache poi将表格放在单词的标题中?
问题描述
XWPFDocument document = new XWPFDocument();
XWPFTable table = document.createTable();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun paragraphOneRunOne = paragraph.createRun();
int twipsPerInch = 1500;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
table.setWidth(5*1800);
XWPFTableRow tableRowOne = table.getRow(0);
tableRowOne.getCell(0).setText(CUSTOMER_NAME);
tableRowOne.addNewTableCell().setText(displayName);
tableRowOne.addNewTableCell().setText(CUSTOMER_ID);
tableRowOne.addNewTableCell().setText(displayCustomerID);
XWPFTableRow tableRowTwo = table.createRow();
tableRowTwo.getCell(0).setText(AGE_GENDER);
tableRowTwo.getCell(1).setText(displayCustomerAge);
tableRowTwo.getCell(2).setText(VISIT_DATE);
tableRowTwo.getCell(3).setText(formatedDate);
XWPFTableRow tableRowThree = table.createRow();
tableRowThree.getCell(0).setText(REFERRED_BY);
tableRowThree.getCell(1).setText(displayRefBy);
paragraphOneRunOne.addCarriageReturn();
paragraphOneRunOne.setText("Patient Report");
paragraphOneRunOne.setBold(true);
paragraphOneRunOne.setUnderline(UnderlinePatterns.SINGLE);
paragraphOneRunOne.setFontSize(18);
//paragraphOneRunOne.setColor(255,0,0);
paragraphOneRunOne.setFontFamily("Times New Roman");
paragraph.setAlignment(ParagraphAlignment.CENTER);
paragraphOneRunOne.addBreak();
paragraphOneRunOne.addCarriageReturn();
现在你能解释一下在哪里修改我的表格以及如何将它放在标题中。我尝试了以前的答案。但这对我不起作用。
解决方案
使用当前apache poi 5.0.0
XWPFHeaderFooter
提供的方法XWPFHeaderFooter.createTable。因此可以将表格直接放入表头。
您的代码片段作为最小的可重现示例提供,以展示如何做:
import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.wp.usermodel.HeaderFooterType;
public class CreateWordTableInHeader {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph;
XWPFRun run;
XWPFTable table;
XWPFTableRow row;
//the header
XWPFHeader header = document.createHeader(HeaderFooterType.DEFAULT);
table = header.createTable(3, 4);
table.setWidth("100%");
row = table.getRow(0);
row.getCell(0).setText("CUSTOMER_NAME");
row.getCell(1).setText("displayName");
row.getCell(2).setText("CUSTOMER_ID");
row.getCell(3).setText("displayCustomerID");
row = table.getRow(1);
row.getCell(0).setText("AGE_GENDER");
row.getCell(1).setText("displayCustomerAge");
row.getCell(2).setText("VISIT_DATE");
row.getCell(3).setText("formatedDate");
row = table.getRow(2);
row.getCell(0).setText("REFERRED_BY");
row.getCell(1).setText("displayRefBy");
paragraph = header.createParagraph();
//the body
paragraph = document.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
run = paragraph.createRun();
run.setText("Patient Report");
run.setBold(true);
run.setUnderline(UnderlinePatterns.SINGLE);
run.setFontSize(18);
run.setFontFamily("Times New Roman");
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("CreateWordTableInHeader.docx");
document.write(out);
out.close();
document.close();
}
}
推荐阅读
- c - 带哨兵的链表
- javascript - 在 for() 循环中编辑变量的值
- php - wp_nav_menu 如何将整个子菜单包装在 div 中
- bootstrap-4 - Bootstrap 4 导航栏切换按钮是否可以在打开另一个菜单之前关闭先前打开的菜单?
- python - 如何避免 pytest 设备中的状态依赖?
- android - Android Studio 4.1.1“运行”不编译代码
- python - 如何有效地检查列表中的值是否存在于字典列表中
- python - python中的随机数
- xamarin - 如何在 C# 中向 StackLayout 模板添加两个标签?
- c# - 通过 Postman 将字符串发布到 C# AWS Lambda