使用apache poi从幻灯片(.pptx)保存折线图(基于.xlsx)数据

Saving line chart(.xlsx based) data from slide(.pptx) using apache poi

我想使用 apache poi 保存 excel 文件 (.xlsx),其中包含与 powerpoint 幻灯片 (.pptx) 中折线图对应的数据。

使用当前 apacjhe poi 5.0.0XDDFChart.importContent which imports the chart content from one XDDFChart to another. And there is XDDFChart.getWorkbook 获取 XDDFChart 后面的数据源工作簿。

如果需要将图表从 PowerPoint 中提取到 Excel 文件中,则需要完成以下操作:

PowerPoint 幻灯片中获取 XSLFChart

获取数据源工作簿和该图表后面的数据源sheet。

在 sheet 中创建一个 XSSFChart

XSLFChart.

导入图表内容

将工作簿保存到文件中。

示例:

来源PowerPoint.pptx:

代码:

import java.io.*;

import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.util.Units;

public class CreateExcelXDDFChartFromPowerPointXDDFChart {

 public static void main(String[] args) throws Exception {

  String powerPointPath = "./PowerPoint.pptx";
  String excelPath = "./Excel.xlsx";

  // get the PowerPoint slide show
  XMLSlideShow slideShow = new XMLSlideShow(new FileInputStream(powerPointPath));
  // get first chart
  XSLFChart powerPointchart = slideShow.getCharts().get(0);

  // get data source workbook behind that chart
  XSSFWorkbook workbook = powerPointchart.getWorkbook();
  // get data source sheet
  XSSFSheet sheet = workbook.getSheetAt(0);

  // create the chart in Excel
  XSSFDrawing drawing = sheet.createDrawingPatriarch();
  XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 5, 0, 15, 30);
  XSSFChart excelChart = drawing.createChart(anchor);

  // get chart data from PowerPoint chart
  excelChart.importContent(powerPointchart);
 
  // Write the output to a file
  try (FileOutputStream fileOut = new FileOutputStream(excelPath)) {
   workbook.write(fileOut);
  }

  slideShow.close();
  workbook.close();
 }
}

结果Excel.xlsx:


反之亦然。要将 Excel 图表放入 PowerPoint 滑梯表演,需要完成以下操作:

从给定的 Excel 工作簿中的 sheet 中获取 XSSFChart

创建 创建一个新的 PowerPoint 幻灯片放映或打开一个现有的。

在该幻灯片放映的幻灯片中创建 XSLFChart

XSSFChart.

导入图表内容

Excel 工作簿保存为 PowerPoint 图表的数据源。为此,我们需要 XDDFChart.saveWorkbook 将给定的 XSSFWorkbook 作为该图表的数据源写入嵌入式 Excel 文件。

示例:

给定 Excel.xlsx:

代码:

import java.io.*;

import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.util.Units;

public class CreatePowerPointXDDFChartFromExcelXDDFChart {

 public static void main(String[] args) throws Exception {

  String excelPath = "./Excel.xlsx";
  String powerPointPath = "./PowerPoint.pptx";

  // get the Excel workbook
  XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(excelPath));
  // get first sheet
  XSSFSheet sheet = workbook.getSheetAt(0);
  // get first chart in that sheet
  XSSFDrawing drawing = sheet.createDrawingPatriarch();
  XSSFChart excelChart = drawing.getCharts().get(0);

  // create new PowerPoint slide show
  XMLSlideShow slideShow = new XMLSlideShow();

  // create new slide
  XSLFSlide slide = slideShow.createSlide();

  // create the chart
  XSLFChart powerPointchart = slideShow.createChart();
  // add chart to slide
  slide.addChart(powerPointchart, new java.awt.geom.Rectangle2D.Double(1d*Units.EMU_PER_CENTIMETER, 1d*Units.EMU_PER_CENTIMETER, 20d*Units.EMU_PER_CENTIMETER, 15d*Units.EMU_PER_CENTIMETER));
   
  // get chart data from Excel chart
  powerPointchart.importContent(excelChart);
  // save Excel workbook as PowerPoint chart's data source
  powerPointchart.saveWorkbook(workbook);
 
  // Write the output to a file
  try (FileOutputStream fileOut = new FileOutputStream(powerPointPath)) {
   slideShow.write(fileOut);
  }

  workbook.close();
  slideShow.close();
 }
}

结果 PowerPoint.pptx: