图表用于以易于理解的方式反映信息。几乎所有办公室都使用电子表格来管理数据,但它们不太容易阅读。这就是为什么图表(通常称为 Excel 中的图形)有助于呈现数据概览的原因。Aspose.Cells for Cloud API 拥有丰富的图表操作 API。在本文中,我们将学习以下功能:

  • 在 Excel 电子表格中添加图表
  • 删除 Excel 电子表格中的图表
  • 更新 Excel 电子表格中的现有图表

可以使用任何编程语言从任何平台调用 API。有不同的 SDK 可用,包括 Python、PHP、Ruby、Perl、Go、Swift 和 更多。您可以根据自己的方便选择和使用其中任何一种。在这里我们将使用 C# 代码来举例。

在 Excel 电子表格中添加图表

您可以使用 Aspose.Cells Cloud API 在 Excel 工作簿中插入图表或图形。只需指定图表的必要属性,如图表类型、图表区域,然后进行 API 调用。以下是将饼图添加到工作表的示例代码:

// 从 https://dashboard.aspose.cloud/ 获取 AppKey 和 AppSID
// 安装 Nuget 包 Aspose.Cells-Cloud
// 完整示例和数据文件请见https://github.com/aspose-cells-cloud/aspose-cells-cloud-dotnet

CellsApi cellsApi = new CellsApi(AppKey, AppSid);
string name = "myDocument.xlsx";
string sheetName = "SHEET4";
string chartType = "Pie";
int? upperLeftRow = 5;
int? upperLeftColumn = 5;
int? lowerRightRow = 10;
int? lowerRightColumn = 10;
string area = "C7:D11";
bool? isVertical = true;
string categoryData = null;
bool? isAutoGetSerialName = null;
string title = null;
string folder = "TEMPFOLDER";
UpdateDataFile(cellsApi,folder, name);
var response = cellsApi.CellsChartsPutWorksheetAddChart(name, sheetName, chartType, upperLeftRow, upperLeftColumn, lowerRightRow, lowerRightColumn, area, isVertical, categoryData, isAutoGetSerialName, title, folder);

此代码片段将创建一个图表,如以下屏幕截图所示:

在 Excel 中添加图表

从 Excel 电子表格中删除图表

您可以使用 Aspose.Cells Cloud API 从 XLSX 或 XLS 文件中删除或移除现有图表。只需指定文件名、工作表名称和图表索引。随后,调用 API,相应的图表将从工作表中删除。但是,单元格中的数据将保持原样,不会受到影响,只有图表会被删除。以下代码片段可用于相同目的:

// 从 https://dashboard.aspose.cloud/ 获取 AppKey 和 AppSID
// 安装 Nuget 包 Aspose.Cells-Cloud
// 完整示例和数据文件请见https://github.com/aspose-cells-cloud/aspose-cells-cloud-dotnet

CellsApi cellsApi = new CellsApi(AppKey, AppSid);
string name = "myDocument1.xlsx";
string sheetName = "SHEET4";
int? chartIndex = 0;
string folder = "TEMPFOLDER";
UpdateDataFile(cellsApi,folder, name);
var response = cellsApi.CellsChartsDeleteWorksheetDeleteChart(name, sheetName, chartIndex, folder);

下面的屏幕截图显示了输出工作表,其中数据保持不变并且图表已被删除。

更新 Excel 电子表格中的图表

由于我们已经考虑了上述两个图表操作功能,让我们看看更新现有图表的另一个功能。在这里,我们将通过指定 Excel 文件的名称、工作表的名称和图表的索引来更新图表的标题,如以下 C# 代码片段所示:

// 从 https://dashboard.aspose.cloud/ 获取 AppKey 和 AppSID
// 安装 Nuget 包 Aspose.Cells-Cloud
// 完整示例和数据文件请见https://github.com/aspose-cells-cloud/aspose-cells-cloud-dotnet

CellsApi cellsApi = new CellsApi(AppKey, AppSid);
string name = "myDocument.xlsx";
string sheetName = "SHEET3";
int? chartIndex = 0;
Title title = new Title();
title.Text = "Test title";
string folder = "TEMPFOLDER";
UpdateDataFile(cellsApi,folder, name);
var response = cellsApi.CellsChartsPostWorksheetChartTitle(name, sheetName, chartIndex, title, folder);

下面的屏幕截图显示了此代码片段的输出。图表标题与我们指定的相同。

这些只是 API 提供的众多图表操作功能中的一小部分。您可以免费 注册 并全面测试 API。如有任何疑问,请在 免费支持论坛 上写信给我们。

相关文章