Apache POI + Office Word 生成页码

需求: 添加页码

		<properties>
			<easypoi.version>4.0.0</easypoi.version>
		</properties>
        <!-- easypoi -->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>${easypoi.version}</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>${easypoi.version}</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>${easypoi.version}</version>
        </dependency>
package com.platform.modules.sas.utils;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.*;


/**
 * ClassName   FooterTest
 * Description add footer to word with poi
 * Author  Kalinda
 * Date  2020/7/1 13:54
 * Version 1.0
 */
public class FooterTest {

    public static void main(String[] args) {
        try {
            // file path
            File is = new File("D:/test.docx");
            FileInputStream fis = new FileInputStream(is);
            // document object
            XWPFDocument doc = new XWPFDocument(fis);

			// calling method
            createFooter(doc);
            // or this method both ok!
            // createFooter(doc);
            
            // output
            OutputStream os = new FileOutputStream("D:\\Test1.docx");
            doc.write(os);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void createFooter(XWPFDocument doc) {

        // create footer
        XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
        CTP ctpFooter = CTP.Factory.newInstance();

        XWPFParagraph[] parsFooter;

        // add style (s.th.)
        CTPPr ctppr = ctpFooter.addNewPPr();
        CTString pst = ctppr.addNewPStyle();
        pst.setVal("style21");
        CTJc ctjc = ctppr.addNewJc();
        ctjc.setVal(STJc.CENTER);
        ctppr.addNewRPr();

        // add everything from the footerXXX.xml you need
        CTR ctr = ctpFooter.addNewR();
        ctr.addNewRPr();
        CTFldChar fch = ctr.addNewFldChar();
        fch.setFldCharType(STFldCharType.BEGIN);

        ctr = ctpFooter.addNewR();
        ctr.addNewInstrText().setStringValue(" PAGE ");

        ctpFooter.addNewR().addNewFldChar().setFldCharType(STFldCharType.SEPARATE);

        ctpFooter.addNewR().addNewT().setStringValue("1");

        ctpFooter.addNewR().addNewFldChar().setFldCharType(STFldCharType.END);

        XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, doc);

        parsFooter = new XWPFParagraph[1];

        parsFooter[0] = footerParagraph;

        policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);
    }
    
	public static void createDefaultFooter(final XWPFDocument document) {
        CTP pageNo = CTP.Factory.newInstance();
        XWPFParagraph footer = new XWPFParagraph(pageNo, document);
        
        CTPPr begin = pageNo.addNewPPr();
        begin.addNewPStyle().setVal("style21");
        begin.addNewJc().setVal(STJc.CENTER);
        
        pageNo.addNewR().addNewFldChar().setFldCharType(STFldCharType.BEGIN);
        pageNo.addNewR().addNewInstrText().setStringValue("PAGE   \\* MERGEFORMAT");
        pageNo.addNewR().addNewFldChar().setFldCharType(STFldCharType.SEPARATE);
        
        CTR end = pageNo.addNewR();
        CTRPr endRPr = end.addNewRPr();
        endRPr.addNewNoProof();
        endRPr.addNewLang().setVal("zh-CN");
        end.addNewFldChar().setFldCharType(STFldCharType.END);
        
        CTSectPr sectPr = document.getDocument().getBody().isSetSectPr() ? document.getDocument().getBody().getSectPr() : docx.getDocument().getBody().addNewSectPr();
        
        XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
        policy.createFooter(STHdrFtr.DEFAULT, new XWPFParagraph[] { footer });
    }
   
}

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用Apache POI库在Word文档中生成表格可以通过以下步骤完成: 1. 导入必要的POI库: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 编写代码来创建表格并添加内容: ```java import org.apache.poi.xwpf.usermodel.*; import java.io.FileOutputStream; import java.io.IOException; public class WordTableGenerator { public static void main(String[] args) { XWPFDocument document = new XWPFDocument(); // 创建一个表格 XWPFTable table = document.createTable(); // 添加表头 XWPFTableRow headerRow = table.getRow(0); headerRow.getCell(0).setText("姓名"); headerRow.addNewTableCell().setText("年龄"); headerRow.addNewTableCell().setText("性别"); // 添加数据行 XWPFTableRow dataRow1 = table.createRow(); dataRow1.getCell(0).setText("张三"); dataRow1.getCell(1).setText("25"); dataRow1.getCell(2).setText("男"); XWPFTableRow dataRow2 = table.createRow(); dataRow2.getCell(0).setText("李四"); dataRow2.getCell(1).setText("30"); dataRow2.getCell(2).setText("女"); // 保存文档 try { FileOutputStream out = new FileOutputStream("example.docx"); document.write(out); out.close(); System.out.println("生成Word文档成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 上述代码创建了一个包含表头和两行数据的表格,并将其保存为名为`example.docx`的Word文档。你可以根据需要添加更多的表格行和单元格,并设置样式。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值