广州网站建设咨询电话,好用的网站链接,如何自己做音频网站,网页设计与制作微课教程第4版李敏往期系列传送门#xff1a;
Poi实现根据word模板导出-文本段落篇
#xff08;需要完整代码的直接看最后位置#xff01;#xff01;#xff01;#xff09;
前言#xff1a;
补充Word中图表的知识#xff1a;
每个图表在word中都有一个内置的Excel#xff0c;用于…往期系列传送门
Poi实现根据word模板导出-文本段落篇
需要完整代码的直接看最后位置
前言
补充Word中图表的知识
每个图表在word中都有一个内置的Excel用于操作数据。 内置Excel有类别、系列、值三个概念 poi可以获取word中的图表对象通过这个图表对象来操作Excel的系列、类别、值。这样是不是思路很清晰了直接看代码 public void exportWord() throws Exception {//获取word模板InputStream is WordController.class.getResourceAsStream(/template/wordChartTemplate.docx);try {ZipSecureFile.setMinInflateRatio(-1.0d);// 获取docx解析对象XWPFDocument document new XWPFDocument(is);// 解析替换第一个图表数据,根据具体业务封装数据ListNumber[] singleBarValues new ArrayList();// 第一个系列的值singleBarValues.add(new Number[]{3232, 5222, 2424, 2346, 3123});// 第二个系列的值singleBarValues.add(new Number[]{42.2, 24.41, 31.73, 74.83, 53.28});// x轴的值String[] xValues new String[]{山东省, 河南省, 北京市, 天津市, 陕西省};changeChart1(document, singleBarValues, xValues);// 输出新文件FileOutputStream fos new FileOutputStream(D:\\test\\test.docx);document.write(fos);document.close();fos.close();} catch (Exception e) {e.printStackTrace();}}private static XWPFChart changeChart1(XWPFDocument document, ListNumber[] singleBarValues, String[] xValues) {//获取word中所有图表对象ListXWPFChart charts document.getCharts();//获取第一个单系列柱状图XWPFChart docChart charts.get(0);//系列信息String[] seriesNames {出生人口数,出生率};//分类信息String[] cats xValues;// 业务数据singleBarValues.add(singleBarValues.get(0));singleBarValues.add(singleBarValues.get(1));//获取图表数据对象XDDFChartData chartData null;//word图表均对应一个内置的excel用于保存图表对应的数据//excel中 第一列第二行开始的数据为分类信息//CellRangeAddress(1, categories.size(), 0, 0) 四个参数依次为 起始行 截止行 起始列 截止列。//excel中分类信息的范围String catDataRange docChart.formatRange(new CellRangeAddress(1, cats.length, 0, 0));//根据分类信息的范围创建分类信息的数据源XDDFDataSource? catDataSource XDDFDataSourcesFactory.fromArray(cats, catDataRange, 0);//更新数据for (int i 0; i seriesNames.length; i) {chartData docChart.getChartSeries().get(i);//excel中各系列对应的数据的范围String valDataRange docChart.formatRange(new CellRangeAddress(1, cats.length, i 1, i 1));//根据数据的范围创建值的数据源Number[] val singleBarValues.get(i);XDDFNumericalDataSourceNumber valDataSource XDDFDataSourcesFactory.fromArray(val, valDataRange, i 1);//获取图表系列的数据对象XDDFChartData.Series series chartData.getSeries(0);//替换系列数据对象中的分类和值series.replaceData(catDataSource, valDataSource);//修改系列数据对象中的标题
// CellReference cellReference docChart.setSheetTitle(seriesNames[i], 1);
// series.setTitle(seriesNames[i], cellReference);//更新图表数据对象docChart.plot(chartData);}//图表整体的标题 传空值则不替换标题
// if (!Strings.isNullOrEmpty(title)) {
// docChart.setTitleText(title);
// docChart.setTitleOverlay(false);
// }return docChart;}
导出效果 重点重点重点
看下面这个图表用上述方法能成功导出吗 像这种x轴带有分类的通过上述方法已经不行了原因是在用
chartData docChart.getChartSeries().get(i);
这个方法获取系列对象时报空指针异常。可能是poi不支持这个格式的x轴。
那我们只能换个角度来处理了之前说过Word中每个图表都是一个内置Excel控制那Poi操作Excel我可太会了不熟悉的可以看之前Poi处理Excel的文章。
果然我们可以通过图表对象拿到XSSFWorkbook
//获取word中所有图表对象
ListXWPFChart charts doc.getCharts();
//获取第一个单系列柱状图
XWPFChart singleBarChar charts.get(1);XSSFWorkbook workbook singleBarChar.getWorkbook();
XSSFSheet sheet workbook.getSheetAt(0);
int lastRowNum sheet.getLastRowNum();
// 更新内置excel数据
for (int i 1; i lastRowNum; i) {XSSFRow row sheet.getRow(i);XSSFCell cell row.getCell(2);cell.setCellValue(Double.parseDouble(n1[i - 1].toString()));XSSFCell cell1 row.getCell(3);cell1.setCellValue(Double.parseDouble(n2[i - 1].toString()));
}
处理完后发现导出的Word虽然内置的Excel数据替换成我们的数据了页面显示的还是之前模板数据必须点击编辑后页面数据才显示Excel中的值。
现在问题成了怎么刷新内置Excel数据简单图表通过docChart.plot(chartData);方法直接刷新现在拿不到了怎么办
上一篇Poi实现根据word模板导出-文本段落篇说到poi是将word解析成xml操作的那Word页面显示的图表也应该是xml来生成的我们只需把对应标签数据也改成我们的数据那页面数据和内置Excel数据不就保持一致了。
通过Debug看下一图表对象 可以发现果然有标签是控制值显示的下面我们只需按照标签顺序找到位置替换值就可以了。
// 内置excel数据不会更新到页面需要刷新页面数据
CTChart ctChart singleBarChar.getCTChart();
CTPlotArea plotArea ctChart.getPlotArea();
// 第一列数据
CTBarChart barChartArray plotArea.getBarChartArray(0);
ListCTBarSer serList barChartArray.getSerList();
CTBarSer ctBarSer serList.get(0);
ListCTNumVal ptList ctBarSer.getVal().getNumRef().getNumCache().getPtList();
for (int i 0; i ptList.size(); i) {ptList.get(i).setV(String.valueOf(n1[i]));
}
// 第二列数据
CTLineChart lineChartArray plotArea.getLineChartArray(0);
ListCTLineSer lineSers lineChartArray.getSerList();
CTLineSer ctLineSer lineSers.get(0);
ListCTNumVal ptList1 ctLineSer.getVal().getNumRef().getNumCache().getPtList();
for (int i 0; i ptList1.size(); i) {ptList1.get(i).setV(String.valueOf(n2[i]));
}
导出效果 完整代码
package com.javacoding.controller;import cn.hutool.core.util.RandomUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xddf.usermodel.chart.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.drawingml.x2006.chart.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.io.*;
import java.util.*;RestController
RequestMapping(/word)
public class WordController {RequestMapping(/export)public void exportWord() throws Exception {//获取word模板InputStream is WordController.class.getResourceAsStream(/template/wordChartTemplate.docx);try {ZipSecureFile.setMinInflateRatio(-1.0d);// 获取docx解析对象XWPFDocument document new XWPFDocument(is);// 解析替换文本段落对象// 替换word模板中占位符数据根据业务自行封装MapString, String params new HashMap();params.put(area1, 山东省);params.put(area2, 河南省);params.put(area3, 北京市);params.put(area4, 天津市);params.put(area5, 陕西省);params.put(areaRate1, 42.15);params.put(areaRate2, 22.35);params.put(areaRate3, 42.35);params.put(areaRate4, 23.11);params.put(areaRate5, 15.34);changeText(document, params);// 解析替换第一个图表数据,根据具体业务封装数据ListNumber[] singleBarValues new ArrayList();// 第一个系列的值singleBarValues.add(new Number[]{3232, 5222, 2424, 2346, 3123});// 第二个系列的值singleBarValues.add(new Number[]{42.2, 24.41, 31.73, 74.83, 53.28});// x轴的值String[] xValues new String[]{山东省, 河南省, 北京市, 天津市, 陕西省};changeChart1(document, singleBarValues, xValues);// 解析替换第二个图表数据,根据具体业务封装数据ListNumber[] singleBarValues2 new ArrayList();Number[] n1 new Number[32];Number[] n2 new Number[32];for (int i 0; i 32; i) {n1[i] RandomUtil.randomInt(1000000);n2[i] RandomUtil.randomDouble(1);}singleBarValues2.add(n1);singleBarValues2.add(n2);changeChart2(document, singleBarValues2);// 输出新文件FileOutputStream fos new FileOutputStream(D:\\test\\test.docx);document.write(fos);fos.close();} catch (Exception e) {e.printStackTrace();}}private static void changeChart2(XWPFDocument doc, ListNumber[] singleBarValues2) throws IOException, InvalidFormatException {// 业务数据Number[] n1 singleBarValues2.get(0);Number[] n2 singleBarValues2.get(1);//获取word中所有图表对象ListXWPFChart charts doc.getCharts();//获取第一个单系列柱状图XWPFChart singleBarChar charts.get(1);XSSFWorkbook workbook singleBarChar.getWorkbook();XSSFSheet sheet workbook.getSheetAt(0);int lastRowNum sheet.getLastRowNum();// 更新内置excel数据for (int i 1; i lastRowNum; i) {XSSFRow row sheet.getRow(i);XSSFCell cell row.getCell(2);cell.setCellValue(Double.parseDouble(n1[i - 1].toString()));XSSFCell cell1 row.getCell(3);cell1.setCellValue(Double.parseDouble(n2[i - 1].toString()));}// 内置excel数据不会更新到页面需要刷新页面数据CTChart ctChart singleBarChar.getCTChart();CTPlotArea plotArea ctChart.getPlotArea();// 第一列数据CTBarChart barChartArray plotArea.getBarChartArray(0);ListCTBarSer serList barChartArray.getSerList();CTBarSer ctBarSer serList.get(0);ListCTNumVal ptList ctBarSer.getVal().getNumRef().getNumCache().getPtList();for (int i 0; i ptList.size(); i) {ptList.get(i).setV(String.valueOf(n1[i]));}// 第二列数据CTLineChart lineChartArray plotArea.getLineChartArray(0);ListCTLineSer lineSers lineChartArray.getSerList();CTLineSer ctLineSer lineSers.get(0);ListCTNumVal ptList1 ctLineSer.getVal().getNumRef().getNumCache().getPtList();for (int i 0; i ptList1.size(); i) {ptList1.get(i).setV(String.valueOf(n2[i]));}}private static XWPFChart changeChart1(XWPFDocument document, ListNumber[] singleBarValues, String[] xValues) {//获取word中所有图表对象ListXWPFChart charts document.getCharts();//获取第一个单系列柱状图XWPFChart docChart charts.get(0);//系列信息String[] seriesNames {出生人口数,出生率};//分类信息String[] cats xValues;// 业务数据singleBarValues.add(singleBarValues.get(0));singleBarValues.add(singleBarValues.get(1));//获取图表数据对象XDDFChartData chartData null;//word图表均对应一个内置的excel用于保存图表对应的数据//excel中 第一列第二行开始的数据为分类信息//CellRangeAddress(1, categories.size(), 0, 0) 四个参数依次为 起始行 截止行 起始列 截止列。//excel中分类信息的范围String catDataRange docChart.formatRange(new CellRangeAddress(1, cats.length, 0, 0));//根据分类信息的范围创建分类信息的数据源XDDFDataSource? catDataSource XDDFDataSourcesFactory.fromArray(cats, catDataRange, 0);//更新数据for (int i 0; i seriesNames.length; i) {chartData docChart.getChartSeries().get(i);//excel中各系列对应的数据的范围String valDataRange docChart.formatRange(new CellRangeAddress(1, cats.length, i 1, i 1));//根据数据的范围创建值的数据源Number[] val singleBarValues.get(i);XDDFNumericalDataSourceNumber valDataSource XDDFDataSourcesFactory.fromArray(val, valDataRange, i 1);//获取图表系列的数据对象XDDFChartData.Series series chartData.getSeries(0);//替换系列数据对象中的分类和值series.replaceData(catDataSource, valDataSource);//修改系列数据对象中的标题
// CellReference cellReference docChart.setSheetTitle(seriesNames[i], 1);
// series.setTitle(seriesNames[i], cellReference);//更新图表数据对象docChart.plot(chartData);}//图表整体的标题 传空值则不替换标题
// if (!Strings.isNullOrEmpty(title)) {
// docChart.setTitleText(title);
// docChart.setTitleOverlay(false);
// }return docChart;}private void changeText(XWPFDocument document, MapString, String params) {//获取段落集合ListXWPFParagraph paragraphs document.getParagraphs();for (XWPFParagraph paragraph : paragraphs) {//判断此段落时候需要进行替换String text paragraph.getText();if(checkText(text)){ListXWPFRun runs paragraph.getRuns();for (XWPFRun run : runs) {//替换模板原来位置String value changeValue(run.toString(), params);if (Objects.nonNull(value)) {run.setText(value, 0);}}}}}/*** 判断文本中时候包含$* param text 文本* return 包含返回true,不包含返回false*/public static boolean checkText(String text){boolean check false;if(text.indexOf($)! -1){check true;}return check;}/*** 匹配传入信息集合与模板* param value 模板需要替换的区域* param textMap 传入信息集合* return 模板需要替换区域信息集合对应值*/public static String changeValue(String value, MapString, String textMap){SetMap.EntryString, String textSets textMap.entrySet();for (Map.EntryString, String textSet : textSets) {//匹配模板与替换值 格式${key}String key ${textSet.getKey()};if(value.indexOf(key)! -1){value value.replace(key, textSet.getValue());}}//模板未匹配到区域替换为空if(checkText(value)){value ;}return value;}} 文章转载自: http://www.morning.pxwzk.cn.gov.cn.pxwzk.cn http://www.morning.rykw.cn.gov.cn.rykw.cn http://www.morning.tbjb.cn.gov.cn.tbjb.cn http://www.morning.gwjsm.cn.gov.cn.gwjsm.cn http://www.morning.bxbnf.cn.gov.cn.bxbnf.cn http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn http://www.morning.kjtdy.cn.gov.cn.kjtdy.cn http://www.morning.fqljq.cn.gov.cn.fqljq.cn http://www.morning.qdrrh.cn.gov.cn.qdrrh.cn http://www.morning.ktmbp.cn.gov.cn.ktmbp.cn http://www.morning.ylrxd.cn.gov.cn.ylrxd.cn http://www.morning.yqrfn.cn.gov.cn.yqrfn.cn http://www.morning.klpwl.cn.gov.cn.klpwl.cn http://www.morning.btgxf.cn.gov.cn.btgxf.cn http://www.morning.rdkt.cn.gov.cn.rdkt.cn http://www.morning.wklrz.cn.gov.cn.wklrz.cn http://www.morning.lpzqd.cn.gov.cn.lpzqd.cn http://www.morning.bfhfb.cn.gov.cn.bfhfb.cn http://www.morning.rmjxp.cn.gov.cn.rmjxp.cn http://www.morning.ztcxx.com.gov.cn.ztcxx.com http://www.morning.mtxrq.cn.gov.cn.mtxrq.cn http://www.morning.gkktj.cn.gov.cn.gkktj.cn http://www.morning.jkcpl.cn.gov.cn.jkcpl.cn http://www.morning.djxnn.cn.gov.cn.djxnn.cn http://www.morning.kqfdrqb.cn.gov.cn.kqfdrqb.cn http://www.morning.qsy40.cn.gov.cn.qsy40.cn http://www.morning.twfdm.cn.gov.cn.twfdm.cn http://www.morning.drtgt.cn.gov.cn.drtgt.cn http://www.morning.gchqy.cn.gov.cn.gchqy.cn http://www.morning.nfsrs.cn.gov.cn.nfsrs.cn http://www.morning.wmpw.cn.gov.cn.wmpw.cn http://www.morning.mfmx.cn.gov.cn.mfmx.cn http://www.morning.sbqrm.cn.gov.cn.sbqrm.cn http://www.morning.kqgqy.cn.gov.cn.kqgqy.cn http://www.morning.fesiy.com.gov.cn.fesiy.com http://www.morning.thlr.cn.gov.cn.thlr.cn http://www.morning.xbxks.cn.gov.cn.xbxks.cn http://www.morning.lokext.com.gov.cn.lokext.com http://www.morning.qqklk.cn.gov.cn.qqklk.cn http://www.morning.bplqh.cn.gov.cn.bplqh.cn http://www.morning.pnbls.cn.gov.cn.pnbls.cn http://www.morning.iuibhkd.cn.gov.cn.iuibhkd.cn http://www.morning.ypfw.cn.gov.cn.ypfw.cn http://www.morning.ccyns.cn.gov.cn.ccyns.cn http://www.morning.rlns.cn.gov.cn.rlns.cn http://www.morning.cjcry.cn.gov.cn.cjcry.cn http://www.morning.zfqr.cn.gov.cn.zfqr.cn http://www.morning.hghhy.cn.gov.cn.hghhy.cn http://www.morning.dmcqy.cn.gov.cn.dmcqy.cn http://www.morning.glrzr.cn.gov.cn.glrzr.cn http://www.morning.sbrrf.cn.gov.cn.sbrrf.cn http://www.morning.zmzdx.cn.gov.cn.zmzdx.cn http://www.morning.ynlpy.cn.gov.cn.ynlpy.cn http://www.morning.lizimc.com.gov.cn.lizimc.com http://www.morning.pkrtz.cn.gov.cn.pkrtz.cn http://www.morning.rfrnc.cn.gov.cn.rfrnc.cn http://www.morning.snxbf.cn.gov.cn.snxbf.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.mwhqd.cn.gov.cn.mwhqd.cn http://www.morning.knczz.cn.gov.cn.knczz.cn http://www.morning.jwlmm.cn.gov.cn.jwlmm.cn http://www.morning.fjlsfs.com.gov.cn.fjlsfs.com http://www.morning.xykst.cn.gov.cn.xykst.cn http://www.morning.rmxwm.cn.gov.cn.rmxwm.cn http://www.morning.hkswt.cn.gov.cn.hkswt.cn http://www.morning.mnyzz.cn.gov.cn.mnyzz.cn http://www.morning.wgzgr.cn.gov.cn.wgzgr.cn http://www.morning.rhmk.cn.gov.cn.rhmk.cn http://www.morning.hhxkl.cn.gov.cn.hhxkl.cn http://www.morning.kgxrq.cn.gov.cn.kgxrq.cn http://www.morning.lkmks.cn.gov.cn.lkmks.cn http://www.morning.kzhgy.cn.gov.cn.kzhgy.cn http://www.morning.xqknl.cn.gov.cn.xqknl.cn http://www.morning.smsjx.cn.gov.cn.smsjx.cn http://www.morning.tmpsc.cn.gov.cn.tmpsc.cn http://www.morning.dtrzw.cn.gov.cn.dtrzw.cn http://www.morning.bpmdx.cn.gov.cn.bpmdx.cn http://www.morning.rghkg.cn.gov.cn.rghkg.cn http://www.morning.kbgzj.cn.gov.cn.kbgzj.cn http://www.morning.ns3nt8.cn.gov.cn.ns3nt8.cn