Java 处理word相关

该文章仅简短记录,相关具体操作自行查阅

1.poi-tl
是一个基于Apache POI的Word模板引擎
http://deepoove.com/poi-tl/#_why_poi_tl

WordTpl_1003_Utils

package com.louddt.contentSecurity.common.utils.excelWord;

import com.louddt.contentSecurity.common.exception.ApplicationRuntimeException;
import com.louddt.contentSecurity.common.utils.io.IOUtils;
import com.louddt.contentSecurity.common.viewvo.WordTplVarHeaderFor1003HelperVO;
import pl.jsolve.templ4docx.core.Docx;
import pl.jsolve.templ4docx.core.VariablePattern;
import pl.jsolve.templ4docx.variable.TextVariable;
import pl.jsolve.templ4docx.variable.Variables;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class WordTpl_1003_Utils
{
    public static byte[] genWordDoc(byte[] wordTemplateByteData, WordTplVarHeaderFor1003HelperVO wordTplVarHeaderVO)
    {
        ByteArrayOutputStream outputStream = null;
        try {
            outputStream = new ByteArrayOutputStream();
            genWordDoc(wordTemplateByteData, outputStream, wordTplVarHeaderVO);

            return outputStream.toByteArray();
        }
        catch (Exception e) {
            throw new ApplicationRuntimeException("生成审查简报word错误");
        }
        finally {
            IOUtils.closeQuietly(outputStream);
        }
    }

    public static void genWordDoc(byte[] wordTemplateByteData, OutputStream outputStream, WordTplVarHeaderFor1003HelperVO wordTplVarHeaderVO)
    {
        try
        {
            Docx docx = new Docx(new ByteArrayInputStream(wordTemplateByteData));
            docx.setVariablePattern(new VariablePattern("#{", "}"));

            docx.fillTemplate(buildVariablesForTemplate4Docx(wordTplVarHeaderVO));

            docx.save(outputStream);
        }
        catch(Exception e) {
            throw new ApplicationRuntimeException("生成审查简报word错误");
        }
    }

    public static Variables buildVariablesForTemplate4Docx(WordTplVarHeaderFor1003HelperVO wordTplVarHeaderVO)
    {
        Variables variables = new Variables();

        wordTplVarHeaderVO = wordTplVarHeaderVO == null ? new WordTplVarHeaderFor1003HelperVO() : wordTplVarHeaderVO;

        variables.addTextVariable(new TextVariable("#{sitesName}", safeStringVariable(wordTplVarHeaderVO.getSitesName())));
        variables.addTextVariable(new TextVariable("#{siteUri}", safeStringVariable(wordTplVarHeaderVO.getSiteUri())));
        variables.addTextVariable(new TextVariable("#{generateTime}", safeStringVariable(wordTplVarHeaderVO.getGenerateTime())));
        variables.addTextVariable(new TextVariable("#{platformName}", safeStringVariable(wordTplVarHeaderVO.getPlatformName())));
        variables.addTextVariable(new TextVariable("#{orderTypeName}", safeStringVariable(wordTplVarHeaderVO.getOrderTypeName())));
        variables.addTextVariable(new TextVariable("#{articleNum}", safeStringVariable(wordTplVarHeaderVO.getArticleNum())));
        variables.addTextVariable(new TextVariable("#{errorNum}", safeStringVariable(wordTplVarHeaderVO.getErrorNum())));
        variables.addTextVariable(new TextVariable("#{errorTypeNum}", safeStringVariable(wordTplVarHeaderVO.getErrorTypeNum())));
        variables.addTextVariable(new TextVariable("#{type_1_num}", safeStringVariable(wordTplVarHeaderVO.getType_1_num())));
        variables.addTextVariable(new TextVariable("#{type_2_num}", safeStringVariable(wordTplVarHeaderVO.getType_2_num())));
        variables.addTextVariable(new TextVariable("#{type_3_num}", safeStringVariable(wordTplVarHeaderVO.getType_3_num())));
        variables.addTextVariable(new TextVariable("#{type_4_num}", safeStringVariable(wordTplVarHeaderVO.getType_4_num())));
        variables.addTextVariable(new TextVariable("#{type_5_num}", safeStringVariable(wordTplVarHeaderVO.getType_5_num())));
        variables.addTextVariable(new TextVariable("#{type_6_num}", safeStringVariable(wordTplVarHeaderVO.getType_6_num())));
        variables.addTextVariable(new TextVariable("#{type_7_num}", safeStringVariable(wordTplVarHeaderVO.getType_7_num())));
        variables.addTextVariable(new TextVariable("#{type_8_num}", safeStringVariable(wordTplVarHeaderVO.getType_8_num())));
        variables.addTextVariable(new TextVariable("#{type_9_num}", safeStringVariable(wordTplVarHeaderVO.getType_9_num())));
        variables.addTextVariable(new TextVariable("#{type_10_num}", safeStringVariable(wordTplVarHeaderVO.getType_10_num())));
        variables.addTextVariable(new TextVariable("#{type_11_num}", safeStringVariable(wordTplVarHeaderVO.getType_11_num())));
        variables.addTextVariable(new TextVariable("#{type_12_num}", safeStringVariable(wordTplVarHeaderVO.getType_12_num())));

        variables.addTextVariable(new TextVariable("#{firstPublishTime}", safeStringVariable(wordTplVarHeaderVO.getFirstPublishTime())));
        variables.addTextVariable(new TextVariable("#{lastPublishTime}", safeStringVariable(wordTplVarHeaderVO.getLastPublishTime())));
        variables.addTextVariable(new TextVariable("#{totalArticleWordNum}", safeStringVariable(String.valueOf(wordTplVarHeaderVO.getTotalArticleWordNum()))));
        variables.addTextVariable(new TextVariable("#{errorArticleNum}", safeStringVariable(String.valueOf(wordTplVarHeaderVO.getErrorArticleNum()))));


        return variables;
    }

    protected static String safeStringVariable(String value)
    {
        if (value == null || value.trim().isEmpty())
        {
            return "";
        }

        return value;
    }

    protected static String safeDateVariable(Date value)
    {
        if (value == null) {
            return "";
        }
        else
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            return sdf.format(value);
        }
    }

}

WordTplVarHeaderFor1003HelperVO

package com.louddt.contentSecurity.common.viewvo;

public class WordTplVarHeaderFor1003HelperVO
{
    private String sitesName;

    private String siteUri;

    private String generateTime;

    private String platformName;

    private String orderTypeName;

    private String articleNum;

    private String errorNum;

    private String errorTypeNum;

    private String type_1_num;

    private String type_2_num;

    private String type_3_num;

    private String type_4_num;

    private String type_5_num;

    private String type_6_num;

    private String type_7_num;

    private String type_8_num;

    private String type_9_num;

    private String type_10_num;

    private String type_11_num;

    private String type_12_num;

    private String firstPublishTime;

    private String lastPublishTime;

    private String totalArticleWordNum;

    private Integer errorArticleNum;

    public String getSitesName() {
        return sitesName;
    }

    public void setSitesName(String sitesName) {
        this.sitesName = sitesName;
    }

    public String getSiteUri() {
        return siteUri;
    }

    public void setSiteUri(String siteUri) {
        this.siteUri = siteUri;
    }

    public String getGenerateTime() {
        return generateTime;
    }

    public void setGenerateTime(String generateTime) {
        this.generateTime = generateTime;
    }

    public String getPlatformName() {
        return platformName;
    }

    public void setPlatformName(String platformName) {
        this.platformName = platformName;
    }

    public String getOrderTypeName() {
        return orderTypeName;
    }

    public void setOrderTypeName(String orderTypeName) {
        this.orderTypeName = orderTypeName;
    }

    public String getArticleNum() {
        return articleNum;
    }

    public void setArticleNum(String articleNum) {
        this.articleNum = articleNum;
    }

    public String getErrorNum() {
        return errorNum;
    }

    public void setErrorNum(String errorNum) {
        this.errorNum = errorNum;
    }

    public String getErrorTypeNum() {
        return errorTypeNum;
    }

    public void setErrorTypeNum(String errorTypeNum) {
        this.errorTypeNum = errorTypeNum;
    }

    public String getType_1_num() {
        return type_1_num;
    }

    public void setType_1_num(String type_1_num) {
        this.type_1_num = type_1_num;
    }

    public String getType_2_num() {
        return type_2_num;
    }

    public void setType_2_num(String type_2_num) {
        this.type_2_num = type_2_num;
    }

    public String getType_3_num() {
        return type_3_num;
    }

    public void setType_3_num(String type_3_num) {
        this.type_3_num = type_3_num;
    }

    public String getType_4_num() {
        return type_4_num;
    }

    public void setType_4_num(String type_4_num) {
        this.type_4_num = type_4_num;
    }

    public String getType_5_num() {
        return type_5_num;
    }

    public void setType_5_num(String type_5_num) {
        this.type_5_num = type_5_num;
    }

    public String getType_6_num() {
        return type_6_num;
    }

    public void setType_6_num(String type_6_num) {
        this.type_6_num = type_6_num;
    }

    public String getType_7_num() {
        return type_7_num;
    }

    public void setType_7_num(String type_7_num) {
        this.type_7_num = type_7_num;
    }

    public String getType_8_num() {
        return type_8_num;
    }

    public void setType_8_num(String type_8_num) {
        this.type_8_num = type_8_num;
    }

    public String getType_9_num() {
        return type_9_num;
    }

    public void setType_9_num(String type_9_num) {
        this.type_9_num = type_9_num;
    }

    public String getType_10_num() {
        return type_10_num;
    }

    public void setType_10_num(String type_10_num) {
        this.type_10_num = type_10_num;
    }

    public String getType_11_num() {
        return type_11_num;
    }

    public void setType_11_num(String type_11_num) {
        this.type_11_num = type_11_num;
    }

    public String getType_12_num() {
        return type_12_num;
    }

    public void setType_12_num(String type_12_num) {
        this.type_12_num = type_12_num;
    }

    public String getFirstPublishTime() {
        return firstPublishTime;
    }

    public void setFirstPublishTime(String firstPublishTime) {
        this.firstPublishTime = firstPublishTime;
    }

    public String getLastPublishTime() {
        return lastPublishTime;
    }

    public void setLastPublishTime(String lastPublishTime) {
        this.lastPublishTime = lastPublishTime;
    }

    public String getTotalArticleWordNum() {
        return totalArticleWordNum;
    }

    public void setTotalArticleWordNum(String totalArticleWordNum) {
        this.totalArticleWordNum = totalArticleWordNum;
    }

    public Integer getErrorArticleNum() {
        return errorArticleNum;
    }

    public void setErrorArticleNum(Integer errorArticleNum) {
        this.errorArticleNum = errorArticleNum;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值