FreeMaker自动生成word文档

2020-07-07  本文已影响0人  Allenlll

一、下载FreeMaker

地址:https://freemarker.apache.org/freemarkerdownload.html

二、制作文档模板

 <w:t>${content1}</w:t></w:r></w:p>
 <w:binData w:name="wordml://02000001.jpg xml:space="preserve">${image1}</w:binData>

#list

三、构造替换数据

 Map<String, String> DataSources = new HashMap();
   public static String[] getImg(List<File> paths) throws Exception {
        int k = paths.size();
        String[] imgPBASE64 = new String[paths.size()];

        for(int i = 0; i < k; ++i) {
            File fileImage = new File(((File)paths.get(i)).getAbsolutePath());
            InputStream inputStream = new FileInputStream(fileImage);
            byte[] data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
            imgPBASE64[i] = new String(Base64.encodeBase64(data));
        }

        return imgPBASE64;
    }

FreeMaker加载模板生成Word文档

    public static void getResult(Map dataMap, String templateName, String filePath, String fileName) {
        try {
            Configuration configuration = new Configuration();
            configuration.setDefaultEncoding("UTF-8");
            configuration.setClassForTemplateLoading(OrderProByTime.class, "/bean");
            Template template = configuration.getTemplate(templateName);
            File outFile = new File(filePath + File.separator + fileName);
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }

            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
            template.process(dataMap, out);
            out.flush();
            out.close();
        } catch (Exception var9) {
            var9.printStackTrace();
        }

    }

读写Excel

Apache POI

上一篇下一篇

猜你喜欢

热点阅读