2016-09-19 6 views
1

XWPFヘッダー(またはフッター)に新しい簡単な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(); 

ご協力いただければ幸いです。

種類について...

〜ダニエル

答えて

3

これはpublic XWPFTable insertNewTbl(org.apache.xmlbeans.XmlCursor cursor)を使用して可能です。

アプローチ: ヘッダーに新しい段落を作成します。その段落の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.createRow(); 
    int twipsPerInch = 1440; 
    table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(6 * twipsPerInch)); 
    for (int i = 0; i < 3; i++) { 
    XWPFTableCell cell = row.createCell(); 
    CTTblWidth tblWidth = cell.getCTTc().addNewTcPr().addNewTcW(); 
    tblWidth.setW(BigInteger.valueOf(2 * twipsPerInch)); 
    tblWidth.setType(STTblWidth.DXA); 
    paragraph = cell.getParagraphs().get(0); 
    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:"); 

    doc.write(new FileOutputStream("CreateWordHeaderFooterTable.docx")); 

} 
} 
+0

ちょっとアクセル、どうもありがとうございました。完璧に動作します。驚くばかり!!! – Daniel

関連する問題