Excel 格式化单元格

可用性是一个术语,它描述了一个人与程序、网站或设备的交互能力。提高可用性的因素包括易学性和使用效率。您可以使用颜色以多种方式提高电子表格的可用性。例如,如果您需要识别电子表格中值超过 1000 的所有单元格,您可以通过告诉 Excel 将这些单元格涂成红色来简化这项任务。然后人们可以在几秒钟内识别它们。通过电子表格的行颜色,您可以使其具有专业外观并使其更易读。此外,当您希望工作簿中的所有标题看起来都一样时,样式非常有用。

颜色还可以帮助您更有效地可视化数据,因为您可以通过视觉识别相关信息组。此外,您可以使用样式来帮助您的工作表和工作簿包含一致的格式。

用于工作表处理的云 API

Aspose.Cells Cloud API 提供创建和操作本地系统以及云上托管的 MS Excel 和 OpenOffice 电子表格的功能。为了处理工作表,您无需安装任何软件(包括 MS Office 或 OpenOffice),所有处理均使用 Cloud API 执行。请注意,随着每个新版本的发布,我们都努力为产品带来更高的稳定性,并努力引入新功能,使我们的 API 更加强大。因此,在最近发布的 Aspose.Cells Cloud 20.7 中,我们显著改进了与工作簿创建、设置单元格特征、获取单元格范围值和发布单元格样式相关的功能。

使用 C# 创建工作簿

该 API 提供了使用更少代码行创建 Excel 工作表的功能。即使只使用一行代码,也可以将新工作表添加到现有 Excel 工作簿中。下面的代码片段显示了创建示例 Excel 工作簿、在第二个索引处添加 Excel 工作表以及将结果文件保存到云存储的步骤。

string MyAppKey = "xxxxxxxx";    // Get AppKey from https://dashboard.aspose.cloud/
string MyAppSid = "xxxxxxxxx";   // Get AppSID from https://dashboard.aspose.cloud/
// 完整的示例可以在 https://github.com/aspose-cells-cloud/aspose-cells-cloud-dotnet 上找到
// 创建 Cells Cloud API 实例
CellsApi instance = new CellsApi(MyAppSid, MyAppKey);
// 指定结果文件的名称
string name = "NewBook" + DateTime.Now.ToString("yymmddhhmiss") + ".xlsx";
// 将 Excel 文件保存至云存储
instance.CellsWorkbookPutWorkbookCreate(name);
// 将工作表添加到第二个位置
instance.CellsWorksheetsPutAddNewWorksheet(name, "Sheet2", 2);

获取单元格范围值

API 提供根据命名范围获取、添加或更新单元格数据的功能。在最近的版本中,API 已得到改进,可以从工作表单元格中获取范围值。

string MyAppKey = "xxxxxxxx";    // Get AppKey from https://dashboard.aspose.cloud/
string MyAppSid = "xxxxxxxxx";   // Get AppSID from https://dashboard.aspose.cloud/
// 完整的示例可以在 https://github.com/aspose-cells-cloud/aspose-cells-cloud-dotnet 上找到
// 创建 Cells Cloud API 实例
CellsApi instance = new CellsApi(MyAppSid, MyAppKey);
string name = "NewBook2056160256i53.xlsx";
string sheetName = "Sheet1";
int? firstRow = 0;
int? firstColumn = 0;
int? rowCount = 3;
int? columnCount = 2;
var response = instance.CellsRangesGetWorksheetCellsRangeValue(name, sheetName, null, firstRow, firstColumn, rowCount, columnCount);
NUnit.Framework.Assert.AreEqual(response.Code, 200);

var rangeName = "A1:B3";
response = instance.CellsRangesGetWorksheetCellsRangeValue(name, sheetName, rangeName, null, null, null, null);
NUnit.Framework.Assert.AreEqual(response.Code, 200);

rangeName = "MyRange";
response = instance.CellsRangesGetWorksheetCellsRangeValue(name, sheetName, rangeName, null, null, null, null);
NUnit.Framework.Assert.AreEqual(response.Code, 200);

将富文本格式应用于单元格

单个单元格包含数据,为了正确区分每个单元格的数据,可以为每个单元格应用特定的格式样式。该 API 还支持为 Excel 工作表单元格设置富文本格式的功能。该 API 提供 Font 类,该类提供为特定单元格指定字体信息的功能。您可以设置粗体、斜体、删除线、下标、上标、下划线、大小、字体名称等格式。

string MyAppKey = "xxxxxxxx";    // Get AppKey from https://dashboard.aspose.cloud/
string MyAppSid = "xxxxxxxxx";   // Get AppSID from https://dashboard.aspose.cloud/
// 完整的示例可以在 https://github.com/aspose-cells-cloud/aspose-cells-cloud-dotnet 上找到

// 创建 Cells Cloud API 实例
Aspose.Cells.Cloud.SDK.Api.CellsApi cellsApi = new Aspose.Cells.Cloud.SDK.Api.CellsApi(MyAppSid, MyAppKey);

string fileName = "NewBook2056160256i53.xlsx";
String sheetName = "Sheet1";
String cellName = "A3";

Aspose.Cells.Cloud.SDK.Model.Style style = new Aspose.Cells.Cloud.SDK.Model.Style();
Aspose.Cells.Cloud.SDK.Model.Font font = new Aspose.Cells.Cloud.SDK.Model.Font();
font.IsBold = true;
font.Color = new Aspose.Cells.Cloud.SDK.Model.Color() { A = 10, R = 120, G = 200, B = 230 };
font.Size = 16;

Aspose.Cells.Cloud.SDK.Model.ThemeColor themeColor = new Aspose.Cells.Cloud.SDK.Model.ThemeColor();
themeColor.ColorType = "Text2";
themeColor.Tint = 2;
style.BackgroundThemeColor = themeColor;
style.Font = font;            

try
{
    // 调用 Aspose.Cells Cloud SDK API 更改单元格样式
    Aspose.Cells.Cloud.SDK.Model.StyleResponse apiResponse = cellsApi.CellsPostUpdateWorksheetCellStyle(fileName, sheetName, cellName, style);

    if (apiResponse != null && apiResponse.Status.Equals("OK"))
    {
        Console.WriteLine("Change Cell Style in Excel Worksheet, Done!");
        Console.ReadKey();
    }
}
catch (Exception ex)
{
    System.Diagnostics.Debug.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}
代码执行后更新 Excel 单元格格式

图 1:代码执行后单元格格式更新。

相关文章

我们还建议您访问以下文章以获取更多信息: