Cree gráficos de Excel Microsoft® y conviértalos en imágenes mediante C++
Convierta gráficos de documentos de Excel en imágenes y cree gráficos que incluyen gráficos circulares, piramidales, de líneas y de burbujas dentro de aplicaciones basadas en C++.
Con los gráficos de Excel, se puede obtener una visión más amplia y analizar los datos fácilmente para tomar las decisiones correctas. C++ Biblioteca de Excel admite la creación de diferentes gráficos enumerados por enumeración Aspose::Cells::Gráficos::Tipo de gráfico incluyendo gráficos de áreas, barras, circulares, piramidales, de líneas y de burbujas. Además, para la conversión de gráficos a imágenes, API proporciona una A la imagen método en el formato de imagen requerido.
Crear gráficos de Excel
El proceso de creación de un gráfico de Excel es crear una instancia del clase de libro de trabajo y seleccione el deseado Hoja de cálculo . Agregue el gráfico usando Agregar método con parámetros relevantes, incluido el tipo de gráfico. Acceda al gráfico a través del índice y Agregar la fuente de datos para el gráfico.
C++ Código para crear gráficos de Excel
Aspose::Cells::Startup(); | |
// Path of output XLSM file | |
U16String outputChartTypeCustom = u"sourceFile.xlsm"; | |
// Create a new workbook | |
Workbook wkb; | |
// Get first worksheet which is created by default | |
Worksheet wks = wkb.GetWorksheets().Get(0); | |
// Adding sample values to cells | |
wks.GetCells().Get(u"A1").PutValue(50); | |
wks.GetCells().Get(u"A2").PutValue(100); | |
wks.GetCells().Get(u"A3").PutValue(150); | |
wks.GetCells().Get(u"B1").PutValue(4); | |
wks.GetCells().Get(u"B2").PutValue(20); | |
wks.GetCells().Get(u"B3").PutValue(50); | |
// Adding a chart to the worksheet | |
int chartIndex = wks.GetCharts().Add(Aspose::Cells::Charts::ChartType::Column, 5, 0, 20, 8); | |
// Accessing the instance of the newly added chart | |
Chart chart = wks.GetCharts().Get(chartIndex); | |
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3" | |
chart.GetNSeries().Add(u"A1:B3", true); | |
// Saving the ODS file | |
wkb.Save(outputChartTypeCustom); | |
Aspose::Cells::Cleanup(); |
Convertir gráficos en imágenes
Para el proceso de conversión de gráficos, primero cree un gráfico del tipo relevante utilizando el código anterior o acceda a él desde la hoja correspondiente. Defina la ruta para guardar la salida de la imagen y utilice el método ToImage para la conversión.
C++ Código para convertir gráficos de Excel
Aspose::Cells::Startup(); | |
// Output directory path | |
U16String outDir = u"..\\OutputDirectory\\"; | |
// Path of output image file | |
U16String outputChartImage = outDir + u"out1image.png"; | |
// Create a new workbook | |
Workbook wkb; | |
// Get first worksheet which is created by default | |
Worksheet wks = wkb.GetWorksheets().Get(0); | |
// Adding sample values to cells | |
wks.GetCells().Get(u"A1").PutValue(50); | |
wks.GetCells().Get(u"A2").PutValue(100); | |
wks.GetCells().Get(u"A3").PutValue(150); | |
wks.GetCells().Get(u"B1").PutValue(4); | |
wks.GetCells().Get(u"B2").PutValue(20); | |
wks.GetCells().Get(u"B3").PutValue(50); | |
// Adding a chart to the worksheet | |
int chartIndex = wks.GetCharts().Add(Aspose::Cells::Charts::ChartType::Column, 5, 0, 20, 8); | |
// Accessing the instance of the newly added chart | |
Chart chart = wks.GetCharts().Get(chartIndex); | |
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3" | |
chart.GetNSeries().Add(u"A1:B3", true); | |
// Saving the chart to image file | |
chart.ToImage(outputChartImage, ImageType::Png); | |
Aspose::Cells::Cleanup(); |