poi操作ppt图表史上最完整示例演示和内嵌excel的获取添加数据简单示例,POI3.15版本. 在模板中构造几中基本图表进行测试就行了.

其它操作ppt的基础资料见:http://blog.csdn.net/mike_caoyong/article/details/28651665

https://blog.csdn.net/mike_caoyong/article/details/71852438

代码项目完整下载地址:https://pan.baidu.com/s/19VrO-wlEWDKVhc2XzH6rLw

提取码:ki6e

模板下载地址:https://pan.baidu.com/s/1LI-dWDqQhl-qpL6bcBsFWA

提取码:q4zh

  1. 图表数据类

    import java.util.List;/*** 图表系列数据* * @author caoyong**/
    public class GraphData {// 图形标题private String title;// 系列值private List<SeriesData> serList;public GraphData(String title, List<SeriesData> serList){this.title = title;this.serList = serList;}public String getTitle(){return title;}public List<SeriesData> getSerList(){return serList;}
    }class SeriesData {// 系列名称private String serName;// 系列值private double serVal;public SeriesData(String serName, double serVal){this.serName = serName;this.serVal = serVal;}public String getSerName(){return serName;}public double getSerVal(){return serVal;}}
  2. PPT图表操作公共类

    import java.io.IOException;
    import java.io.OutputStream;import org.apache.poi.POIXMLDocumentPart;
    import org.apache.poi.hssf.util.CellReference;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.util.CellRangeAddress;
    import org.apache.poi.xslf.usermodel.XMLSlideShow;
    import org.apache.poi.xslf.usermodel.XSLFChart;
    import org.apache.poi.xslf.usermodel.XSLFSlide;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.openxmlformats.schemas.drawingml.x2006.chart.CTAreaChart;
    import org.openxmlformats.schemas.drawingml.x2006.chart.CTAreaSer;
    import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;
    import org.openxmlformats.schemas.drawingml.x2006.chart.CTBarChart;
    import org.openxmlformats.schemas.drawingml.x2006.chart.CTBarSer;
    import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
    import org.openxmlformats.schemas.drawingml.x2006.chart.CTLineChart;
    import org.openxmlformats.schemas.drawingml.x2006.chart.CTLineSer;
    import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumData;
    import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;
    import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumVal;
    import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieChart;
    import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieSer;
    import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
    import org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx;
    import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrData;
    import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrVal;/*** PPT公具类* * @author CAOYONG**/
    public class PPTGrapthUtils
    {public static void updateGraph(XSLFChart chart, GraphData graphData){String type = PPTGrapthUtils.getGraphType(chart);if ("pie".equalsIgnoreCase(type)){PPTGrapthUtils.refreshPieGraph(chart, graphData);} else if ("bar".equalsIgnoreCase(type)){refreshBarGraph(chart, graphData);} else if ("line".equalsIgnoreCase(type)){refreshLineGraph(chart, graphData);} else if ("area".equalsIgnoreCase(type)){refreshAreaGraph(chart, graphData);}System.out.println("updateGraph type:" + type);}/*** 判断PPT图表类型(注意需要依赖:ooxml-schemas-1.1.jar)* * @param pptx*/public static void judgeGraphSheetType(XMLSlideShow pptx){int pageIdx = 1;for (XSLFSlide slide : pptx.getSlides()){for (POIXMLDocumentPart part : slide.getRelations()){if (part instanceof XSLFChart){XSLFChart chart = (XSLFChart) part;CTPlotArea plot = chart.getCTChart().getPlotArea();judgeGraphSheetType(plot, pageIdx);}}pageIdx++;}}// 具体判断private static void judgeGraphSheetType(CTPlotArea plot, int pageIdx){StringBuffer infos = new StringBuffer();if (null != plot && plot.getBarChartList().size() > 0){infos.append("pageIdx:" + pageIdx + " has 柱状图");infos.append(" DetailInfo: \n ");for (CTBarSer ser : plot.getBarChartList().get(0).getSerList()){infos.append(getGraphTitle(ser.getTx()));infos.append(" ");String info = getGraphDetailInfo(ser.getCat());ser.getTx();if (info.length() > 0){infos.append(info + "\n");}}} else if (null != plot && plot.getPieChartList().size() > 0){infos.append("pageIdx:" + pageIdx + " has 圆饼图");infos.append(" DetailInfo: \n ");for (CTPieSer ser : plot.getPieChartList().get(0).getSerList()){infos.append(getGraphTitle(ser.getTx()));infos.append(" ");String info = getGraphDetailInfo(ser.getCat());if (info.length() > 0){infos.append(info + "\n");}}} else if (null != plot && plot.getLineChartList().size() > 0){infos.append("pageIdx:" + pageIdx + " has 线性图");infos.append(" DetailInfo: \n ");for (CTLineSer ser : plot.getLineChartList().get(0).getSerList()){infos.append(getGraphTitle(ser.getTx()));infos.append(" ");String info = getGraphDetailInfo(ser.getCat());if (info.length() > 0){infos.append(info + "\n");}}} else if (null != plot && plot.getAreaChartList().size() > 0){infos.append("pageIdx:" + pageIdx + " has 面积图");infos.append(" DetailInfo: \n ");for (CTAreaSer ser : plot.getAreaChartList().get(0).getSerList()){infos.append(getGraphTitle(ser.getTx()));infos.append(" ");String info = getGraphDetailInfo(ser.getCat());if (info.length() > 0){infos.append(info + "\n");}}}// 还可以判断其它图System.out.println(infos.toString());}// 得到图表标题private static String getGraphTitle(CTSerTx tx){StringBuilder infos = new StringBuilder();if (null != tx && null != tx.getStrRef()){for (CTStrVal val : tx.getStrRef().getStrCache().getPtList()){infos.append("Title ID:" + val.getIdx() + " V:" + val.getV());}infos.append("\n");}return infos.toString();}// 得到第系值private static String getGraphDetailInfo(CTAxDataSource cat){StringBuilder infos = new StringBuilder();if (null != cat && null != cat.getStrRef()){for (CTStrVal val : cat.getStrRef().getStrCache().getPtList()){infos.append("ser ID:" + val.getIdx() + " V:" + val.getV());}}return infos.toString();}/*** 通过图表类型* * @param chart* @return*/private static 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;}// 刷新圆饼图private static boolean refreshPieGraph(XSLFChart chart, GraphData graphData){boolean result = true;// 把图表绑定到Excel workbook中try{Workbook wb = new XSSFWorkbook();Sheet sheet = wb.createSheet();CTChart ctChart = chart.getCTChart();CTPlotArea plotArea = ctChart.getPlotArea();CTPieChart pieChart = plotArea.getPieChartArray(0);// 获取图表的系列CTPieSer ser = pieChart.getSerArray(0);// Series TextCTSerTx tx = ser.getTx();tx.getStrRef().getStrCache().getPtArray(0).setV(graphData.getTitle());sheet.createRow(0).createCell(1).setCellValue(graphData.getTitle());String titleRef = new CellReference(sheet.getSheetName(), 0, 1, true, true).formatAsString();tx.getStrRef().setF(titleRef);// Category Axis DataCTAxDataSource cat = ser.getCat();// 获取图表的值CTNumDataSource val = ser.getVal();refreshGraphContent(sheet, cat, val, graphData);// 更新嵌入的workbookPOIXMLDocumentPart xlsPart = chart.getRelations().get(0);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;}}}} catch (Exception e){e.printStackTrace();}return result;}// 刷新柱状图private static boolean refreshBarGraph(XSLFChart chart, GraphData graphData){boolean result = true;// 把图表绑定到Excel workbook中try{Workbook wb = new XSSFWorkbook();Sheet sheet = wb.createSheet();CTChart ctChart = chart.getCTChart();CTPlotArea plotArea = ctChart.getPlotArea();CTBarChart pieChart = plotArea.getBarChartArray(0);// 获取图表的系列CTBarSer ser = pieChart.getSerArray(0);// Series TextCTSerTx tx = ser.getTx();tx.getStrRef().getStrCache().getPtArray(0).setV(graphData.getTitle());sheet.createRow(0).createCell(1).setCellValue(graphData.getTitle());String titleRef = new CellReference(sheet.getSheetName(), 0, 1, true, true).formatAsString();tx.getStrRef().setF(titleRef);// Category Axis DataCTAxDataSource cat = ser.getCat();// 获取图表的值CTNumDataSource val = ser.getVal();refreshGraphContent(sheet, cat, val, graphData);// 更新嵌入的workbookPOIXMLDocumentPart xlsPart = chart.getRelations().get(0);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;}}}} catch (Exception e){e.printStackTrace();}return result;}// 刷新线性图private static boolean refreshLineGraph(XSLFChart chart, GraphData graphData){boolean result = true;// 把图表绑定到Excel workbook中try{Workbook wb = new XSSFWorkbook();Sheet sheet = wb.createSheet();CTChart ctChart = chart.getCTChart();CTPlotArea plotArea = ctChart.getPlotArea();CTLineChart pieChart = plotArea.getLineChartArray(0);// 获取图表的系列CTLineSer ser = pieChart.getSerArray(0);// Series TextCTSerTx tx = ser.getTx();tx.getStrRef().getStrCache().getPtArray(0).setV(graphData.getTitle());sheet.createRow(0).createCell(1).setCellValue(graphData.getTitle());String titleRef = new CellReference(sheet.getSheetName(), 0, 1, true, true).formatAsString();tx.getStrRef().setF(titleRef);// Category Axis DataCTAxDataSource cat = ser.getCat();// 获取图表的值CTNumDataSource val = ser.getVal();refreshGraphContent(sheet, cat, val, graphData);// 更新嵌入的workbookPOIXMLDocumentPart xlsPart = chart.getRelations().get(0);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;}}}} catch (Exception e){e.printStackTrace();}return result;}// 刷新面积图private static boolean refreshAreaGraph(XSLFChart chart, GraphData graphData){boolean result = true;// 把图表绑定到Excel workbook中try{Workbook wb = new XSSFWorkbook();Sheet sheet = wb.createSheet();CTChart ctChart = chart.getCTChart();CTPlotArea plotArea = ctChart.getPlotArea();CTAreaChart pieChart = plotArea.getAreaChartArray(0);// 获取图表的系列CTAreaSer ser = pieChart.getSerArray(0);// Series TextCTSerTx tx = ser.getTx();tx.getStrRef().getStrCache().getPtArray(0).setV(graphData.getTitle());sheet.createRow(0).createCell(1).setCellValue(graphData.getTitle());String titleRef = new CellReference(sheet.getSheetName(), 0, 1, true, true).formatAsString();tx.getStrRef().setF(titleRef);// Category Axis DataCTAxDataSource cat = ser.getCat();// 获取图表的值CTNumDataSource val = ser.getVal();refreshGraphContent(sheet, cat, val, graphData);// 更新嵌入的workbookPOIXMLDocumentPart xlsPart = chart.getRelations().get(0);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;}}}} catch (Exception e){e.printStackTrace();}return result;}// 刷新图表内容private static void refreshGraphContent(Sheet sheet, CTAxDataSource cat, CTNumDataSource val, GraphData graphData){CTStrData strData = cat.getStrRef().getStrCache();CTNumData numData = val.getNumRef().getNumCache();// strData.setstrData.setPtArray((CTStrVal[]) null); // unset old axis textnumData.setPtArray((CTNumVal[]) null); // unset old values// set modellong idx = 0;int rownum = 1;for (SeriesData seriesData : graphData.getSerList()){CTNumVal numVal = numData.addNewPt();numVal.setIdx(idx);numVal.setV(seriesData.getSerVal() + "");CTStrVal sVal = strData.addNewPt();sVal.setIdx(idx);sVal.setV(seriesData.getSerName());idx++;Row row = sheet.createRow(rownum++);row.createCell(0).setCellValue(seriesData.getSerName());row.createCell(1).setCellValue(seriesData.getSerVal());}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);}}
  3. 测试类

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.ArrayList;
    import java.util.List;import org.apache.poi.POIXMLDocumentPart;
    import org.apache.poi.xslf.usermodel.XMLSlideShow;
    import org.apache.poi.xslf.usermodel.XSLFChart;
    import org.apache.poi.xslf.usermodel.XSLFSlide;/*** PPT图表测试类* * @author cy**/
    public class PPTGraphDemo
    {public static void main(String[] args){String basePath = System.getProperty("user.dir") + File.separator + "testFile";String templateFile = basePath + File.separator + "template.pptx";String destFile = basePath + File.separator + "dest.pptx";System.out.println("templateFile" + templateFile + " destFile:" + destFile);String result = createNewPPT(templateFile, destFile);System.out.println(result);}public static String createNewPPT(String templateFile, String destFile){String result = "success";XMLSlideShow pptx = null;try{// 打开模板pptpptx = new XMLSlideShow(new FileInputStream(templateFile));PPTGrapthUtils.judgeGraphSheetType(pptx);for (XSLFSlide slide : pptx.getSlides()){for (POIXMLDocumentPart part : slide.getRelations()){if (part instanceof XSLFChart){PPTGrapthUtils.updateGraph((XSLFChart) part, getGraphData());}}}// 保存文件OutputStream out = new FileOutputStream(destFile);pptx.write(out);out.close();} catch (Exception e){result = e.toString();} finally{if (pptx != null){try{pptx.close();} catch (IOException e){result = e.toString();}}}return result;}private static GraphData getGraphData(){List<SeriesData> dataList = new ArrayList<SeriesData>();SeriesData seriesData = new SeriesData("优", 10);dataList.add(seriesData);seriesData = new SeriesData("中", 150);dataList.add(seriesData);seriesData = new SeriesData("及格", 80);dataList.add(seriesData);seriesData = new SeriesData("不及格", 20);dataList.add(seriesData);GraphData graphData = new GraphData("成绩情况", dataList);return graphData;}}
  4. 控制台输出

  5. 输出后PPT

POI操作ppt图表完整示例演示相关推荐

  1. poi操作ppt图表史上最完整示例演示

    poi操作ppt图表史上最完整示例演示和内嵌excel的获取添加数据简单示例 ,POI3.15版本. 在模板中构造几中基本图表进行测试就行了. 完整下载地址:http://download.csdn. ...

  2. poi 操作 PPT,针对 PPTX--图表篇

    poi 操作 PPT,针对 PPTX–图表篇 文章目录 poi 操作 PPT,针对 PPTX--图表篇 1.读取 PPT 模板 2.替换标题 4.替换图表数据 接下来对 ppt 内的图表进行操作,替换 ...

  3. poi操作PPT文档总结

    POI操作PPT文档1 注意ppt模板不能使用${user}--这样的作为替换标识,在读取PPT模板时会解析成三段文本1.${ 2.user  3.},而一般在控件中使用%1$S这样的方式做为替换标识 ...

  4. java poi 操作ppt

    java poi 操作ppt 可以参考: https://www.w3cschool.cn/apache_poi_ppt/apache_poi_ppt_installation.html http:/ ...

  5. java使用poi操作ppt(导入,导出,读取,添加,拼接,替换文本,页面排序)

    POI操作PPT文档(导入,导出,读取,添加,拼接,替换文本,页面排序) 注意:直接读取.pptx文件时不能使用${user}这样的作为替换标识,在读取.pptx文件时会解析成三段文本"${ ...

  6. Java使用poi操作ppt

    Java使用poi操作ppt https://editor.csdn.net/md/?articleId=117926694 上一篇中写了操作文本框和插入图片 这一篇主要是如何在有模板的情况下如替换文 ...

  7. 【JAVA - POI 合集】之 POI 操作word 图表,柱状图,折线图,雷达图,条形图 poi4.1.2

    1.前言 关于poi 操作word 的吐槽: 山路崎岖, 一言难尽啊!!! 原本项目中的poi 版本是3.17的版本,但是3.17对于在word 中操作图表是有问题的.所以对项目的jar 包进行了升级 ...

  8. poi 操作 PPT,针对 PPTX--文字操作

    poi 操作 PPT,针对 PPTX–文字操作 文章目录 poi 操作 PPT,针对 PPTX--文字操作 1.获取幻灯片内的所有段落文字 2.对幻灯片内的文本内容进行模板替换 3.对段落新增文本 接 ...

  9. 【POI操作word方法大全示例】

    POI操作word方法大全示例总结 提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 POI操作word方法大全示例总结 使用场景 一.poi是什么? 二.使用示例 1.代码 ...

最新文章

  1. ListView分页
  2. 松下SW-9585-C全功能DVD刻录机 狂降100+享受专业刻录!
  3. 【算法】有关点分治的一些理解与看法
  4. 如何查找UI5应用对应在ABAP Netweaver服务器上的BSP应用名称
  5. matlab之norm函数
  6. Flask中的HttpResponse Redirect 和Render
  7. 写随笔写日记多参与评论
  8. 16个 Redis 常见使用场景,面试有内容聊啦
  9. 云台山风景区,感受人生最美的风景
  10. sql 计算周环比wow_Oracle计算环比的方法
  11. 伦敦时间现在几点_英国伦敦现在时间是几点
  12. NX二次开发(C#)-UIStyler-获取UI选择对象
  13. Gartner 发布 2022 年人工智能技术成熟度曲线:复合 AI、决策智能快速发展,因果 AI 是热点
  14. 基于微信小程序的社区垃圾回收管理系统小程序
  15. 互联网+国家战略-整理
  16. 百度SRE面经-为三面攒人品
  17. 记录第一次完整安卓逆向过程笔记
  18. 细思极恐-你真的会写java吗?
  19. 名字竞技场 V3.0
  20. 3DMAX2016安装教程【图文】

热门文章

  1. BTCC受DDoS敲诈攻击nbsp;现赎金涨…
  2. dvwa之 file upload (low)一句话木马和中国菜刀
  3. 常用SQL注入语句大全
  4. 微信群发工具,只能在自己电脑上使用(基于wxpy库)
  5. springboot整合redis后整合es,报错Failed to instantiate [org.elasticsearch.client.transport.TransportClient]
  6. 发展最快的小程序:甩甩宝宝
  7. Oracle数据库基本常用命令
  8. G - 阿牛的EOF牛肉串
  9. aizu-0558 cheese
  10. python实现二维码识别软件_用 Python 生成 识别二维码