Created
June 15, 2025 17:10
-
-
Save aspose-cloud-kb/ec6422f920cb0e63767b06978438d029 to your computer and use it in GitHub Desktop.
Convert Excel Chart to Image using C# REST API. https://kb.aspose.cloud/cells/net/convert-excel-chart-to-image-using-net-rest-api/
This file contains hidden or 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
using Aspose.Cells.Cloud.SDK.Api; | |
using Aspose.Cells.Cloud.SDK.Request; | |
class ExcelChartExporter | |
{ | |
static void Main() | |
{ | |
try | |
{ | |
// Initialize API credentials and endpoint | |
string appKey = "Application Key"; | |
string appSecret = "Application Secret"; | |
string serviceBaseUrl = "https://api.aspose.cloud"; | |
// Create an instance of the Aspose.Cells API client | |
CellsApi cellsClient = new CellsApi(appKey, appSecret, serviceBaseUrl); | |
// Define local Excel file path | |
string spreadsheetFile = "charts.xlsx"; | |
// Prepare and execute file upload request | |
var fileUpload = new UploadFileRequest | |
{ | |
path = spreadsheetFile, | |
UploadFiles = new Dictionary<string, Stream> | |
{ | |
{ spreadsheetFile, File.OpenRead(spreadsheetFile) } | |
} | |
}; | |
cellsClient.UploadFile(fileUpload); | |
// Setup parameters for exporting chart as an image | |
var chartExportRequest = new GetWorksheetChartRequest | |
{ | |
name = spreadsheetFile, | |
sheetName = "Sheet1", | |
chartNumber = 0, | |
folder = "", // Root folder | |
format = "PNG", // Export format | |
storageName = "" // Default storage | |
}; | |
// Fetch the chart image stream from the API | |
var chartImageStream = cellsClient.GetWorksheetChart(chartExportRequest); | |
// Save the image stream to a file | |
string chartImageOutput = "chart_output.png"; | |
using (FileStream outputFileStream = new FileStream(chartImageOutput, FileMode.Create, FileAccess.Write)) | |
{ | |
chartImageStream.CopyTo(outputFileStream); | |
} | |
Console.WriteLine("The chart has been successfully converted to an image."); | |
} | |
catch (Exception error) | |
{ | |
Console.WriteLine("Something went wrong: " + error.Message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment