Excel 在處理數據分析、報告和計算方面被廣泛認可。然而,隨著電子表格的複雜性增長,其檔案大小可能變得難以管理。大型檔案會消耗儲存空間,減慢系統速度,並在共享或協作時造成困難。一個可靠的解決方案是壓縮 Excel 工作簿,使其變得更小且更易於管理——而不會失去準確性或功能。在本教程中,我們將探討如何在 C# .NET 中利用 Aspose.Cells Cloud SDK 和 REST API 減少 Excel 檔案大小。

API for Excel File Compression

減少工作簿大小的一個有效方法是利用 Aspose.Cells Cloud API。這個 API 簡化了雲端中的 Excel 管理任務,包括文件壓縮。它使您能夠應用各種壓縮算法並調整壓縮級別,提供靈活性和精確性。由於它是一個基於雲的解決方案,您可以在任何地方執行壓縮,而不需要安裝額外的軟體。

Aspose.Cells Cloud SDK for .NET 處理低級操作,讓您可以專注於在應用程式中構建功能。現在,為了開始,從 NuGet 套件管理器中搜索 Aspose.Cells-Cloud,然後在 Cloud Dashboard 上創建一個免費帳戶。

使用 C# 縮小 Excel 工作簿

這裡有一個簡單的範例,演示如何在 C# .NET 中壓縮 Excel 工作簿:

var cellsApi = new CellsApi(config);

創建一個 cellsApi 類的對象,並將 Configuration 對象作為參數傳遞。

var request = new PostCompressRequest(...);

建立請求實例以壓縮工作簿。

var response = cellsApi.PostCompress(request);

呼叫 API 以壓縮 Excel 檔案並將結果檔案儲存到雲端儲存。

// 完整的範例和數據文件,請前往 
https://github.com/aspose-cells-cloud/aspose-cells-cloud-dotnet/

// 從 https://dashboard.aspose.cloud/ 獲取客戶端憑證
var config = new Configuration
{
    ClientId = "XXXXXXXX",
    ClientSecret = "XXXXX-XXXX-XXXXX-XXXXXX"
};

var cellsApi = new CellsApi(config);

// Excel 文件名稱儲存在雲端儲存中
string workbookName = "sample.xlsx";
string folder = "input"; // Folder in cloud storage where the file is located
string outPath = "output/compressed-sample.xlsx";

try
{
    // 建立請求以壓縮工作簿
    var request = new PostCompressRequest(
    name: workbookName,
    folder: folder,
    outPath: outPath
);

// 執行壓縮
var response = cellsApi.PostCompress(request);

Console.WriteLine("Workbook compressed successfully.");
Console.WriteLine("Compressed file saved at: " + outPath);
}
catch (Exception ex)
{
    Console.WriteLine("Error: " + ex.Message);
}

使用 cURL 命令壓縮 XLS

另外,您可以使用 cURL 命令與 Aspose.Cells Cloud API 自動化工作簿壓縮。這種方法對於腳本編寫和批量工作流程非常有用。

首先,使用您的客戶端憑證生成訪問令牌:

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=XXXXXX-XXXXX-XXXXX-be35-ff5c3a6aa4a2&client_secret=XXXXXXXXXXX" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

然後,執行以下 cURL 命令將 XLS 壓縮到更小的尺寸:

curl -v "https://api.aspose.cloud/v3.0/cells/compress?CompressLevel=1&checkExcelRestriction=true" \
-X POST \
-H  "accept: application/json" \
-H  "authorization: Bearer {accessToken}" \
-H  "Content-Type: multipart/form-data" \
-d  "File":{"excelFile"}

Replace:

  • {excelFile} - 輸入 Excel 檔案的名稱。
  • {accessToken} - 上述生成的訪問令牌。
  • 若要下載結果壓縮檔案,請嘗試使用 -o 參數。

結論

壓縮 Excel 檔案是一種實用的方法,可以優化儲存空間、減少帶寬使用和提升協作。無論您使用 .NET SDK 還是 cURL 命令,Aspose.Cells Cloud 提供可靠的工具來有效減少 Excel 檔案的大小。試試看,看看您在工作流程中可以獲得多少效率。

實用資源

推薦文章

請訪問以下連結以了解更多資訊: