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)