Excel 格式化儲存格

可用性是一個術語,描述人與程式、網站或裝置互動的程度。提高可用性的因素包括易於學習和使用效率。您可以透過多種方式使用顏色來提高電子表格的可用性。例如,如果您需要識別電子表格中值超過 1000 的所有儲存格,則可以透過告訴 Excel 將這些儲存格標記為紅色來簡化該任務。人們可以在幾秒鐘內識別它們。透過電子表格的行顏色,您可以使其看起來更專業並且更清晰。此外,當您希望工作簿中的所有標題看起來相同時,樣式很有用。

顏色也能讓您透過視覺識別相關資訊群組,從而幫助您更有效地視覺化資料。此外,您可以使用樣式來幫助您的工作表和工作簿包含一致的格式。

用於工作表處理的雲端 API

Aspose.Cells Cloud API提供建立和操作本機系統以及雲端託管的 MS Excel 和 OpenOffice 電子表格的功能。為了處理工作表,您不需要安裝任何軟體,包括 MS Office 或 OpenOffice,所有處理都是使用雲端 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:程式碼執行後儲存格格式更新。

相關文章

我們還建議您訪問以下文章以獲取更多資訊: