- 首先上测试代码,用的SpringMVC。
/** * 测试导出word文档 * * @param request * @return * @throws CustomException * @author yuanjin */ @RequestMapping(value = "/exportWord", method = {RequestMethod.GET}) public void exportWord(HttpServletResponse response, HttpServletRequest request) throws IOException, XDocReportException { //获取Word模板,模板存放路径在项目的resources目录下 InputStream ins = this.getClass().getResourceAsStream("/template/word/测试模板.docx"); //注册xdocreport实例并加载FreeMarker模板引擎 IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins, TemplateEngineKind.Freemarker); //创建xdocreport上下文对象 IContext context = report.createContext(); //创建要替换的文本变量 context.put("city", "深圳市"); context.put("startDate", "2022-01-17"); context.put("endDate", "2022-01-26"); context.put("num", 96689); context.put("fee", 1056.378); List<Goods> goodsList = new ArrayList<>(); Goods goods1 = new Goods(); goods1.setSerialNo(1); goods1.setType("衣服"); goods1.setQuantity(98989); goods1.setSaleAmount(new BigDecimal(5692.56)); goodsList.add(goods1); Goods goods2 = new Goods(); goods2.setSerialNo(2); goods2.setType("鞋子"); goods2.setQuantity(89562); goods2.setSaleAmount(new BigDecimal(123.56)); goodsList.add(goods2); Goods goods3 = new Goods(); goods3.setSerialNo(3); goods3.setType("手机"); goods3.setQuantity(72465); goods3.setSaleAmount(new BigDecimal(68442.666)); goodsList.add(goods3); Goods goods4 = new Goods(); goods4.setSerialNo(4); goods4.setType("电脑"); goods4.setQuantity(66895); goods4.setSaleAmount(new BigDecimal(7342.353)); goodsList.add(goods4); Goods goods5 = new Goods(); goods5.setSerialNo(5); goods5.setType("食品"); goods5.setQuantity(55231); goods5.setSaleAmount(new BigDecimal(99.99)); goodsList.add(goods5); context.put("goods", goodsList); //创建字段元数据 FieldsMetadata fm = report.createFieldsMetadata(); //Word模板中的表格数据对应的集合类型 fm.load("goods", Goods.class, true); //元数据中加入图片 fm.addFieldAsImage("img1"); fm.addFieldAsImage("img2"); //获取图片 // InputStream img1 = this.getClass().getResourceAsStream("/template/word/img/11.png"); // InputStream img2 = this.getClass().getResourceAsStream("/template/word/img/22.png"); PieChart chart = new PieChartBuilder().width(800).height(620) .title("销售饼图").build(); //给饼图设置对应的值 chart.addSeries("臭美毁肤", 589); chart.addSeries("女装", 651); chart.addSeries("手机", 866); chart.addSeries("家居家装", 783); chart.addSeries("食物饮品", 405); //生成饼图 ByteArrayOutputStream baos1 = new ByteArrayOutputStream(); BitmapEncoder.saveBitmap(chart, baos1, BitmapEncoder.BitmapFormat.PNG); //把图片添加到上下文对象 context.put("img1", new ByteArrayImageProvider(baos1.toByteArray())); XYChart chartXy = new XYChartBuilder().width(800).height(600).xAxisTitle("X").yAxisTitle("Y").build(); // Customize Chart chartXy.getStyler().setLegendPosition(Styler.LegendPosition.InsideNE); // Series chartXy.addSeries("a", new double[] { 0, 3, 5, 7, 9 }, new double[] { -3, 5, 9, 6, 5 }).setMarker(SeriesMarkers.NONE); chartXy.addSeries("b", new double[] { 0, 2, 4, 6, 9 }, new double[] { -1, 6, 4, 0, 4 }).setMarker(SeriesMarkers.NONE); chartXy.addSeries("c", new double[] { 0, 1, 3, 8, 9 }, new double[] { -2, -1, 1, 0, 1 }).setMarker(SeriesMarkers.NONE); ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); BitmapEncoder.saveBitmap(chartXy, baos2, BitmapEncoder.BitmapFormat.PNG); context.put("img2", new ByteArrayImageProvider(baos2.toByteArray())); //浏览器端下载 response.setCharacterEncoding("utf-8"); response.setContentType("application/msword"); String fileName = "商品销售报表.docx"; response.setHeader("Content-Disposition", "attachment;filename=" .concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8")))); report.process(context, response.getOutputStream()); baos1.close(); baos2.close(); }View Code
- 导入依赖
版本号:
<xdocreport.version>2.0.2</xdocreport.version>
<xchart.version>3.5.4</xchart.version>
gav:
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.core</artifactId>
<version>${xdocreport.version}</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.document</artifactId>
<version>${xdocreport.version}</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.template</artifactId>
<version>${xdocreport.version}</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.document.docx</artifactId>
<version>${xdocreport.version}</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId>
<version>${xdocreport.version}</version>
</dependency>
<dependency>
<groupId>org.knowm.xchart</groupId>
<artifactId>xchart</artifactId>
<version>${xchart.version}</version>
</dependency>
注:另外需要POI依赖。
- 制作模板
模板图片随便找的,但是需要手动插入到word文档,然后还需要插入书签,这样后台生成的统计图表才会覆盖上去;
word文档模板、模板图片上传路径:
最终实现效果:
标签:fr,Goods,word,xdocreport,opensagres,文档,context,new,Java From: https://www.cnblogs.com/jin9yuan/p/17167322.html