一、单个饼状图生成
1.word模版制作
在word中创建一个饼状图,点击图表,点击“文本选项”,在可选文字标题中填入{{pieChart}}
2.编写接口完整代码
@GetMapping("/exportWord")
public void exportWord(HttpServletResponse response) throws FileNotFoundException {
//存放数据,也就是填充在word里面的值
Map<String, Object> params = new HashMap<>();
params.put("title","测试使用poi-tl模版导出word");
ChartSingleSeriesRenderData pie = Charts
.ofSingleSeries("测试第一个饼图", new String[] { "男", "女" })
.series("countries", new Integer[] { 9826675, 9596961 })
.create();
params.put("pieChart", pie);
// 或模板在静态资源的相对路径
File rootFile = new File((ResourceUtils.getURL("classpath:").getPath()));
File templateFile = new File(rootFile, "/static/templates/exportWord.docx");
//jar包获取不到文件路径`
//URLDecoder.decode() 解决获取中文名称文件路径乱码
String templatePath = URLDecoder.decode(templateFile.getPath());
//生成文件名
String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "_" + System.currentTimeMillis();
// 导出wold
try {
// 导出Word文档为文件
XWPFTemplate template = XWPFTemplate.compile(templatePath,builder.build()).render(params);
// 将导出的Word文件转换为流
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition","attachment;filename=\""+fileName+".docx"+"\"");
// HttpServletResponse response
OutputStream out = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(out);
template.write(bos);
bos.flush();
out.flush();
// 最后不要忘记关闭这些流。
PoitlIOUtils.closeQuietlyMulti(template, bos, out);
} catch (Exception e) {
System.out.println("导出Word文档时出现异常:" + e.getMessage());
}
}
3.导出结果
二、批量生成饼图
1.word模版制作
在word中创建一个饼状图,点击图表,点击“文本选项”,在可选文字标题中同样填入{{pieChart}}
2.编写接口完整代码
@GetMapping("/exportWord")
public void exportWord(HttpServletResponse response) throws FileNotFoundException {
//存放数据,也就是填充在word里面的值
Map<String, Object> params = new HashMap<>();
params.put("title","测试使用poi-tl模版导出word");
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
map.put("a","测试");
map.put("b","米");
map.put("c","201312");
ChartSingleSeriesRenderData pie = Charts
.ofSingleSeries("测试第一个饼图", new String[] { "男", "女" })
.series("countries", new Integer[] { 9826675, 9596961 })
.create();
map.put("pieChart", pie);
list.add(map);
Map<String, Object> map1 = new HashMap<>();
map1.put("a","测试2");
map1.put("b","千米");
map1.put("c","2012312");
ChartSingleSeriesRenderData pie1 = Charts
.ofSingleSeries("测试第二个饼图", new String[] { "上海", "广州" })
.series("countries", new Integer[] { 253, 985 })
.create();
map1.put("pieChart", pie1);
list.add(map1);
params.put("list",list);
// 或模板在静态资源的相对路径
File rootFile = new File((ResourceUtils.getURL("classpath:").getPath()));
File templateFile = new File(rootFile, "/static/templates/exportWord.docx");
//jar包获取不到文件路径`
//URLDecoder.decode() 解决获取中文名称文件路径乱码
String templatePath = URLDecoder.decode(templateFile.getPath());
//生成文件名
String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "_" + System.currentTimeMillis();
// 导出wold
try {
// 导出Word文档为文件
XWPFTemplate template = XWPFTemplate.compile(templatePath,builder.build()).render(params);
// 将导出的Word文件转换为流
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition","attachment;filename=\""+fileName+".docx"+"\"");
// HttpServletResponse response
OutputStream out = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(out);
template.write(bos);
bos.flush();
out.flush();
// 最后不要忘记关闭这些流。
PoitlIOUtils.closeQuietlyMulti(template, bos, out);
} catch (Exception e) {
System.out.println("导出Word文档时出现异常:" + e.getMessage());
}
}