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

本文提供了一个使用Apache POI操作PPT图表的完整示例,包括更新图表数据、判断图表类型等功能。示例代码展示了如何刷新柱状图、饼图、线性图和面积图的数据。
摘要由CSDN通过智能技术生成

poi操作ppt图表史上最完整示例演示和内嵌excel的获取添加数据简单示例

,POI3.15版本. 在模板中构造几中基本图表进行测试就行了.

完整下载地址:http://download.csdn.net/detail/mike_caoyong/9841635

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

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. pptl图表操作公共类

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)

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值