Java编辑PPT的柱状图,与内嵌的Excel联动

/**
* 条形图;柱形图 的数据写入方法
* @param slide 图表
* @param index 柱状图的下标
* @param data 要填充的数据
* @param titles 内嵌Excel的X轴title
* @param columnTitles 内嵌Excel的Y轴title
* @return
*/
public static XSLFSlide setBarChart(XSLFSlide slide, int index, List<List<String>> data, List<String> titles,
List<String> columnTitles) {
XSLFChart chart = setChartData(slide, index);
List<POIXMLDocumentPart> partList = chart.getRelations();
POIXMLDocumentPart xlsPart = null;
// 获取到内嵌的Excel
for (int i = 0; i < partList.size(); i++) {
xlsPart = partList.get(i);
if (xlsPart.getPackagePart().getPartName().getName().endsWith("xlsx")) {
break;
}
}

CTChart ctChart = chart.getCTChart();
CTPlotArea plotArea = ctChart.getPlotArea();
CTBarChart barChart = null;
try {
barChart = plotArea.getBarChartArray(0);
} catch (Exception e) {
log.error("不存在柱状图或者不是柱状图!" + e);
}

List<CTBarSer> serList = barChart.getSerList();
// 如果模板中的列数与Excel数据中的列不对等
if (serList.size() > columnTitles.size()) {
serList = serList.subList(0, columnTitles.size());
} else if (serList.size() < columnTitles.size()) {
for (int i = 0; i < (columnTitles.size() - serList.size()); i++) {
serList.add(serList.get(0));
}
}
Object[] objArr = serList.toArray();
CTBarSer[] serArr = new CTBarSer[serList.size()];
for (int i = 0; i < objArr.length; i++) {
if (serList.toArray()[i] instanceof CTBarSer) {
serArr[i] = (CTBarSer) objArr[i];
}
}
barChart.setSerArray(serArr);
// 把图表绑定到Excel workbook中
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
// 创建列标题行
Row lineTitle = sheet.createRow(0);
// 获取行数
CTStrData strData = serList.get(0).getCat().getStrRef().getStrCache();
strData.setPtArray(null);
// 创建行数和行标题
for (int i = 0; i < titles.size(); i++) {
lineTitle.createCell(i + 1).setCellValue(titles.get(i));
CTStrVal sVal = strData.addNewPt();
sVal.setIdx(i);
sVal.setV(titles.get(i));
}
for (int rows = 0; rows < columnTitles.size(); rows++) {
sheet.createRow(rows + 1).createCell(0).setCellValue(columnTitles.get(rows));
}
// ser对应一列
for (int i = 0; i < serList.size(); i++) {
CTBarSer ser = serList.get(i);
CTSerTx tx = ser.getTx();
tx.getStrRef().getStrCache().getPtArray(0).setV(columnTitles.get(i));
// 关联列标题
String titleRef = new CellReference(sheet.getSheetName(), i + 1, 0, true, true).formatAsString();
serList.get(i).getTx().getStrRef().setF(titleRef);
// 写入数据
CTNumData numData = serList.get(i).getVal().getNumRef().getNumCache();
// 清除旧的数据值
numData.setPtArray(null);
// 一格
for (int j = 0; j < titles.size(); j++) {
CTNumVal numVal = numData.addNewPt();
numVal.setIdx(j);
numVal.setV("" + getDataDouble(data, j, i));
// 写入workbook
Row row = sheet.getRow(i + 1);
Cell cell = row.createCell(j + 1);
cell.setCellValue(getDataDouble(data, j, i));
}
numData.getPtCount().setVal(titles.size());
strData.getPtCount().setVal(titles.size());
// 设置行标题和数据关联
String numDataRange = new CellRangeAddress(i + 1, i + 1, 1, data.size()).formatAsString(sheet.getSheetName(), true);
serList.get(i).getVal().getNumRef().setF(numDataRange);
}
// 关联行标题
String axisDataRange = new CellRangeAddress(0, 0, 1, data.size()).formatAsString(sheet.getSheetName(), true);
serList.get(0).getCat().getStrRef().setF(axisDataRange);
// 更新嵌入的workbook
updateWorkbook(xlsPart, wb);
return slide;
}

/**
* 更新嵌入的workbook
* @param ppt
* @param xlsPart
* @param wb
* @return
*/
private static void updateWorkbook(POIXMLDocumentPart xlsPart, XSSFWorkbook wb) {
// 更新嵌入的workbook
OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream();
try {
wb.write(xlsOut);
} catch (IOException e) {
log.error("PPTUtil.updateWorkbook() :" + e);
} finally {
try {
xlsOut.close();
} catch (IOException e) {
log.error("PPTUtil.updateWorkbook() :" + e);
}
}
}

上一篇:undefined is not an object (evaluating 'RNFetchBlob.DocumentDir')


下一篇:python异常处理try,except,else,finally,raise