如何将表格添加到页眉或页脚? [英] How to add a table to header or footer?

查看:105
本文介绍了如何将表格添加到页眉或页脚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having serious trouble adding a new, simple XWPFTable to an XWPFHeader (or Footer). Unfortunately there seems to be only one guy having the same problem (https://bz.apache.org/bugzilla/show_bug.cgi?id=57366#c0).

有没有人有办法实现这一目标?

Does anyone has an approach to achieve this??

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

任何帮助将不胜感激!

亲切的问候...

~丹尼尔

推荐答案

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

方法:在标题中创建一个新段落.然后从该段落的 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP 中获取一个 org.apache.xmlbeans.XmlCursor.然后插入该光标所在的表格.

Approach: Create a new paragraph in the header. Then get a org.apache.xmlbeans.XmlCursor from the org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP of that paragraph. Then insert the table positioned by that cursor.

示例:

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

 }
}

这篇关于如何将表格添加到页眉或页脚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆