如何在页眉或页脚添加 table?

How to add a table to header or footer?

我在向 XWPFHeader(或页脚)添加新的简单 XWPFTable 时遇到了严重问题。不幸的是,似乎只有一个人遇到同样的问题 (https://bz.apache.org/bugzilla/show_bug.cgi?id=57366#c0)。

有没有人有办法实现这个??

  XWPFDocument docx = (XWPFDocument) dockingObject;

  CTSectPr sectPr = docx.getDocument().getBody().addNewSectPr();
  XWPFHeaderFooterPolicy policy = new WPFHeaderFooterPolicy(docx, sectPr);
  XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);

  // somthing like: header.createTable();

我们将不胜感激任何帮助!

此致...

~大牛

这可以使用 public XWPFTable insertNewTbl(org.apache.xmlbeans.XmlCursor cursor)

方法: 在 header 中创建一个新段落。然后从该段的 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP 中得到一个 org.apache.xmlbeans.XmlCursor。然后插入由该光标定位的 table。

示例:

import java.io.*;
import org.apache.poi.xwpf.usermodel.*;

import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

import org.apache.xmlbeans.XmlCursor;
import java.math.BigInteger;

public class CreateWordHeaderFooterTable {

 public static void main(String[] args) throws Exception {

  XWPFDocument doc= new XWPFDocument();

  // the body content
  XWPFParagraph paragraph = doc.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("The Body:");

  paragraph = doc.createParagraph();
  run=paragraph.createRun();  
  run.setText("Lorem ipsum....");

  // create header-footer
  CTSectPr sectPr = doc.getDocument().getBody().addNewSectPr();
  XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(doc, sectPr);

  // create header start
  XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);

  paragraph = header.getParagraphArray(0);
  if (paragraph == null) paragraph = header.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.LEFT);

  run = paragraph.createRun();  
  run.setText("The Header:");

  // create table in header
  paragraph = header.createParagraph();
  XmlCursor cursor = paragraph.getCTP().newCursor();
  XWPFTable table = header.insertNewTbl(cursor);
  XWPFTableRow row = table.getRow(0); if (row == null) row = table.createRow();
  int twipsPerInch =  1440;
  table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(6 * twipsPerInch));
  for (int i = 0; i < 3; i++) {
   XWPFTableCell cell = row.getCell(i); if (cell == null) cell = row.createCell();
   CTTblWidth tblWidth = cell.getCTTc().addNewTcPr().addNewTcW();
   tblWidth.setW(BigInteger.valueOf(2 * twipsPerInch));
   tblWidth.setType(STTblWidth.DXA);
   if (cell.getParagraphs().size() > 0) {
    paragraph = cell.getParagraphs().get(0);
   } else {
    paragraph = cell.addParagraph();   
   }
   run = paragraph.createRun();
   run.setText("Header Table Cell " + i);
  }

  // create footer start
  XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);

  paragraph = footer.getParagraphArray(0);
  if (paragraph == null) paragraph = footer.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.CENTER);

  run = paragraph.createRun();  
  run.setText("The Footer:");
  
  FileOutputStream out = new FileOutputStream("CreateWordHeaderFooterTable.docx");
  doc.write(out);
  out.close();
  doc.close();

 }
}

使用 Apache POI 在 docx 文档页脚中创建 table:

    XWPFHeaderFooterPolicy policy = document.getHeaderFooterPolicy();
    if (policy == null)
      policy = document.createHeaderFooterPolicy();

    XWPFHeaderFooter footer = policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);

    XWPFParagraph p = footer.createParagraph();
    p.createRun().setText("My First Footer");

    XWPFTable t = footer.createTable(1, 3);
    XWPFTableRow r = t.getRow(0);
    r.createCell().setText("Uno");
    r.createCell().setText("Dos");
    r.createCell().setText("Tres");