Création d'un graphique Excel et intégration dans la présentation en tant qu'objet OLE
Contents
[
Hide
]
Dans les diapositives PowerPoint, l’utilisation de graphiques modifiables pour l’affichage graphique des données est une activité courante. Aspose fournit le support de la création de graphiques Excel à l’aide d’Aspose.Cells pour Java et ces graphiques peuvent ensuite être intégrés en tant qu’objet OLE dans la diapositive PowerPoint via Aspose.Slides pour Java. Cet article couvre les étapes nécessaires ainsi que l’implémentation en Java pour créer et intégrer un graphique MS Excel en tant qu’objet OLE dans une présentation PowerPoint en utilisant Aspose.Cells pour Java et Aspose.Slides pour Java.
Étapes Nécessaires
La séquence suivante d’étapes est requise pour créer et intégrer un graphique Excel en tant qu’objet OLE dans la diapositive PowerPoint :
Créer un graphique Excel en utilisant Aspose.Cells pour Java.
Définir la taille OLE du graphique Excel en utilisant Aspose.Cells pour Java.
Obtenir l’image du graphique Excel avec Aspose.Cells pour Java.
Intégrer le graphique Excel en tant qu’objet OLE dans la présentation PPTX en utilisant Aspose.Slides pour Java.
Remplacer l’image de l’objet modifié par l’image obtenue à l’étape 3 pour résoudre le problème d’objet modifié.
Enregistrer la présentation de sortie sur le disque au format PPTX.
Implémentation des Étapes Nécessaires
L’implémentation des étapes ci-dessus en Java est comme suit :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.getWorksheets().setOleSize(0, chartRows, 0, chartCols); | |
//Get the chart image and save to stream | |
com.aspose.cells.ImageOrPrintOptions opts= new com.aspose.cells.ImageOrPrintOptions(); | |
opts.setImageFormat(com.aspose.cells.ImageFormat.getPng()); | |
ByteArrayOutputStream imageStream=new ByteArrayOutputStream(); | |
wb.getWorksheets().get(chartSheetIndex).getCharts().get(0).toImage(imageStream, opts); | |
//Save the workbook to stream | |
ByteArrayOutputStream bout=new ByteArrayOutputStream(); | |
wb.save(bout,com.aspose.cells.SaveFormat.EXCEL_97_TO_2003); | |
//Create a presentation | |
Presentation pres = new Presentation(); | |
ISlide sld = pres.getSlides().get_Item(0); | |
//Add the workbook on slide | |
AddExcelChartInPresentation(pres, sld, bout.toByteArray(), imageStream.toByteArray()); | |
//Write the presentation to disk | |
pres.save("outputJ.pptx", SaveFormat.Pptx); | |
} catch (Exception e) { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void AddExcelChartInPresentation(Presentation pres, ISlide sld, byte[] wbArray, byte[] imgChart) throws Exception | |
{ | |
double oleHeight = pres.getSlideSize().getSize().getHeight(); | |
double oleWidth = pres.getSlideSize().getSize().getWidth(); | |
Workbook wb=new Workbook(); | |
//Createing and EXCEL_97_TO_2003 LoadOptions object | |
com.aspose.cells.LoadOptions loadOptions = new com.aspose.cells.LoadOptions(com.aspose.cells.FileFormatType.EXCEL_97_TO_2003); | |
wb=new Workbook(new ByteArrayInputStream(wbArray),loadOptions); | |
IOleObjectFrame oof = sld.getShapes().addOleObjectFrame(0f, 0f, (float)oleWidth, (float)oleHeight, "Excel.Sheet.8", wbArray); | |
oof.getSubstitutePictureFormat().getPicture().setImage(pres.getImages().addImage(new ByteArrayInputStream(imgChart))); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 dataSheetIndex =wb.getWorksheets().add(); | |
Worksheet dataSheet =wb.getWorksheets().get(dataSheetIndex); | |
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().get(cellName).setValue(cellValue); | |
} | |
//Add a chart sheet | |
int WorksheetIndex = wb.getWorksheets().add(SheetType.CHART); | |
Worksheet chartSheet = wb.getWorksheets().get(WorksheetIndex); | |
chartSheet.setName("ChartSheet"); | |
int chartSheetIdx = chartSheet.getIndex(); | |
//Add a chart in ChartSheet with data series from DataSheet | |
int chIndex = chartSheet.getCharts().add(ChartType.COLUMN, 0, chartRows, 0, chartCols); | |
Chart chart=chartSheet.getCharts().get(chIndex); | |
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().setActiveSheetIndex(chartSheetIdx); | |
return chartSheetIdx; | |
} |
La présentation créée par la méthode ci-dessus comportera le graphique Excel en tant qu’objet OLE qui peut être activé en double-cliquant sur le cadre de l’objet OLE.
Conclusion
En utilisant Aspose.Cells pour Java avec Aspose.Slides pour Java, nous pouvons créer n’importe lequel des graphiques Excel pris en charge par Aspose.Cells pour Java et intégrer le graphique créé en tant qu’objet OLE dans une diapositive PowerPoint. La taille OLE du graphique Excel peut également être définie. Les utilisateurs finaux peuvent ensuite modifier le graphique Excel comme tout autre objet OLE.
Sections Connexes
Solution Fonctionnelle pour le Redimensionnement de Graphiques