我正在使用 Apache POI 4.01 并尝试使用带有单个折线图作为模板的 powerpoint 幻灯片,然后使用它来生成图表中包含一些不同值的新幻灯片,但我得到的是相同的幻灯片复制而没有任何值更改. 请帮忙。
我正在使用PowerPoint幻灯片中的简单折线图尝试以下代码。
public static void main(String[] args) throws IOException {
// creating presentation
XMLSlideShow ppt = new XMLSlideShow();
File file1 = new File("MyDrive://LineSample.pptx");
FileInputStream inputstream;
try {
inputstream = new FileInputStream(file1);
XMLSlideShow template = new XMLSlideShow(inputstream);
XMLSlideShow testReport = new XMLSlideShow();
XSLFSlide xslfSlide = template.getSlides().get(0);
// TODO Auto-generated method stub
// adding slides to the slideshow
XSLFSlide slide1 = testReport.createSlide();
XSLFSlideLayout src_sl = xslfSlide.getSlideLayout();
XSLFSlideMaster src_sm = xslfSlide.getSlideMaster();
XSLFSlideLayout new_sl = slide1.getSlideLayout();
XSLFSlideMaster new_sm = slide1.getSlideMaster();
// copy source layout to the new layout
new_sl.importContent(src_sl);
// copy source master to the new master
new_sm.importContent(src_sm);
slide1.importContent(xslfSlide);
XSLFSlide slide = xslfSlide;
// find chart in the slide
XSLFChart chart = null;
for (POIXMLDocumentPart part : slide.getRelations()) {
if (part instanceof XSLFChart) {
chart = (XSLFChart) part;
break;
}
}
if (chart == null) {
throw new IllegalStateException("chart not found in the template");
} else {
System.out.println("Chart Found");
}
// Series Text
List<XDDFChartData> series = chart.getChartSeries();
XDDFLineChartData linechart = (XDDFLineChartData) series.get(0);
// Category
List<String> listCategories = new ArrayList<>(3);
listCategories.add("Test1");
listCategories.add("Test2");
listCategories.add("Test3");
// Values
List<Double> listValues = new ArrayList<>(3);
listValues.add(10.00);
listValues.add(20.00);
listValues.add(30.00);
String[] categories = listCategories.toArray(new String[listCategories.size()]);
Double[] values = listValues.toArray(new Double[listValues.size()]);
final int numOfPoints = categories.length;
final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));
final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1));
final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange);
final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values,
valuesDataRange);
XDDFLineChartData.Series firstSeries = (XDDFLineChartData.Series) linechart.getSeries().get(0);
firstSeries.replaceData(categoriesData, valuesData);
// firstSeries.setTitle("chartTitle",
// chart.setSheetTitle("chartTitle", 0));
firstSeries.setMarkerSize((short) 70);
firstSeries.setMarkerStyle(MarkerStyle.DASH);
firstSeries.setShowLeaderLines(true);
firstSeries.setSmooth(true);
// firstSeries.setShapeProperties(XDDFShapeProperties);
chart.plot(linechart);
FileOutputStream out = new FileOutputStream("MyDrive");
testReport.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
没有错误但图表值保持不变,实际需要的结果是具有不同值的折线图。