如何使用JavaApachePOI将表放在word的头中?

如何使用JavaApachePOI将表放在word的头中?,java,ms-word,header,apache-poi,Java,Ms Word,Header,Apache Poi,现在你能解释一下在哪里修改我的表以及如何将它放在标题中吗。我试过前面的答案。但是它对我不起作用。使用当前的ApachePOI5.0.0XWPFHeaderFooter提供了一种方法。因此,可以将表格直接放入标题中 您提供的代码片段显示了如何执行以下操作: XWPFDocument document = new XWPFDocument(); XWPFTable table = document.createTable(); XWPFParagraph paragraph = document.c

现在你能解释一下在哪里修改我的表以及如何将它放在标题中吗。我试过前面的答案。但是它对我不起作用。

使用当前的
ApachePOI5.0.0
XWPFHeaderFooter
提供了一种方法。因此,可以将表格直接放入标题中

您提供的代码片段显示了如何执行以下操作:

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

如何在poi word(XWPF)中设置页眉和页脚位置。我以前见过你回答这个问题。我用上面的代码试过了。但它不起作用。它显示了CTPageMar的一些问题。请帮帮我。@Hareesh:你指的是什么?使用当前的apache-poi 5.0.0,这对我也适用。请注意参考。对于
apache poi 5.0.0
您需要
poi ooxml full jar
,否则一些
CT…
类将不在类路径中。是。我发现我找不到CTPageMar。@Hareesh:我需要从顶部增加标题约5厘米。除此之外还有其他选项吗
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();

 }
}