POI之根据模板导出word-yellowcong

根据模版导出word这个东西在实际开发中,是很常用的,我今天遇到坑了,就是如何根据word模版生成word。网上的代码好多都是坑爹,不是缺环境,就是却方法,所以我自己做了一个小例子。如果希望有图片替换的教程,大家可以给我留言

环境搭建

<!-- excel -->
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>

<!-- word -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>3.17</version>
</dependency>

<!-- xlsx -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
<!-- xlsx  依赖这个包 -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>3.17</version>
</dependency>

帐票模版

这里写图片描述

实现代码

package com.yellowcong.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;

/**
 * @version $Id$
 */
public class POITest {
    public static void main(String[] args) throws Exception, IOException {

        String file = "C:/Users/zhangrw/Desktop/test/指名業者指名理由書.doc";

        //获取到文档
        HWPFDocument doc = new HWPFDocument(new FileInputStream(file));
        Range range = doc.getRange();
        //查看段落数量
        int paraNum = range.numParagraphs();
        for(int i=0;i<paraNum;i++){
            //获取段落
            Paragraph paragraph =range.getParagraph(i);
            //当存在替换符号的情况

             if(paragraph.text().indexOf("$")>-1){
                paragraph.replaceText("$共通.契約名$", "yellowcong");
                paragraph.replaceText("$共通.施工場所$", "yellowcong");
                paragraph.replaceText("$案件.工事担当課$", "NB");
            }
        }

        //保存doc文件
        FileOutputStream out = new FileOutputStream(new File("C:/Users/zhangrw/Desktop/test/demo2.doc"));
        doc.write(out);
        out.close();
    }


}

优化后的代码

只需要提供输入、输出、替换的代码三个参数,就可以完成张票的生成

package com.yellowcong.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;

/**
 *
 *
 * @version $Id$
 */
public class POITest {
    /**
     * @param args
     * @throws Exception
     * @throws IOException
     */
    public static void main(String[] args) throws Exception, IOException {

        String inFile = "C:/Users/zhangrw/Desktop/test/指名業者指名理由書.doc";
        String outFile = "C:/Users/zhangrw/Desktop/test/demo3.doc";

        //需要替换的模版代码
        Map<String,String> map = new HashMap<String,String>();
        map.put("$共通.契約名$", "yellowcong");
        map.put("$共通.施工場所$", "yellowcong");
        map.put("$案件.工事担当課$", "NB");

        //获取修改模版后的代码
        HWPFDocument doc = createTemplateDoc(inFile, map);

        //保存doc文件
        saveDoc(doc,outFile);
    }
    /**
     * 输出文件
     * @param doc
     * @param outPath
     */
    private static void saveDoc(HWPFDocument doc,String outPath){
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(new File(outPath));
            doc.write(out);
        } catch (Exception e) {
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        }finally{
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    // TODO 自動生成された catch ブロック
                    e.printStackTrace();
                }
            }
        }
    }
    /**
       根据代码的code,格式化输出数据
     * @param file
     * @param map
     * @return
     * @throws IOException
     * @throws FileNotFoundException
     */
    private static HWPFDocument createTemplateDoc(String file, Map<String, String> map) throws IOException,
            FileNotFoundException {
        //获取到文档
        HWPFDocument doc = new HWPFDocument(new FileInputStream(file));
        Range range = doc.getRange();
        //查看段落数量
        int paraNum = range.numParagraphs();
        for(int i=0;i<paraNum;i++){
            //获取段落
            Paragraph paragraph =range.getParagraph(i);
            if(paragraph.text().indexOf("$")>-1){
                for(Map.Entry<String, String> entry:map.entrySet()){
                    paragraph.replaceText(entry.getKey(), entry.getValue());
                }
            }
        }
        return doc;
    }


}

替换后效果

这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狂飙的yellowcong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值