Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将表格添加到页眉或页脚?_Java_Apache Poi_Xwpf - Fatal编程技术网

Java 如何将表格添加到页眉或页脚?

Java 如何将表格添加到页眉或页脚?,java,apache-poi,xwpf,Java,Apache Poi,Xwpf,我在向XWPFHeader(或Footer)添加新的简单XWPFTable时遇到了严重的问题。不幸的是,似乎只有一个人有同样的问题() 有人有办法做到这一点吗 XWPFDocument docx = (XWPFDocument) dockingObject; CTSectPr sectPr = docx.getDocument().getBody().addNewSectPr(); XWPFHeaderFooterPolicy policy = new WPFHeaderFoote

我在向XWPFHeader(或Footer)添加新的简单XWPFTable时遇到了严重的问题。不幸的是,似乎只有一个人有同样的问题()

有人有办法做到这一点吗

  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();
任何帮助都将不胜感激

亲切的问候

~Daniel

这是可以使用的

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

例如:

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

 }
}
import java.io.*;
导入org.apache.poi.xwpf.usermodel.*;
导入org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
导入org.apache.xmlbeans.XmlCursor;
导入java.math.biginger;
公共类CreateWordHeaderFooterTable{
公共静态void main(字符串[]args)引发异常{
XWPFDocument doc=新XWPFDocument();
//正文内容
XWPFParagraph paragraph paragraph=doc.createParagraph();
XWPFRun=段落.createRun();
run.setText(“主体:”);
段落=doc.create段落();
run=段落.createRun();
run.setText(“Lorem ipsum…”);
//创建页眉页脚
CTSectPr sectPr=doc.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy headerFooterPolicy=新的XWPFHeaderFooterPolicy(doc,sectPr);
//创建标题开始
XWPFHeader header=headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
段落=标题。getParagraphArray(0);
如果(段落==null)段落=header.create段落();
段落.设置对齐(段落对齐.左);
run=段落.createRun();
run.setText(“标题:”);
//在标题中创建表
段落=标题。创建段落();
XmlCursor cursor=段落.getCTP().newCursor();
XWPFTable table=表头.insertNewTbl(光标);
XWPFTableRow=table.getRow(0);if(row==null)row=table.createRow();
int twipperinch=1440;
table.getCTTbl();
对于(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*twipperinch));
设置类型(STTblWidth.DXA);
if(cell.getPages().size()>0){
段落=cell.get段落().get(0);
}否则{
段落=单元格。添加段落();
}
run=段落.createRun();
run.setText(“标题表单元格”+i);
}
//创建页脚开始
xwpfooter footer=headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
段落=页脚。getParagraphArray(0);
如果(段落==null)段落=footer.create段落();
段落.设置对齐(段落对齐.中间);
run=段落.createRun();
run.setText(“页脚:”);
FileOutputStream out=新的FileOutputStream(“CreateWordHeaderFooterTable.docx”);
写(出)文件;
out.close();
doc.close();
}
}
这可以使用

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

例如:

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

 }
}
import java.io.*;
导入org.apache.poi.xwpf.usermodel.*;
导入org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
导入org.apache.xmlbeans.XmlCursor;
导入java.math.biginger;
公共类CreateWordHeaderFooterTable{
公共静态void main(字符串[]args)引发异常{
XWPFDocument doc=新XWPFDocument();
//正文内容
XWPFParagraph paragraph paragraph=doc.createParagraph();
XWPFRun=段落.createRun();
run.setText(“主体:”);
段落=doc.create段落();
run=段落.createRun();
run.setText(“Lorem ipsum…”);
//创建页眉页脚
CTSectPr sectPr=doc.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy headerFooterPolicy=新的XWPFHeaderFooterPolicy(doc,sectPr);
//创建标题开始
XWPFHeader header=headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
段落=标题。getParagraphArray(0);
如果(段落==null)段落=header.create段落();
段落.设置对齐(段落对齐.左);
run=段落.createRun();
run.setText(“标题:”);
//在标题中创建表
段落=标题。创建段落();
XmlCursor cursor=段落.getCTP().newCursor();
XWPFTable table=表头.insertNewTbl(光标);
XWPFTableRow=table.getRow(0);if(row==null)row=table.createRow();
int twipperinch=1440;
table.getCTTbl();
对于(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*twipperinch));
设置类型(STTblWidth.DXA);
if(cell.getPages().size()>0){
段落=cell.get段落().get(0);
}否则{
段落=单元格。添加段落();
}
run=段落.createRun();
run.setText(“标题表单元格”+i);
}
//创建页脚开始
xwpfooter footer=headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
段落=页脚。getParagraphArray(0);
如果(段落==null)段落=footer.create段落();