标签:填充 err instance ram throw length void error i++
/**
* 折线图的数据写入方法
* @param slide ppt图表
* @param index 折线图的下标
* @param data 需要填充的数据
* @param titles 内嵌Excel的X轴title
* @param columnTitles 内嵌Excel的Y轴title
* @return
* @throws IOException
*/
@SuppressWarnings("unused")
public static XSLFSlide setLineChart(XSLFSlide slide, int index, List<List<String>> data, List<String> titles,
List<String> columnTitles) throws IOException {
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();
CTLineChart lineChart = null;
try {
lineChart = plotArea.getLineChartArray(0);
} catch (Exception e) {
log.error("异常:" + e);
}
if (lineChart == null) {
return slide;
}
List<CTLineSer> serList = lineChart.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();
CTLineSer[] serArr = new CTLineSer[serList.size()];
for (int i = 0; i < objArr.length; i++) {
if (serList.toArray()[i] instanceof CTLineSer) {
serArr[i] = (CTLineSer) objArr[i];
}
}
lineChart.setSerArray(serArr);
// 把图表绑定到Excel workbook中
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
// 创建列标题行
Row lineTitle = sheet.createRow(0);
int rowNum = sheet.getLastRowNum();
// 创建并设置列标题行
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));
}
for (int i = 0; i < serList.size(); i++) {
CTLineSer 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();
tx.getStrRef().setF(titleRef);
// 写入数据
CTNumData numData = serList.get(i).getVal().getNumRef().getNumCache();
// 清除旧的数据值
numData.setPtArray(null);
// 每一格
for (int j = 0; j < data.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(data.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);
}
}
}
标签:填充 err instance ram throw length void error i++
原文地址:https://www.cnblogs.com/wupangzio/p/9995951.html