Buat Microsoft® Bagan Excel dan Konversi ke Gambar melalui C++

Ubah bagan dokumen Excel menjadi gambar serta buat bagan termasuk bagan Pai, Piramida, Garis, dan Gelembung dalam aplikasi berbasis C++.

 

Dengan menggunakan grafik Excel, seseorang bisa mendapatkan gambaran yang lebih besar dan menganalisis data dengan mudah untuk mengambil keputusan yang tepat. C++ Perpustakaan Excel mendukung pembuatan bagan berbeda yang dicantumkan oleh enum Aspose::Cells::Bagan::Jenis Bagan termasuk diagram area, batang, pai, piramida, garis, dan gelembung. Selain itu, Untuk konversi grafik menjadi gambar, API menyediakan a Untuk Gambar mehtod ke dalam format gambar yang diperlukan.

Buat Bagan Excel

Proses membuat bagan Excel adalah, membuat instance dari Kelas buku kerja dan pilih yang diinginkan Lembar kerja . Tambahkan grafik menggunakan Tambahkan metode dengan parameter yang relevan termasuk jenis bagan. Akses grafik melalui indeks dan Menambahkan sumber data untuk bagan.

C++ Kode untuk Membuat Grafik 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();

Konversi Bagan menjadi Gambar

Untuk proses konversi bagan, pertama-tama buat bagan dengan tipe yang relevan menggunakan kode di atas atau akses dari lembar yang relevan. Tentukan jalur penyimpanan keluaran untuk gambar dan gunakan metode ToImage untuk konversi.

C++ Kode untuk Mengonversi Grafik 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();