富文本框输出到doc文档

doc文件格式调整;

doc文档另存为mht格式

mht格式修改后缀名为ftl

填充数据;

返回;

 

String mhtName = "wordTemplate/manuscript-mht.ftl";
fileService.generateDocByMht(fileName, mhtName, dataMap, response);
public void generateDocByMht(String fileName, String ftlName, Map<String, Object> dataMap, HttpServletResponse response) {
    InputStream inputStream = null;
    ServletOutputStream out = null;
    try {
        String path = EPlatform.Windows.equals(OSinfo.getOSname()) ? pathWindow : pathLinux;
        inputStream = WordUtil.generateWordForMht(dataMap, ftlName, path, fileName);
        if (inputStream != null) {
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/msword");
            response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
            out = response.getOutputStream();
            byte[] buffer = new byte[1024];
            int bytesToRead = -1;
            while ((bytesToRead = inputStream.read(buffer)) != -1) {
                out.write(buffer, 0, bytesToRead);
            }
            out.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

public static InputStream generateWordForMht(Map<String, Object> dataMap, String ftlName, String path, String fileName) {
    ByteArrayInputStream inputStream = null;
    FileOutputStream fileOutputStream = null;
    try {
        // 创建配置实例
        Configuration configuration = new Configuration();
        // 设置编码
        configuration.setDefaultEncoding("UTF-8");
        configuration.setClassicCompatible(true);
        // ftl模板文件
        configuration.setClassLoaderForTemplateLoading(WordUtil.class.getClassLoader(),"/");
        // configuration.setClassForTemplateLoading(WordUtil.class, "ftl");
        //获取模板
        Template template = configuration.getTemplate(ftlName);
        // 输出文件
        File outFile = new File(path + File.separator + fileName);
        // 如果输出目标文件夹不存在,则创建
        if (!outFile.getParentFile().exists()) {
            outFile.getParentFile().mkdirs();
        }
        Writer out = new StringWriter();
        template.process(dataMap, out);
        // 关闭流
        out.flush();
        out.close();
        String result = out.toString();
        //转换格式
        String changeResult = DataUtil.string2ASCII(result);
        byte [] b = changeResult.getBytes("utf-8");
        inputStream = new ByteArrayInputStream(b);
        //将inputStream写到outStream中
        fileOutputStream = new FileOutputStream(outFile);
        inputStreamToOutputStream(inputStream, fileOutputStream);
        fileOutputStream.flush();
        return inputStream;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值