I am trying to insert a doughnut chart into the power point presentation. Unfortunately, it crashes when I call Chart.toImage() function (see code below).
package com.citi.aw.reporting.excel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Array;
import com.aspose.cells.Chart;
import com.aspose.cells.ChartType;
import com.aspose.cells.ImageOptions;
import com.aspose.cells.SheetType;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import com.aspose.slides.OleObjectFrame;
import com.aspose.slides.Presentation;
import com.aspose.slides.Slide;
public class TestExcel
{
public static void Run()
{
try
{
// Create a workbook
Workbook wb = new Workbook();
// Add an excel chart
int chartRows = 55;
int chartCols = 25;
int chartSheetIndex = AddExcelChartInWorkbook(wb, chartRows, chartCols);
// Set chart ole size
wb.setOleSize(0, chartRows, 0, chartCols);
final Chart chart = wb.getWorksheets().getSheet(chartSheetIndex).getCharts().getChart(0);
//----------------------------------------------------------------------------------
final int ShapeW = 3 * 576;
final int ShapeH = 3 * 576;
chart.setSizeWithWindow(true);
//set window width of the workbook in inches (divided by 576 as PowerPoint uses
//576 pixels / inch)
wb.setWindowWidthInch(ShapeW/576f);
//set window height of the workbook in inches
wb.setWindowHeightInch(ShapeW/576f);
//Save Workbook to Stream
ByteArrayOutputStream wbStream=new ByteArrayOutputStream();
wb.save(wbStream);
//----------------------------------------------------------------------------------
// Get the chart image and save to stream
ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
chart.toImage(imageStream, new ImageOptions());
// Save the workbook to stream
ByteArrayOutputStream bout = new ByteArrayOutputStream();
wb.save(bout);
// Create a presentation
Presentation pres = new Presentation();
Slide sld = pres.getSlideByPosition(1);
AddExcelChartInPresentation(pres, sld, 0, 0, ShapeW, ShapeH, bout.toByteArray(), imageStream.toByteArray());
pres.write(new FileOutputStream("c:\\temp\\aspose\\outputJava.ppt"));
}
catch(Exception e)
{
e.printStackTrace();
}
}
static int AddExcelChartInWorkbook(Workbook wb, int chartRows, int chartCols)
{
String[] cellsName = new String[]
{
"A1", "A2", "A3", "A4",
"B1", "B2", "B3", "B4",
"C1", "C2", "C3", "C4",
"D1", "D2", "D3", "D4",
"E1", "E2", "E3", "E4"
};
// Array of cell data
int[] cellsValue = new int[]
{
67, 86, 68, 91,
44, 64, 89, 48,
46, 97, 78, 60,
43, 29, 69, 26,
24, 40, 38, 25
};
// Add a new worksheet to populate cells with data
Worksheet dataSheet = wb.getWorksheets().addSheet();
String sheetName = "DataSheet";
dataSheet.setName(sheetName);
// Populate DataSheet with data
int size = Array.getLength(cellsName);
for(int i = 0; i < size; i++)
{
String cellName = cellsName[i];
int cellValue = cellsValue[i];
dataSheet.getCells().getCell(cellName).setValue(cellValue);
}
// Add a chart sheet
Worksheet chartSheet = wb.getWorksheets().addSheet(SheetType.CHART);
chartSheet.setName("ChartSheet");
int chartSheetIdx = chartSheet.getIndex();
// Add a chart in ChartSheet with data series from DataSheet
Chart chart = chartSheet.getCharts().addChart(ChartType.DOUGHNUT, 0, chartRows, 0, chartCols);
chart.getNSeries().add(sheetName + "!A1:E1", false);
// chart.getNSeries().add(sheetName + "!A2:E2", false);
// chart.getNSeries().add(sheetName + "!A3:E3", false);
// chart.getNSeries().add(sheetName + "!A4:E4", false);
// Set ChartSheet as active sheet
wb.getWorksheets().setActiveSheet(chartSheetIdx);
return chartSheetIdx;
}
static void AddExcelChartInPresentation(Presentation pres, Slide sld, int x, int y, int w, int h, byte[] wbArray, byte[] imgChart)
throws Exception
{
com.aspose.slides.Picture pic = new com.aspose.slides.Picture(pres, new ByteArrayInputStream(imgChart));
int picId = pres.getPictures().add(pic);
OleObjectFrame oof = sld.getShapes().addOleObjectFrame(x, y, w, h, "Excel.Sheet.8", wbArray);
oof.setPictureId(picId);
}
public static void main(String[] args)
{
Run();
}
}