支持在pptx中动态刷新各种图表数据柱状图、饼状图、折线图、柱状折现组合图等
编写的工具类。
public class PoiPPTGrapthUtils {

private static Logger logger =Logger.getLogger(PoiPPTGrapthUtils.class);
@Resource
private ReadXmlUtil readXml;

@Resource
private IExportBatchWordService iExportBatchWordService;

public static void main(String[] args) {
//String templateFile = “D://ZHRISK_TEMPLETE.pptx”;
//String destFile = “D://dest.pptx”;
String templateFile = “D://weituimage.pptx”;
String destFile = “D://weituimage11.pptx”;
logger.info(“templateFile” + templateFile + " destFile:" + destFile);
XMLSlideShow pptx = null;
try {

     //准备模拟数据pptx = new XMLSlideShow(new FileInputStream(templateFile));List<BaseFormMap> dataList=new ArrayList<BaseFormMap>();//数据List<String> fldNameArr = new ArrayList<String>();// 字段名List<String> titleArr = new ArrayList<String>();// 标题BaseFormMap baseFormMap=new BaseFormMap();List<String> showtailArr=new ArrayList<String>();List<String> ispercentArr=new ArrayList<String>();

// 销售额
// 第一季度 8.2
// 第二季度 3.2
// 第三季度 1.4
// 第四季度 1.2
baseFormMap.put(“value1”, “2002/1/5”);
baseFormMap.put(“value2”, new BigDecimal(0.300));
baseFormMap.put(“value3”, new BigDecimal(0.500));

     BaseFormMap baseFormMap1=new BaseFormMap();baseFormMap1.put("value1", "2002/1/6");baseFormMap1.put("value2", new BigDecimal(0.300));baseFormMap1.put("value3", new BigDecimal(0.400));BaseFormMap baseFormMap2=new BaseFormMap();baseFormMap2.put("value1", "2002/1/7");baseFormMap2.put("value2", new BigDecimal(0.200));baseFormMap2.put("value3", new BigDecimal(0.300));BaseFormMap baseFormMap3=new BaseFormMap();baseFormMap3.put("value1", "2002/1/8");baseFormMap3.put("value2", new BigDecimal(0.200));baseFormMap3.put("value3", new BigDecimal(0.400));fldNameArr.add("value1");fldNameArr.add("value2");fldNameArr.add("value3");titleArr.add("类型");titleArr.add("买入");titleArr.add("卖出");showtailArr.add("0");showtailArr.add("3");showtailArr.add("3");ispercentArr.add("0");ispercentArr.add("1");ispercentArr.add("1");dataList.add(baseFormMap);dataList.add(baseFormMap1);dataList.add(baseFormMap2);dataList.add(baseFormMap3);//PPTGrapthUtils.judgeGraphSheetType(pptx);for (XSLFSlide slide : pptx.getSlides()) {for (POIXMLDocumentPart part : slide.getRelations()) {if (part instanceof XSLFChart) {boolean result = refreshLineGraph(pptx, (XSLFChart)part, dataList, fldNameArr, titleArr,showtailArr,ispercentArr);logger.info(result);}}}} catch (Exception e) {e.printStackTrace();}// 保存文件OutputStream out;try {out = new FileOutputStream("D://weituimage11.pptx");pptx.write(out);out.close();} catch (Exception e) {e.printStackTrace();}

}

public String createNewPPT(String templateFile, String destFile) {
String clearId="";
String suiteId="";
int serial=0;
String unitcode="";
String type="";
String result = “success”;
XMLSlideShow pptx = null;
try {
// 打开模板ppt
pptx = new XMLSlideShow(new FileInputStream(templateFile));
//PPTGrapthUtils.judgeGraphSheetType(pptx);
for (XSLFSlide slide : pptx.getSlides()) {

        for (POIXMLDocumentPart part : slide.getRelations()) {if (part instanceof XSLFChart) {updateGraph(pptx, (XSLFChart) part, clearId, suiteId, serial, unitcode, type);}}}// 保存文件OutputStream out = new FileOutputStream(destFile);pptx.write(out);out.close();} catch (Exception e) {e.printStackTrace();result = e.toString();} finally {if (pptx != null) {try {pptx.close();} catch (IOException e) {result = e.toString();}}}return result;

}

public boolean updateGraph(XMLSlideShow pptx,XSLFChart chart,String clearId, String suiteId, int serial,
String unitcode, String type) {
boolean result = false;
try {
String content = readXml.readFormat(serial, true);
content = content.replaceFirst(“GB2312”, “UTF-8”);

     List<String> list_seccategory = readXml.getSingleNode(content,"seccategory");String subName = list_seccategory.get(0);List<String> arr_fldname = new ArrayList<String>();// 字段名List<String> arr_showtext = new ArrayList<String>();// 显示文本List<String> arr_fldtype = new ArrayList<String>();// 字段类型,0文本,1数字,3日期List<String> arr_datarow = new ArrayList<String>();// 数据行属性List<String> arr_datatype = new ArrayList<String>();// 数据类型List<String> arr_showtail = new ArrayList<String>();//显示百分比List<String> arr_ispercent = new ArrayList<String>();//小数点// 读取表格配置信息List<String[]> list_format = readXml.readClob(content, type);String[] s = new String[36];// 解析表格配置信息for (int loopi = 0; loopi < list_format.size(); loopi++) {//// 开始s = list_format.get(loopi);arr_fldtype.add(s[0]);arr_fldname.add(s[1]);arr_showtext.add(s[2]);arr_datarow.add((s[13] == null ? "0" : s[13]));// 数据行:否、是arr_datatype.add((s[14] == null ? "0" : s[14]));// 数据类型:0文本、1数据、2日期arr_showtail.add(s[5]);arr_ispercent.add(s[6]);} // 每个表格元素int index = 0;int maxRow = readXml.rowcount(content); // 总行数int maxCol = readXml.colcount(content, type);// 总列数// 显示标题行index = 0;// 显示标题行 循环List<String> fldNameArr = new ArrayList<String>();// 字段名List<String> titleArr = new ArrayList<String>();// 标题List<String> showtailArr = new ArrayList<String>();//显示百分比List<String> ispercentArr = new ArrayList<String>();//小数点for (int i = 0; i < maxRow; i++) { index = i * maxCol; // 数据行:0否、1是 if (arr_datarow.get(index).trim().equals("1") ) {for (int colIndex = 0; colIndex < maxCol; colIndex++) {// 存储-类型:字段value、文本name、公式formulaif (arr_fldtype.get(index).equals("value")) {String fldName = arr_fldname.get(index);fldNameArr.add(fldName);showtailArr.add(arr_showtail.get(index));ispercentArr.add(arr_ispercent.get(index));}index++;// 循环单元格加1}}else {// 循环列for (int colIndex = 0; colIndex < maxCol; colIndex++) {// 存储-类型:字段value、文本name、公式formulaif (!arr_fldtype.get(index).trim().equals("value")) {String titleText = arr_showtext.get(index);titleArr.add(titleText);}index++;// 循环单元格加1}}}// 显示标题行 循环结束// 节点6401、6406、6151、6420、6045、6801、7101、6350 查询这些节点数据List<BaseFormMap> dataList = null; if (unitcode.equals("6045")) {dataList = iExportBatchWordService.getNodeTablesInfoBy6045(suiteId, subName);} else if (unitcode.equals("6151")) {dataList = iExportBatchWordService.getNodeTablesInfoBy6151(suiteId, subName);} else if (unitcode.equals("6202")) {dataList = iExportBatchWordService.getNodeTablesInfoBy6202(suiteId, subName);} else if (unitcode.equals("6350")) {dataList = iExportBatchWordService.getNodeTablesInfoBy6350(suiteId, subName);} else if (unitcode.equals("6420")) {dataList = iExportBatchWordService.getNodeTablesInfoBy6420(suiteId, "", "1", subName);} else if (unitcode.equals("6503")) {dataList = iExportBatchWordService.getNodeTablesInfoBy6503(suiteId, subName);} else if (unitcode.equals("6801")) {dataList = iExportBatchWordService.getNodeTablesInfoBy6801(suiteId, subName);} else if (unitcode.equals("7101")) {dataList = iExportBatchWordService.getNodeTablesInfoBy7101(suiteId, subName);} else if (unitcode.equals("6172")) {dataList = iExportBatchWordService.getNodeTables6172(clearId, subName);} else {dataList = iExportBatchWordService.getNodeTablesInfoOther(suiteId, subName);}String chartType = getGraphType(chart);if ("pie".equalsIgnoreCase(chartType)) {result = refreshPieGraph(pptx,chart,dataList,fldNameArr,titleArr,showtailArr,ispercentArr);} else if ("bar".equalsIgnoreCase(chartType)) {result = refreshBarGraph(pptx,chart,  dataList,fldNameArr,titleArr,showtailArr,ispercentArr);} else if ("line".equalsIgnoreCase(chartType)) {result = refreshLineGraph(pptx,chart, dataList,fldNameArr,titleArr,showtailArr,ispercentArr);} else if ("area".equalsIgnoreCase(chartType)) {result = refreshAreaGraph(pptx,chart, dataList,fldNameArr,titleArr,showtailArr,ispercentArr);}logger.info("updateGraph type:" + type);} catch (Exception e) {e.printStackTrace();}return result;

}

/**
* 通过图表类型
*
* @param chart
* @return
*/
private String getGraphType(XSLFChart chart) {
String graphTye = “noSupport”;
CTPlotArea plot = chart.getCTChart().getPlotArea();
if (null != plot && plot.getBarChartList().size() > 0) {
graphTye = “bar”;
} else if (null != plot && plot.getPieChartList().size() > 0) {
graphTye = “pie”;
} else if (null != plot && plot.getLineChartList().size() > 0) {
graphTye = “line”;
} else if (null != plot && plot.getAreaChartList().size() > 0) {
graphTye = “area”;
}

  return graphTye;

}

//
/**
* @Description: 刷新圆饼图
* @param pptx
* @param chart
* @param dataList
* @param fldNameArr
* @param titleArr
* @param showtailArr
* @param ispercentArr
* @return
* @author os-nieds
*/
private static boolean refreshPieGraph(XMLSlideShow pptx,XSLFChart chart,List dataList,List fldNameArr,List titleArr,List showtailArr,List ispercentArr) {
boolean result = false;
//根据属性第一列名称切换数据类型
CTChart ctChart = chart.getCTChart();
//获取标题中隐藏的类型
String type=getType(ctChart);
//刷新内置excel数据
result=refreshExcel(chart,dataList,fldNameArr,titleArr,showtailArr,ispercentArr);

  CTPlotArea plotArea = ctChart.getPlotArea();if(type.contains("pie")){//简单饼状CTPieChart pieChart = plotArea.getPieChartArray(0);List<CTPieSer> pieSerList = pieChart.getSerList();result=refreshStrGraphContent(pieChart,pieSerList,dataList,fldNameArr,titleArr,showtailArr,ispercentArr,1);}else if(type.contains("3D")){//3DCTPie3DChart pie3DChart = plotArea.getPie3DChartArray(0);List<CTPieSer> pie3DSerList = pie3DChart.getSerList();result=refreshStrGraphContent(pie3DChart,pie3DSerList,dataList,fldNameArr,titleArr,showtailArr,ispercentArr,1);}else{//c:doughnutChart环状CTDoughnutChart doughnutChart = plotArea.getDoughnutChartArray(0);List<CTPieSer> pieDoughnutSerList = doughnutChart.getSerList();result=refreshStrGraphContent(doughnutChart,pieDoughnutSerList,dataList,fldNameArr,titleArr,showtailArr,ispercentArr,1);}return result;

}

/**
* @Description: 刷新Excel数据
* @param chart
* @param dataList
* @param fldNameArr
* @param titleArr
* @param showtailArr
* @param ispercentArr
* @return boolean
* @author os-nieds
*/
private static boolean refreshExcel(XSLFChart chart,
List dataList,List fldNameArr,List titleArr,
List showtailArr,List ispercentArr) {
boolean result = true;
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
//根据数据创建excel第一行标题行
for (int i = 0; i < titleArr.size(); i++) {
if(sheet.getRow(0)==null){
sheet.createRow(0).createCell(i).setCellValue(titleArr.get(i)==null?"":titleArr.get(i));
}else{
sheet.getRow(0).createCell(i).setCellValue(titleArr.get(i)==null?"":titleArr.get(i));
}
}

  //遍历数据行for (int i = 0; i < dataList.size(); i++) {BaseFormMap baseFormMap = dataList.get(i);//数据行//fldNameArr字段属性for (int j = 0; j < fldNameArr.size(); j++) {if(sheet.getRow(i+1)==null){if(j==0){try {sheet.createRow(i+1).createCell(j).setCellValue(baseFormMap.getStr(fldNameArr.get(j))==null?"":baseFormMap.getStr(fldNameArr.get(j)));} catch (Exception e) {if(baseFormMap.getStr(fldNameArr.get(j))==null){sheet.createRow(i+1).createCell(j).setCellValue("");}else{sheet.createRow(i+1).createCell(j).setCellValue(baseFormMap.getDate(fldNameArr.get(j)));}}}}else{BigDecimal b=baseFormMap.getBigDecimal(fldNameArr.get(j));double value=0d;if(b!=null){value=b.doubleValue();}if(value==0){sheet.getRow(i+1).createCell(j);}else{sheet.getRow(i+1).createCell(j).setCellValue(baseFormMap.getBigDecimal(fldNameArr.get(j)).doubleValue());}if("1".equals(ispercentArr.get(j))){//是否设置百分比// 设置Y轴的数字为百分比样式显示StringBuilder sb=new StringBuilder();if("0".equals(showtailArr.get(j))){//保留几位小数sb.append("0");if("1".equals(ispercentArr.get(j))){//是否百分比sb.append("%");}}else{sb.append("0.");for(int k=0;k<Integer.parseInt(showtailArr.get(j));k++){sb.append("0");}if("1".equals(ispercentArr.get(j))){//是否百分比sb.append("%");}}CellStyle cellStyle = wb.createCellStyle();cellStyle.setDataFormat(wb.createDataFormat().getFormat(sb.toString()));sheet.getRow(i+1).getCell(j).setCellStyle(cellStyle);}else{//是否设置百分比// 设置Y轴的数字为百分比样式显示StringBuilder sb=new StringBuilder();if("0".equals(showtailArr.get(j))){//保留几位小数sb.append("0");}else{sb.append("0.");for(int k=0;k<Integer.parseInt(showtailArr.get(j));k++){sb.append("0");}}CellStyle cellStyle = wb.createCellStyle();cellStyle.setDataFormat(wb.createDataFormat().getFormat(sb.toString()));sheet.getRow(i+1).getCell(j).setCellStyle(cellStyle);}}}}// 更新嵌入的workbookPOIXMLDocumentPart xlsPart = chart.getRelations().get(2);OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream();try {wb.write(xlsOut);xlsOut.close();} catch (IOException e) {e.printStackTrace();result = false;} finally {if (wb != null) {try {wb.close();} catch (IOException e) {e.printStackTrace();result = false;}}}return result;

}

/**
* @Description: 刷新柱状图
* @param pptx
* @param chart
* @param dataList
* @param fldNameArr
* @param titleArr
* @param showtailArr 小数位
* @param ispercentArr 显示百分比 0否,1是
* @return boolean
* @author os-nieds
*/
private static boolean refreshBarGraph(XMLSlideShow pptx,XSLFChart chart,List dataList,List fldNameArr,
List titleArr,List showtailArr,List ispercentArr) {

  boolean result = false;//根据属性第一列名称切换数据类型CTChart ctChart = chart.getCTChart();CTPlotArea plotArea = ctChart.getPlotArea();CTBarChart barChart = plotArea.getBarChartArray(0);List<CTBarSer> barSerList = barChart.getSerList();//获取标题中隐藏的类型String type=getType(ctChart);//刷新内置excel数据result=refreshExcel(chart,dataList,fldNameArr,titleArr,showtailArr,ispercentArr);if(type.contains("bar1")||type.contains("bar2")){//交易情况-2,3,行业配置1,2result=refreshStrGraphContent(barChart,barSerList,dataList,fldNameArr,titleArr,showtailArr,ispercentArr,1);}else if(type.contains("bar-line")){//交易情况-1//更新数据区域barresult=refreshDateGraphContent(barChart,barSerList,dataList,fldNameArr,titleArr,showtailArr,ispercentArr,1);//更新数据区域lineCTLineChart lineChart = plotArea.getLineChartArray(0);List<CTLineSer> lineSerList = lineChart.getSerList();result=refreshDateGraphContent(lineChart,lineSerList,dataList,fldNameArr,titleArr,showtailArr,ispercentArr,3);}return result;

}

//获取隐藏类型
private static String getType(CTChart ctChart) {
//获取图标题隐藏的类型,获取完之后获取标题之后将数据清空
String type="";
String title="";
CTRegularTextRun[] ctArr = ctChart.getTitle().getTx().getRich().getPArray(0).getRArray();
for (int i = 0; i < ctArr.length; i++) {
if (i0) {
type=ctArr[0].getT();
logger.info(type);
ctArr[i].setT("");
}else if(i1){
title=ctArr[1].getT();
logger.info(title);
}else{
ctArr[i].setT("");
}

  }return type;

}

/**
* @Description:刷新数据方法(str进行分类)
* @param typeChart:传递的图形类型
* @param serList:传递的serList(数据)
* @param dataList:显示的数据
* @param fldNameArr:属性列
* @param position:取数的列
* @return boolean
*/
private static boolean refreshStrGraphContent(Object typeChart,
List<?> serList, List dataList, List fldNameArr,List titleArr,ListshowtailArr,List ispercentArr, int position) {

  boolean result=true;//更新数据区域for (int i = 0; i < serList.size(); i++) {CTSerTx tx=null;CTAxDataSource cat=null;CTNumDataSource val=null;//获取序列if(typeChart instanceof CTPieChart){//普通饼状CTPieSer ser = ((CTPieChart)typeChart).getSerArray(i);tx= ser.getTx();// Category Axis Datacat= ser.getCat();// 获取图表的值val = ser.getVal();}else if(typeChart instanceof CTPie3DChart){//3D饼状CTPieSer ser = ((CTPie3DChart)typeChart).getSerArray(i);tx= ser.getTx();// Category Axis Datacat= ser.getCat();// 获取图表的值val = ser.getVal();}else if(typeChart instanceof CTBarChart){//柱状图简单CTBarSer ser = ((CTBarChart)typeChart).getSerArray(i);tx= ser.getTx();// Category Axis Datacat= ser.getCat();// 获取图表的值val = ser.getVal();}else if(typeChart instanceof CTLineChart){//简单折线图CTLineSer ser = ((CTLineChart)typeChart).getSerArray(i);tx= ser.getTx();// Category Axis Datacat= ser.getCat();// 获取图表的值val = ser.getVal();}else if(typeChart instanceof CTAreaChart){CTAreaSer ser = ((CTAreaChart)typeChart).getSerArray(i);tx= ser.getTx();// Category Axis Datacat= ser.getCat();// 获取图表的值val = ser.getVal();}else if(typeChart instanceof CTDoughnutChart){CTPieSer ser = ((CTDoughnutChart)typeChart).getSerArray(i);tx= ser.getTx();// Category Axis Datacat= ser.getCat();// 获取图表的值val = ser.getVal();}else{logger.info("图形类型不存在");}// strData.setCTStrData strData = cat.getStrRef().getStrCache();CTNumData numData = val.getNumRef().getNumCache();strData.setPtArray((CTStrVal[]) null); // unset old axis textnumData.setPtArray((CTNumVal[]) null); // unset old values// set modellong idx = 0;for (int j=0;j<dataList.size();j++) {//判断获取的值是否为空String value = "0";if(dataList.get(j).getBigDecimal(fldNameArr.get(i+position))!=null){value=dataList.get(j).getBigDecimal(fldNameArr.get(i+position)).toString();}if(!"0".equals(value)){CTNumVal numVal = numData.addNewPt();//序列值numVal.setIdx(idx);numVal.setV(value);}CTStrVal sVal = strData.addNewPt();//序列名称sVal.setIdx(idx);sVal.setV(dataList.get(j).getStr(fldNameArr.get(0)));idx++;}numData.getPtCount().setVal(idx);strData.getPtCount().setVal(idx);//赋值横坐标数据区域String axisDataRange = new CellRangeAddress(1, dataList.size(), 0, 0).formatAsString("Sheet0", true);cat.getStrRef().setF(axisDataRange);tx.getStrRef().getStrCache().getPtArray(0).setV(titleArr.get(i+position));//图例区域String legendDataRange = new CellRangeAddress(0, 0, i+position, i+position).formatAsString("Sheet0", true);tx.getStrRef().setF(legendDataRange);//数据区域String numDataRange = new CellRangeAddress(1, dataList.size(), i+position, i+position).formatAsString("Sheet0", true);val.getNumRef().setF(numDataRange);if("1".equals(ispercentArr.get(i+position))){//是否设置百分比// 设置Y轴的数字为百分比样式显示StringBuilder sb=new StringBuilder();if("0".equals(showtailArr.get(i+position))){//保留几位小数sb.append("0");if("1".equals(ispercentArr.get(i+position))){//是否百分比sb.append("%");}}else{sb.append("0.");for(int k=0;k<Integer.parseInt(showtailArr.get(i+position));k++){sb.append("0");}if("1".equals(ispercentArr.get(i+position))){//是否百分比sb.append("%");}}val.getNumRef().getNumCache().setFormatCode(sb.toString());}else{//是否设置百分比// 设置Y轴的数字为百分比样式显示StringBuilder sb=new StringBuilder();if("0".equals(showtailArr.get(i+position))){//保留几位小数sb.append("0");}else{sb.append("0.");for(int k=0;k<Integer.parseInt(showtailArr.get(i+position));k++){sb.append("0");}}val.getNumRef().getNumCache().setFormatCode(sb.toString());}}return result;

}

/**
* @Description:刷新数据方法(日期进行分类)
* @param typeChart:传递的图形类型
* @param serList:传递的serList(数据)
* @param dataList:显示的数据
* @param fldNameArr:属性列
* @param position:取数的列
* @return boolean
* @author os-nieds
*/
private static boolean refreshDateGraphContent(Object typeChart,
List<?> serList, List dataList, List fldNameArr, List titleArr,ListshowtailArr,List ispercentArr,int position) {
boolean result=true;
//更新数据区域
for (int i = 0; i < serList.size(); i++) {
CTSerTx tx=null;
CTAxDataSource cat=null;
CTNumDataSource val=null;
//获取序列
if(typeChart instanceof CTPieChart){//普通饼状
CTPieSer ser = ((CTPieChart)typeChart).getSerArray(i);
tx= ser.getTx();
// Category Axis Data
cat= ser.getCat();
// 获取图表的值
val = ser.getVal();
}else if(typeChart instanceof CTPie3DChart){//3D饼状
CTPieSer ser = ((CTPie3DChart)typeChart).getSerArray(i);
tx= ser.getTx();
// Category Axis Data
cat= ser.getCat();
// 获取图表的值
val = ser.getVal();
}else if(typeChart instanceof CTBarChart){//柱状图简单
CTBarSer ser = ((CTBarChart)typeChart).getSerArray(i);
tx= ser.getTx();
// Category Axis Data
cat= ser.getCat();
// 获取图表的值
val = ser.getVal();
}else if(typeChart instanceof CTLineChart){//简单折线图
CTLineSer ser = ((CTLineChart)typeChart).getSerArray(i);
tx= ser.getTx();
// Category Axis Data
cat= ser.getCat();
// 获取图表的值
val = ser.getVal();
}else if(typeChart instanceof CTAreaChart){
CTAreaSer ser = ((CTAreaChart)typeChart).getSerArray(i);
tx= ser.getTx();
// Category Axis Data
cat= ser.getCat();
// 获取图表的值
val = ser.getVal();
}else if(typeChart instanceof CTDoughnutChart){
CTPieSer ser = ((CTDoughnutChart)typeChart).getSerArray(i);
tx= ser.getTx();
// Category Axis Data
cat= ser.getCat();
// 获取图表的值
val = ser.getVal();
}else{
logger.info(“图形类型不存在”);
}

     // strData.setCTNumData strData = cat.getNumRef().getNumCache();CTNumData numData = val.getNumRef().getNumCache();strData.setPtArray((CTNumVal[]) null); // unset old axis textnumData.setPtArray((CTNumVal[]) null); // unset old values// set modellong idx = 0;for (int j=0;j<dataList.size();j++) {//判断获取的值是否为空String value = "0";if(dataList.get(j).getBigDecimal(fldNameArr.get(i+position))!=null){value=dataList.get(j).getBigDecimal(fldNameArr.get(i+position)).toString();}if(!"0".equals(value)){CTNumVal numVal = numData.addNewPt();//序列值numVal.setIdx(idx);numVal.setV(value);}CTNumVal sVal = strData.addNewPt();//序列名称sVal.setIdx(idx);String days=getDaysSum(dataList.get(j).getStr(fldNameArr.get(0)));sVal.setV(days);idx++;}numData.getPtCount().setVal(idx);strData.getPtCount().setVal(idx);//赋值横坐标数据区域String axisDataRange = new CellRangeAddress(1, dataList.size(), 0, 0).formatAsString("Sheet0", true);cat.getNumRef().setF(axisDataRange);//图例区域String legendDataRange = new CellRangeAddress(0, 0, i+position, i+position).formatAsString("Sheet0", true);tx.getStrRef().setF(legendDataRange);tx.getStrRef().getStrCache().getPtArray(0).setV(titleArr.get(i+position));//数据区域String numDataRange = new CellRangeAddress(1, dataList.size(), i+position, i+position).formatAsString("Sheet0", true);val.getNumRef().setF(numDataRange);if("1".equals(ispercentArr.get(i+position))){//是否设置百分比// 设置Y轴的数字为百分比样式显示StringBuilder sb=new StringBuilder();if("0".equals(showtailArr.get(i+position))){//保留几位小数sb.append("0");if("1".equals(ispercentArr.get(i+position))){//是否百分比sb.append("%");}}else{sb.append("0.");for(int k=0;k<Integer.parseInt(showtailArr.get(i+position));k++){sb.append("0");}if("1".equals(ispercentArr.get(i+position))){//是否百分比sb.append("%");}}val.getNumRef().getNumCache().setFormatCode(sb.toString());}else{//是否设置百分比// 设置Y轴的数字为百分比样式显示StringBuilder sb=new StringBuilder();if("0".equals(showtailArr.get(i+position))){//保留几位小数sb.append("0");}else{sb.append("0.");for(int k=0;k<Integer.parseInt(showtailArr.get(i+position));k++){sb.append("0");}}val.getNumRef().getNumCache().setFormatCode(sb.toString());}}return result;

}

//获取天数从1900年1月1日到endYear
private static String getDaysSum(String endYear) {
String endYearArr[]=endYear.split("/");
int year=Integer.parseInt(endYearArr[0]);
int month=Integer.parseInt(endYearArr[1]);
int day=Integer.parseInt(endYearArr[2]);
LocalDate start=new LocalDate(1900, 1,1);
LocalDate end=new LocalDate(year,month,day);
int days = Days.daysBetween(start, end).getDays()+2;
return days+"";
}

/**
* @Description:刷新线性图
* @param pptx
* @param chart
* @param dataList
* @param fldNameArr
* @param titleArr
* @param showtailArr
* @param ispercentArr
* @return
* @author os-nieds
*/
private static boolean refreshLineGraph(XMLSlideShow pptx,XSLFChart chart,List dataList,List fldNameArr,
List titleArr,List showtailArr,List ispercentArr) {
boolean result = false;
//根据属性第一列名称切换数据类型
CTChart ctChart = chart.getCTChart();
CTPlotArea plotArea = ctChart.getPlotArea();
//更新数据区域line
CTLineChart lineChart = plotArea.getLineChartArray(0);
List lineSerList = lineChart.getSerList();
//获取标题中隐藏的类型
String type=getType(ctChart);
logger.info(type);
//刷新内置excel数据
result=refreshExcel(chart,dataList,fldNameArr,titleArr,showtailArr,ispercentArr);
//刷新图表数据
result=refreshDateGraphContent(lineChart,lineSerList,dataList,fldNameArr,titleArr,showtailArr,ispercentArr,1);
return result;
}

/**
* @Description:刷新面积图(只支持pptx中的area前两种)
* @param pptx
* @param chart
* @param dataList
* @param fldNameArr
* @param titleArr
* @param showtailArr
* @param ispercentArr
* @return
* @author os-nieds
*/
private static boolean refreshAreaGraph(XMLSlideShow pptx,XSLFChart chart,List dataList,List fldNameArr,List titleArr,ListshowtailArr,List ispercentArr) {
boolean result = false;
//根据属性第一列名称切换数据类型
CTChart ctChart = chart.getCTChart();
CTPlotArea plotArea = ctChart.getPlotArea();
CTAreaChart areaChart = plotArea.getAreaChartArray(0);
List areaSerList = areaChart.getSerList();
//获取标题中隐藏的类型
String type=getType(ctChart);
logger.info(type);
//刷新内置excel数据
result=refreshExcel(chart,dataList,fldNameArr,titleArr,showtailArr,ispercentArr);
//刷新图表数据
result=refreshDateGraphContent(areaChart,areaSerList,dataList,fldNameArr,titleArr,showtailArr,ispercentArr,1);
return result;
}

poi在pptx中动态刷新已经存在的图相关推荐

  1. java使用poi在word中生成柱状图、折线图、饼图、柱状图+折线图组合图、动态表格、文本替换、图片替换、更新内置Excel数据、更新插入的文本框内容、合并表格单元格;

    本文参考地址:https://blog.csdn.net/wangxiaoyingWXY/article/details/95377533 在参考文章的基础上,增加了扩展.感谢被参考的妹子.另外该博客 ...

  2. java使用poi在word中生成柱状图、折线图、饼图、柱状图+折线图组合图、动态表格、文本替换、图片替换 springboot项目

    本文参考地址:https://blog.csdn.net/u014427811/article/details/100771314 在参考文章的基础上,增加了模板样例 模板样例地址 百度网盘 链接:h ...

  3. poi编辑word中的chart,对图表数据替换,并刷新图表

    最近工作需要做报表导出,再次接触poi,上次用poi的时候还是上次(在前司编辑excel的时候). 而word呢,数据结构上感觉又比excel复杂,至少我处理的时候认为是这样. 首先看一下要处理的wo ...

  4. POI处理Excel中的日期数据类型

    在POI处理Excel中的日期类型的单元格时,如果仅仅是判断它是否为日期类型的话,最终会以NUMERIC类型来处理. 正确的处理方法是先判断单元格 的类型是否则NUMERIC类型, 然后再判断单元格是 ...

  5. java通过poi读取excel中的日期类型数据或自定义类型日期

    java通过poi读取excel中的日期类型数据或自定义类型日期 Java 读取Excel表格日期类型数据的时候,读出来的是这样的  12-十月-2019,而Excel中输入的是 2019/10/12 ...

  6. java使用poi读写word中的图片(二)

    文章目录 准备工作 简单读取 复杂读取 查看Word的XML 特别说明:Word中的Svg图片 第一种写入图片到Word中的方式 第二种写入图片到Word中的方式 最后 准备工作 这里就不在复述了,可 ...

  7. java POI对word中的表格动态插入固定数据,以及插入不确定数量的的数据

    java POI对word中的表格动态插入固定数据,以及插入不具体的数据 遇到个项目本来是用Execl导出的,相对简单,客户要求用Word导出,并按照他们给的模板进行导出: 从网上百度了一下,然后自己 ...

  8. Office问题:PowerPoint发现.pptx 中的内容有问题。PowerPoint可尝试修复此演示文稿。

    当用office的PowerPoint软件打开ppt/pptx文件显示: 发现XXX.pptx 中的内容有问题.PowerPoint可尝试修复此演示文稿. 如果PPT是从网上下载的模板等,可尝试如下解 ...

  9. PowerPoint 打开文档发现.pptx中胡内容有问题

    一.问题的提出 有一个文件,在window 7操作系统中通过邮箱地址保存到本地,结果打开的时候出现[PowerPoint 打开文档发现 文件.pptx中胡内容有问题] 然后提示[如果您信任此演示文稿的 ...

最新文章

  1. opencv reduce函数
  2. rcmd: socket: Permission denied
  3. SAPABAP性能优化技巧—使用二分查找(Binary Search)选项
  4. Generator 简介
  5. java 课后习题 随机整数最大值和最小值
  6. 浅谈web网站架构演变过程
  7. [水池] 灌水专用:搜狗输入法里收集的纯文字表情
  8. eclipse中编译java_eclipse编译java文件
  9. bp神经网络预测模型python,bp神经网络预测模型
  10. python使用gdal读写BIP、BSQ数据格式tif及相互转换
  11. Python/python/xpath爬虫--妙招网
  12. 022 漏洞与木马、端口扫描、病毒(娱乐向...pdf)
  13. 电气无级变速器(EVT)混合动力传动系半实物仿真平台ETest
  14. Intel寄存器名称解释及用途,%eax%ebx等都是什么意思
  15. 汉字如何应用在平面设计中
  16. 研究google搜索框代码
  17. JavaScript基本原理常识
  18. Elasticsearch入门教程(五):Elasticsearch查询(一)
  19. HTML5期末大作业:个人主页网站设计(6页)代码质量高 学生简单个人静态HTML网页设计作品 DIV布局个人介绍网页模板代码 DW学生个人网站制作成品下载
  20. Latex:跨页多图

热门文章

  1. Nginx和Tomcat优化
  2. Win11如何清理C盘垃圾文件
  3. 链游知识4:以太坊浏览器的使用
  4. IUV“经世杯”option3X
  5. SPA(单页面web应用)和MPA(多页面web应用)的区别
  6. 基于c#条码打印软件源代码 可选择打印机,可选择标签
  7. 网站已迁移、域名未转出,收到阿里云的“备案号可能被取消接入”邮件提示该怎么办。
  8. 混合应用 微信登录授权 微信登录认证失败 ios PGWXAPI错误-1 code:-100 / 安卓 message:invalid appsecret innerCode:40125...
  9. 去雾模块dehaze.py(可直接调用)
  10. 零知识证明(zero knowledge validation)