We are trying to create pptx with embedded xlsx file. It has given classcast exception.
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.FileFormatType;
import com.aspose.cells.SheetType;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import com.aspose.slides.OleObjectFrame;
import com.aspose.slides.pptx.OleObjectFrameEx;
import com.aspose.slides.pptx.PictureEx;
import com.aspose.slides.pptx.PresentationEx;
import com.aspose.slides.pptx.SlideEx;
public class Test2007 {
public static void main(String[] args) {
Run();
}
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);
//Save the workbook to stream
ByteArrayOutputStream bout=new ByteArrayOutputStream();
wb.save(bout, FileFormatType.EXCEL2007);
//Create a presentation
PresentationEx pres = new PresentationEx("C:/Documents and Settings/thuksh01/Desktop/newppt.pptx");
SlideEx sld = pres.getSlides().get(0);
System.out.println(sld.getSlideId());
//Add the workbook on slide
AddExcelChartInPresentation(pres, sld, bout.toByteArray(), "C:/Project/export_ws/Templates/Masking.jpg");
//Write the presentation on disk
pres.write(new FileOutputStream("c:\\output.pptx"));
}catch(Exception e){
e.printStackTrace();
}
}
static int AddExcelChartInWorkbook(Workbook wb, int chartRows, int chartCols)
{
//Array of cell names
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
//int dataSheetIdx = wb.getWorksheets().addSheet();
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.COLUMN_CLUSTERED, 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 an active sheet
wb.getWorksheets().setActiveSheet(chartSheetIdx);
return chartSheetIdx;
}
static void AddExcelChartInPresentation(PresentationEx pres, SlideEx sld, byte[] wbArray, String imgChart) throws Exception
{
// PictureEx pic = new PictureEx();
// int picId = pres.getPictures().add(pic);
// int slideWidth = (int)pres.getSlideSize().getX() - 1500;
// int slideHeight = (int)pres.getSlideSize().getY();
// int x = 1500 / 2;
// OleObjectFrame oof = sld.getShapes().addOleObjectFrame(x, 0, slideWidth, slideHeight, "Excel.Sheet.8", wbArray);
// oof.setPictureId(picId);
int slideWidth = (int)pres.getSlideSize().getSize().getWidth() - 1500;
int slideHeight = (int)pres.getSlideSize().getSize().getHeight();
int x = 1500 / 2;
sld.getShapes().insertOleObjectFrame(x, 0, slideWidth, slideHeight, 0, "Excel.Sheet.12", wbArray);
}
}
java.lang.ClassCastException: com.aspose.slides.pptx.GraphicalObjectEx cannot be cast to com.aspose.slides.pptx.OleObjectFrameEx
at com.aspose.slides.pptx.ShapesEx.do(SourceFile:208)
at com.aspose.slides.pptx.ShapesEx.insertOleObjectFrame(SourceFile:268)
at com.nielsen.ascent.reporting.export.test.Test2007.AddExcelChartInPresentation(Test2007.java:133)
at com.nielsen.ascent.reporting.export.test.Test2007.Run(Test2007.java:49)
at com.nielsen.ascent.reporting.export.test.Test2007.main(Test2007.java:21)