支持在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);// 总列数
// 显示标题行