本例子是一个 maven 项目,要引入 poi 的依赖片段如下:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.13</version>
</dependency>
注意:要想让 poi 操作 word ,须要引入 poi-scratchpad
这个模块。
本例子在开源中国代码托管的地址:
李威小朋友 / poi-word-template - 代码托管 - 开源中国社区
https://git.oschina.net/weimingge/poi-word-template
示例代码:
@Controller
@RequestMapping("/word")
public class DownLoadController {
@RequestMapping(value = "/download",method = RequestMethod.POST)
public void download(HttpServletRequest request,HttpServletResponse response){
String pzjg = request.getParameter("pzjg");
String gdnr = request.getParameter("gdnr");
String jsyy = request.getParameter("jsyy");
String chyf = request.getParameter("chyf");
String xzgf = request.getParameter("xzgf");
String servletContextRealPath = request.getServletContext().getRealPath("");
File templateFile = new File(servletContextRealPath + "/template/template1.doc");
ByteArrayOutputStream ostream = null;
try {
FileInputStream in = new FileInputStream(templateFile);
HWPFDocument hwpfDocument = new HWPFDocument(in);
Map<String,String> params = new HashMap<>();
params.put("$PZJG$",pzjg);
params.put("$GDNR$",gdnr);
params.put("$JSYY$",jsyy);
params.put("$CHYF$",chyf);
params.put("$XZGF$",xzgf);
Range range = hwpfDocument.getRange();
for(Map.Entry<String,String> entry:params.entrySet()){
range.replaceText(entry.getKey(),entry.getValue());
}
response.reset();
response.setContentType("application/x-msdownload");
UUID randomUUID = UUID.randomUUID();
String attachmentName = randomUUID.toString();
response.addHeader("Content-Disposition", "attachment; filename=\""+ attachmentName + ".doc\"");
ostream = new ByteArrayOutputStream();
ServletOutputStream servletOS = response.getOutputStream();
hwpfDocument.write(ostream);
servletOS.write(ostream.toByteArray());
servletOS.flush();
servletOS.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
参考资料:
poi 根据模板调用静态数据
http://download.csdn.net/detail/lxlli/6413637
http://blog.csdn.net/classicbear/article/details/7549035
http://haohaoxuexi.iteye.com/blog/2031335
http://wenku.baidu.com/link?url=N65DYwHXIdl5NjZVgdJgzIgRu5VL2HyNFJ4kGCX0qlTYZTpp0U89817fQ-33898wNa7ms-pPShp2MigCceqxHZx6if2ddI68eMbp-qkBXBS
http://blog.csdn.net/longshengguoji/article/details/39433307
http://meigesir.iteye.com/blog/1539358
http://itindex.net/detail/45161-poi-excel-word
poi 动态调用数据
http://doc.okbase.net/53873039oycg/archive/110664.html
转载自http://blog.csdn.net/lw_power/article/details/49890921