使用的POI接口
传入一个Map,然后根据key:value的形式填充word文档。
sql语句查到的字段用了别名做修饰。如 select uname 供应商名称 from table
@ApiOperation(value = "导出word文档")
@ApiImplicitParams({@ApiImplicitParam(name = "response",value = "返回流"),
@ApiImplicitParam(name = "map",value = "要打印的数据,key:value")})
public static void downWord(HttpServletResponse response,Map<String,Object> map){
OutputStream out = null;
XWPFDocument docx = new XWPFDocument();
try {
//循环Map,以key:value的形式写入word文档中
for (String key:map.keySet()
) {
//创建一个段落
XWPFParagraph para = docx.createParagraph();
//创建一个文本
XWPFRun run = para.createRun();
//加粗
run.setBold(true);
//写入
run.setText(key+":"+map.get(key));
}
out = response.getOutputStream();
response.addHeader("Content-disposition","attachment;filename="
+java.net.URLEncoder.encode("文件名称.doc","utf-8));
docx.write(out);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
效果图片: