首页 新闻 赞助 找找看

poi操作页眉问题

0
[待解决问题]

有人知道怎么用poi给word页眉加入表格吗

嘟嘟zzz的主页 嘟嘟zzz | 菜鸟二级 | 园豆:202
提问于:2019-07-30 17:16
< >
分享
所有回答(1)
0

试了下免费版Spire.Doc for Java(https://www.e-iceblue.cn/Downloads/Free-Spire-PDF-JAVA.html),可以,页眉中添加图片、文字、表格都行:
参考代码:
import com.spire.doc.*;
import com.spire.doc.documents.DefaultTableStyle;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.TableRowHeightType;
import com.spire.doc.documents.VerticalAlignment;
import com.spire.doc.fields.TextRange;

public class AddTableToHeader {
public static void main(String[]args){
//加载需要添加页眉页脚的文档
Document doc= new Document("test.docx");
Section sec = doc.getSections().get(0);

    //调用方法添加页眉页脚
    AddHeaderFooter(sec);

    //保存文档
    doc.saveToFile("AddTableToHeader.docx");
}
//自定义方法来添加图片、文字页眉及页码
private static void AddHeaderFooter(Section sec){
    HeaderFooter header = sec.getHeadersFooters().getHeader();
    //添加表格到页眉
    Table table = header.addTable();
    table.resetCells(2,2);//指定表格行数、列数
    table.applyStyle(DefaultTableStyle.Light_Grid);//设置表格样式

    //声明表格数组内容
    String[][] data ={
            new String[]{"编号","姓名"},
            new String[]{"1","Lily"},
    };
    //填充数组内容到表格
    for (int i = 0; i < data.length; i++) {
        TableRow dataRow = table.getRows().get(i);
        dataRow.getCells().get(i).setWidth(25);
        dataRow.setHeight(20);
        dataRow.setHeightType(TableRowHeightType.Exactly);
        for (int j = 0; j < data[i].length; j++) {
            dataRow.getCells().get(j).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
            TextRange range = dataRow.getCells().get(j).addParagraph().appendText(data[i][j]);
            range.getCharacterFormat().setFontName("楷体");
            range.getCharacterFormat().setFontSize(11f);
            range.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        }
    }

}

}
效果图:

E-iceblue | 园豆:431 (菜鸟二级) | 2019-07-30 18:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册