public void dayinHw(HttpServletRequest request, HttpServletResponse response){
String id = request.getParameter("id");
Outdoorads od = this.outdooradsService.queryById(id);
HWPFDocument hdt=null;
try {
//第一步读取项目中word模板文件
String fileDir = new File(request.getRealPath("/")+"/doc/").getCanonicalPath();
FileInputStream in = new FileInputStream(new File(fileDir+ "/户外宣传品申请表.doc"));

// 第二步,创建一个HWPFDocument,对应读取的一个doc文件
hdt = new HWPFDocument(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//第三步替换读取到的word模板内容的指定字段
Range range = hdt.getRange();//读取word文本内容
Map<String, String> map = new HashMap<String, String>();//将需要填充的字段放入map中
if(StringUtil.isNotEmpty(od.getTitle())){
map.put("title",od.getTitle());
}else{
map.put("title","");
}
if(od.getConduct_date() != null){
map.put("conduct_date",DateTool.dateToStr1(od.getConduct_date()));
}else{
map.put("conduct_date","");
}
if(StringUtil.isNotEmpty(od.getConduct_address())){
map.put("conduct_address",od.getConduct_address());
}else{
map.put("conduct_address","");
}

for (Map.Entry<String, String> entry : map.entrySet()) {
if(entry.getValue()!=null){
range.replaceText(entry.getKey(), entry.getValue());//替换文本内容
}else{
range.replaceText(entry.getKey(), "");//替换文本内容
}

}
dayin(request, response, hdt);
}
public void dayin(HttpServletRequest request, HttpServletResponse response, HWPFDocument hdt){
//*********************wangyaStart*********************************
try{
//路径
String destPathName = request.getRealPath("/") + "//";
File dirPath = new File(destPathName);
if (!dirPath.exists()) {
dirPath.mkdirs();
}
// 生成文件名
String curDate = String.valueOf(new java.util.Date().getTime());
String Filename = dirPath + "//" +"户外宣传品申请表.doc";
//写入数据
FileOutputStream fout = new FileOutputStream(Filename);
hdt.write(fout);
fout.close();

BufferedInputStream bis = null;
BufferedOutputStream bos = null;
//输出word内容文件流,提供下载

String fileNameTemp = URLEncoder.encode("户外宣传品申请表.doc", "utf-8");
request.setCharacterEncoding("GBK");
response.reset();
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=\""+ fileNameTemp + "\"");
OutputStream outs = response.getOutputStream();

bis = new BufferedInputStream(new FileInputStream(Filename));
bos = new BufferedOutputStream(outs);

byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.flush();
bos.close();
}catch(Exception e){
e.printStackTrace();
}
}

java导出word的更多相关文章

  1. java导出word的6种方式(复制来的文章)

    来自: http://www.cnblogs.com/lcngu/p/5247179.html 最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前 ...

  2. java导出word文件

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

  3. [转载]java导出word的5种方式

    在网上找了好多天将数据库中信息导出到word中的解决方案,现在将这几天的总结分享一下.总的来说,java导出word大致有5种解决方案: 1:Jacob是Java-COM Bridge的缩写,它在Ja ...

  4. [原创]java导出word的5种方式

    在网上找了好多天将数据库中信息导出到word中的解决方案,现在将这几天的总结分享一下.总的来说,java导出word大致有5种解决方案: 1:Jacob是Java-COM Bridge的缩写,它在Ja ...

  5. java导出word的6种方式(转发)

    来自: http://www.cnblogs.com/lcngu/p/5247179.html 最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前 ...

  6. java导出word直接下载

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

  7. java 导出word 并下载

    记录一下导出操作 源码: /************ * 导出word 并下载 * @param id 房号记录编号 * ***********************/ @RequestMappin ...

  8. Freemarker + xml 实现Java导出word

    前言 最近做了一个调查问卷导出的功能,需求是将维护的题目,答案,导出成word,参考了几种方案之后,选择功能强大的freemarker+固定格式之后的wordxml实现导出功能.导出word的代码是可 ...

  9. Java导出Word利用freemarker(含图片)

    制作Word模版 建议使用高版本的office做,尽量不要用WPS做,生成xml会出现乱码 编码要统一,推荐UTF-8 建好模板,将模板另存为xml格式,建议原来模板不要删,xml的如果后期打不开,还 ...

  10. java导出word(带图片)

    public class CreateWordDemo { public void createDocContext(String file) throws DocumentException,IOE ...

随机推荐

  1. 使用js dom和jquery分别实现简单增删改

    <html><head> <meta http-equiv="Content-Type" content="text/html; chars ...

  2. com.mysql.jdbc.Driver和com.mysql.cj.jdbc.Driver的区别

    概述:com.mysql.jdbc.Driver是mysql-connector-java 5中的,而com.mysql.cj.jdbc.Driver是mysql-connector-java 6中的 ...

  3. git一键部署代码到远程服务器(linux)(采坑总结)

    原来一直使用FileZilla来代码部署,去年使用git,代码版本管理,真TM好用,一起回顾下历程! 一. 代码部署方式及思路: 1. 使用FTP/SFTP工具,上传代码 2. git人工部署.1. ...

  4. 2015四川省acm B题

    Carries frog has n integers a1,a2,-,an, and she wants to add them pairwise. Unfortunately, frog is s ...

  5. UVA 1626 Brackets sequence 区间DP

    题意:给定一个括号序列,将它变成匹配的括号序列,可能多种答案任意输出一组即可.注意:输入可能是空串. 思路:D[i][j]表示区间[i, j]至少需要匹配的括号数,转移方程D[i][j] = min( ...

  6. 使用simhash库来进行网页去重

    首先感谢作者yanyiwu贡献的开源项目https://github.com/yanyiwu/simhash.在做项 目过程中,翻了一遍<这就是搜索引擎  核心技术详解>这本书的查重算法, ...

  7. R语言︱XGBoost极端梯度上升以及forecastxgb(预测)+xgboost(回归)双案例解读

    XGBoost不仅仅可以用来做分类还可以做时间序列方面的预测,而且已经有人做的很好,可以见最后的案例. 应用一:XGBoost用来做预测 ------------------------------- ...

  8. Python Numpy包安装

    1,下载python 下载地址: https://www.python.org/downloads/windows/ 2,配置python环境变量 在电脑的系统属性的系统变量path中添加python ...

  9. strstr()函数实现

    /* 函数要求:写一个函数模拟strstr()函数,设计中不得使用其他库函数. 函数原型:const char *strstr(const char *str1,const char *str2); ...

  10. 你的变量究竟存储在什么地方 && 全局内存

    我相信大家都有过这样的经历,在面试过程中,考官通常会给你一道题目,然后问你某个变量存储在什么地方,在内存中是如何存储的等等一系列问题.不仅仅是在面试中,学校里面的考试也会碰到同样的问题.  如果你还不 ...