导出多个饼状图pie图表到ppt中

时间:2021-09-23 09:40:18
【文件属性】:

文件名称:导出多个饼状图pie图表到ppt中

文件大小:297KB

文件格式:RAR

更新时间:2021-09-23 09:40:18

poi 多个饼状图 导出图表ppt poi导出ppt

poi 版本 3.17 文件中包含 模板 测试main 数据类 需要调整下 路径 /** * @Description: * @Author: xsr * @date : 2018/7/22 9:41 */ public static void makePiePpt(List dataList) throws Exception { //打开模板ppt String mtemplateName = "E:/PIE/mtemplate/PIE" + dataList.size() + ".pptx"; String path ="E:/PIE/NewPPT/NewPIE" + dataList.size() + ".pptx"; XMLSlideShow pptx = new XMLSlideShow(new FileInputStream(mtemplateName)); pptx.setPageSize(new Dimension(960, 580)); for (int i = 0; i < dataList.size(); i++) { makePiePpt(pptx, i, dataList); } //保存文件 OutputStream out = new FileOutputStream(path); pptx.write(out); out.close(); System.out.println("导出成功"); } /** * @Description: * @Author: xsr * @date : 2018/7/27 5:41 */ public static void makePiePpt(XMLSlideShow pptx, Integer pieNum, List dataList) throws Exception { //获取第一个ppt页面 XSLFSlide slide = pptx.getSlides().get(0); //遍历第一页元素找到图表 XSLFChart chart; List poixmlDocumentParts = new ArrayList<>(); for (POIXMLDocumentPart part : slide.getRelations()) { if (part instanceof XSLFChart) { chart = (XSLFChart) part; poixmlDocumentParts.add(chart); } } chart = (XSLFChart) poixmlDocumentParts.get(pieNum); POIXMLDocumentPart xlsPart = chart.getRelations().get(0); //把图表绑定到Excel workbook中 XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(); CTChart ctChart = chart.getCTChart(); CTPlotArea plotArea = ctChart.getPlotArea(); CTPieChart pieChart = plotArea.getPieChartArray(0); // 获取图表的系列 CTPieSer ser = pieChart.getSerArray(0); XSSFRow row0 = sheet.createRow(0); // Series Text CTSerTx tx = ser.getTx(); tx.getStrRef().getStrCache().getPtArray(0).setV(dataList.get(pieNum).getTitleName()); row0.createCell(1).setCellValue(dataList.get(pieNum).getTitleName()); String titleRef = new CellReference(sheet.getSheetName(), 0, 1, true, true).formatAsString(); tx.getStrRef().setF(titleRef); // Category Axis Data CTAxDataSource cat = ser.getCat(); CTStrData strData = cat.getStrRef().getStrCache(); //获取图表的值 CTNumDataSource val = ser.getVal(); CTNumData numData = val.getNumRef().getNumCache(); strData.setPtArray(null); // unset old axis text numData.setPtArray(null); // unset old values // set model int idx = 0; int rownum = 1; XSSFRow row; for (int i = 0; i < dataList.get(pieNum).getLegengName().size(); i++) { CTNumVal numVal = numData.addNewPt(); numVal.setIdx(idx); numVal.setV(dataList.get(pieNum).getLegengValue().get(i)); CTStrVal sVal = strData.addNewPt(); sVal.setIdx(idx); sVal.setV(dataList.get(pieNum).getLegengName().get(i)); idx++; row = sheet.createRow(rownum++); row.createCell(0).setCellValue(dataList.get(pieNum).getLegengName().get(i)); row.createCell(1).setCellValue(Double.valueOf(dataList.get(pieNum).getLegengValue().get(i))); } numData.getPtCount().setVal(idx); strData.getPtCount().setVal(idx); String numDataRange = new CellRangeAddress(1, rownum - 1, 1, 1).formatAsString(sheet.getSheetName(), true); val.getNumRef().setF(numDataRange); String axisDataRange = new CellRangeAddress(1, rownum - 1, 0, 0).formatAsString(sheet.getSheetName(), true); cat.getStrRef().setF(axisDataRange); //更新嵌入的workbook OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream(); wb.write(xlsOut); xlsOut.close(); }


【文件预览】:
multi-pie
----pie.java(6KB)
----PIE()
--------mtemplate()
--------NewPPT()
----PieData.java(1KB)

网友评论