Java使用模板生成word文件

这里springboot项目,模板放在了templates下面,后面要根据模板生成word

1、生成一个word模板,如图:

注:{{code}}的是需填写的参数

下面是生成本地的

pom文件:

<dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jcommon</artifactId>
            <version>1.0.24</version>
        </dependency>
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jfreechart</artifactId>
            <version>1.5.0</version>
        </dependency>

/**
 * @Version 1.0.0
 * @Description
 */
public class WordUtil {


    /**
     * 生成word
     * @param templatePath
     * @param temDir
     * @param fileName
     * @param params
     */
    public static void exportWord(String templatePath, String temDir, String fileName, Map<String,Object> params){
        Assert.notNull(templatePath, "模板路径不能为空");
        Assert.notNull(temDir, "临时文件路径不能为空");
        Assert.notNull(fileName, "导出文件名不能为空");
        Assert.isTrue(fileName.endsWith(".docx"), "word导出请使用docx格式");
        if (!temDir.endsWith("/")) {
            temDir = temDir + File.separator;
        }
        File dir = new File(temDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        try {
            XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params);

            String tmpPath = temDir + fileName;
            FileOutputStream fos = new FileOutputStream(tmpPath);
            doc.write(fos);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

/**
 * @Version 1.0.0
 * @Description
 */
public class WordDemo {

    public static void main(String[] args) {
        Map<String,Object> map = new HashMap<>();
        map.put("username", "张三");
        map.put("company","xx公司" );
        map.put("date","2020-04-20" );
        map.put("dept","IT部" );
        map.put("startTime","2020-04-20 08:00:00" );
        map.put("endTime","2020-04-20 08:00:00" );
        map.put("reason", "外出办公");
        map.put("time","2020-04-22" );

        WordUtil.exportWord("templates/demo.docx","D:/" ,"生成文件.docx" ,map );

    }

}

  2、下面是下载word文件

    /**
     * 导出word形式
     * @param response
     */
    @RequestMapping("/exportWord")
    public void exportWord(HttpServletResponse response){

        Map<String,Object> map = new HashMap<>();
        map.put("username", "张三");
        map.put("company","杭州xx公司" );
        map.put("date","2020-04-20" );
        map.put("dept","IT部" );
        map.put("startTime","2020-04-20 08:00:00" );
        map.put("endTime","2020-04-20 08:00:00" );
        map.put("reason", "外出办公");
        map.put("time","2020-04-22" );
        try {
            response.setContentType("application/msword");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("测试","UTF-8" );
            //String fileName = "测试"
            response.setHeader("Content-disposition","attachment;filename="+fileName+".docx" );
            XWPFDocument doc = WordExportUtil.exportWord07("templates/demo.docx",map);
            doc.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //WordUtil.exportWord("templates/demo.docx","D:/" ,"生成文件.docx" ,map );
    }  

猜你喜欢

转载自www.cnblogs.com/lazyli/p/12769132.html
今日推荐