import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URLEncoder;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import freemarker.template.Configuration;
import freemarker.template.Template;
/**
* @Desc:word操作工具类
*
* */
public class WordUtil {
private static Logger log = Logger.getLogger(WordUtil.class);
/**
* @Desc:生成word文件
* @paramdataMap word中需要展示的动态数据,用map集合来保存
* @paramtemplateName word模板名称,例如:test.ftl
* @paramfilePath文件生成的目标路径,例如:D:/wordFile/
* @paramfileName生成的文件名称,例如:test.doc
* */
public static void createWord(Map<String, Object>dataMap,String templateName,String filePath,String fileName){
try {
//创建配置实例
Configuration configuration = new Configuration();
//设置编码
configuration.setDefaultEncoding("UTF-8");
//ftl模板文件
File file = new File(filePath);
configuration.setDirectoryForTemplateLoading(file);
//获取模板
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 e) {
log.error("生成 word文档(WordUtil)出错:【msg:"+e.getMessage()+"】 ,文件名:" + fileName);
e.printStackTrace();
}
}
/** 文件下载
* @param path 文件路径全路径,包含文件名
* @param response
* @return
* */
public static HttpServletResponse downFile(String path, HttpServletResponse response) {
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
String fileName = URLEncoder.encode(filename,"UTF-8");
if(fileName.length()>150){ //解决IE 6.0 bug
fileName=new String(filename.getBytes("GBK"),"ISO-8859-1"); }
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Content-Length", "" + file.length());
OutputStream outs = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
outs.write(buffer);
outs.flush();
outs.close();
file.delete();
}catch (IOException e) {
log.error("下载文档(WordUtil)出错:【msg:"+e.getMessage()+"】 "); e.printStackTrace(); }
return response;
}
}

WordUtil java导出word工具类的更多相关文章

  1. Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

    Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...

  2. java导出excel工具类

    java导出excel须要使用HSSFWorkbook这个类,须要导入poi-3.6-20091214.jar 工具类调用例如以下: package com.qlwb.business.util; i ...

  3. Java导出压缩包工具类

    /** * @Title: exportZip * @Description:TODO(导出建压缩文件) * @param delAdviceinfo 为项目实体类 * @date 2018年4月4日 ...

  4. java导出word直接下载

    导出word工具类 package util; import java.io.IOException; import java.io.Writer; import java.util.Map; imp ...

  5. java导出word文件

    java导出word文件 test5.ftl文件生存方法, 第一步:用word新建test5.doc,填写完整模板,将需导出数据用${}代替 第二步:将test5.doc另存为test5.xml 第三 ...

  6. 导入导出Excel工具类ExcelUtil

    前言 前段时间做的分布式集成平台项目中,许多模块都用到了导入导出Excel的功能,于是决定封装一个ExcelUtil类,专门用来处理Excel的导入和导出 本项目的持久化层用的是JPA(底层用hibe ...

  7. javaEE开发之导出excel工具类

    web开发中,一个系统的普通需求也包含导出excel,一般採用POI做统计报表导出excel. 导出excel工具类: import java.io.FileOutputStream; import ...

  8. java下载文件工具类

    java下载文件工具类 package com.skjd.util; import java.io.BufferedInputStream; import java.io.BufferedOutput ...

  9. HttpTool.java(在java tool util工具类中已存在) 暂保留

    HttpTool.java 该类为java源生态的http 请求工具,不依赖第三方jar包 ,即插即用. package kingtool; import java.io.BufferedReader ...

随机推荐

  1. java基础学习总结——java读取properties文件总结

    摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...

  2. Oracle实战笔记(第六天)之PL/SQL基础

    一.PL/SQL介绍 1.概念 PL/SQL也是一种程序语言,叫做过程化SQL语言(Procedural Language/SQL).PL/SQL是Oracle数据库对SQL语句的扩展.在普通SQL语 ...

  3. Python简单爬虫Requests

    首先添加库 附配环境变量:安装环境变量 cmd==> 输入指令: path=%path%;C:\Python(Python安装路径) 回车 python2.7版本可能没有pip的话可以先到www ...

  4. 【转】shell学习笔记(四)——条件测试

    1 test 条件检测 当我要检测系统上面某些文件或者是相关的属性时,利用 test 这个命令来工作真是好用得不得了, 举例来说,我要检查 /home/oracle/zy是否存在时,使用: test ...

  5. 不干胶打印机 www.bgjdyj.com

    不干胶打印机如何保养 不干胶打印机专卖网根据多年的维修经验总结了以下几种保养不干胶打印机的方法: 1.不干胶打印机打印机标签纸不能搁置太长时间,第一容易起静电.第二容易起灰尘2.不干胶打印机的打印头最 ...

  6. 将Flask应用程序部署在nginx,tornado的简单方法

    来自:http://www.xuebuyuan.com/618750.html 在网上搜索了一下部署flask应用的方法,大部分是用wsgi部署在nginx上面,部署了很久,都没有成功,可能是我领悟能 ...

  7. Java设计模式——模板方法模式

    转载自:https://www.cnblogs.com/zplogo/p/6428593.html 用抽象基类定义算法框架 RefreshBeverage package com.pattern.te ...

  8. 济南清北学堂游记 Day 3.

    为什么你们都说下午题简单?我反而觉得下午题难...因为上午暴力很好写啊... 第一题惊了是一道博弈论,冷静推理一大波推出了正解,就变成了结论题. 可是结论题不都是水题吗? T2是一道关于异或的题,照例 ...

  9. Vs Code JavaScript开发插件推荐

    1 vscode-icons 使用效果 安装好后如何设置? Ctrl+Shift+P 然后输入theme 2 document this 如何使用? 在方法或者要注释的地方上 按 快捷键 Ctrl + ...

  10. Python之MYsql、数据库

    一.数据库的操作及安装 1.安装  MySQL Community Server 5.7.16   ----   版本 2.初始化   mysqld --inisialize-insecure 3.启 ...