將試算表內容匯出為 Word 報告是需要結合資料分析與豐富文字格式的商業應用程式常見需求。 Aspose.Cells Cloud SDK for .NET 提供功能強大的雲端函式庫,能輕鬆處理此轉換。在本指南中,您將學習如何使用 Aspose.Cells cloud sdk for .NET 將 Excel 資料匯出為 Word 文件,並提供完整的 C# 範例以及相應的 cURL 呼叫。完成後,您將能在 .NET 專案或透過 REST API,從 XLSX 來源產生 DOCX 檔案。

設定 Aspose.Cells Cloud SDK for .NET

在開始之前,請確保您具備以下條件:

  • .NET 6.0 或更高版本已安裝。
  • 如 Visual Studio 2022 等 IDE。
  • 具備 Client Id 與 Client Secret 的 Aspose Cloud 帳戶。
  • 可存取 NuGet 套件儲存庫。

使用 NuGet 安裝 SDK:

dotnet add package Aspose.cells-Cloud

下載最新套件並在下載頁面上查看發行說明。

在程式碼中配置您的憑證:

var config = new Configuration
{
    ClientId = "your-client-id",
    ClientSecret = "your-client-secret",
    BasePath = "https://api.aspose.cloud"
};

安裝好 SDK 並設定好憑證後,您就可以開始轉換檔案了。

使用 Aspose.Cells Cloud SDK for .NET 將 Excel 資料匯出至 Word 文件的逐步指南

步驟 1:載入來源 Excel 檔案

將 XLSX 檔案讀取為位元組陣列,以便將其傳送至雲端服務。

byte[] excelData = File.ReadAllBytes("input.xlsx");

步驟 2:建立 CellsApi 的實例

CellsApi 類別提供所有轉換操作。請參閱 API 參考 以取得詳細資訊。

var cellsApi = new CellsApi(config);

步驟 3:建立轉換請求

指定來源資料、目標格式(docx)以及任何可選參數。

var convertRequest = new PutConvertWorkbookRequest(
    file: excelData,
    format: "docx",
    password: null,
    outPath: null,
    storageName: null,
    folder: null
);

步驟 4:執行轉換

呼叫 PutConvertWorkbook 以執行轉換。此方法會回傳包含 DOCX 檔案的串流。

using (Stream wordStream = cellsApi.PutConvertWorkbook(convertRequest))

第5步:保存生成的 Word 文檔

將返回的串流寫入磁碟上的檔案。

using (FileStream fileStream = new FileStream("output.docx", FileMode.Create, FileAccess.Write))
{
    wordStream.CopyTo(fileStream);
}

完成這些步驟後,Excel 工作簿現在已匯出為 Word 文件。

完整的工作範例:使用 Aspose.Cells Cloud SDK for .NET 將 Excel 轉換為 Word

以下範例示範了從配置到檔案儲存的完整過程。

using System;
using System.IO;
using Aspose.Cells.Cloud.SDK.Api;
using Aspose.Cells.Cloud.SDK.Model;
using Aspose.Cells.Cloud.SDK.Client;

namespace ExportExcelToWord
{
    class Program
    {
        static void Main(string[] args)
        {
            // Configure Aspose.Cells Cloud credentials
            var config = new Configuration
            {
                ClientId = "your-client-id",
                ClientSecret = "your-client-secret",
                BasePath = "https://api.aspose.cloud"
            };

            // Create an instance of the Cells API
            var cellsApi = new CellsApi(config);

            // Define input Excel file and output Word file paths
            string inputExcelPath = "input.xlsx";
            string outputWordPath = "output.docx";

            // Read the Excel file into a byte array
            byte[] excelData = File.ReadAllBytes(inputExcelPath);

            // Build the conversion request (Excel to DOCX)
            var convertRequest = new PutConvertWorkbookRequest(
                file: excelData,
                format: "docx",
                password: null,
                outPath: null,          // Return the result as a stream
                storageName: null,
                folder: null
            );

            // Perform the conversion
            using (Stream wordStream = cellsApi.PutConvertWorkbook(convertRequest))
            using (FileStream fileStream = new FileStream(outputWordPath, FileMode.Create, FileAccess.Write))
            {
                wordStream.CopyTo(fileStream);
            }

            Console.WriteLine($"Excel file '{inputExcelPath}' has been successfully exported to Word document '{outputWordPath}'.");
        }
    }
}

注意: 此程式碼範例展示了核心功能。 在將其用於您的專案之前,請確保更新所有檔案路徑和設定值以符合實際環境,驗證已正確安裝所有必要的相依性,並在開發環境中徹底測試。 若遇到任何問題,請參閱官方文件或聯繫支援團隊以獲得協助。

使用 cURL 透過 REST API 將 Excel 轉換為 Word

您可以透過直接呼叫 Aspose.Cells Cloud REST API,而不必編寫 C# 程式碼,即可完成相同的轉換。

1. 驗證並取得存取權杖

curl -X POST "https://api.aspose.cloud/connect/token" \
     -H "Content-Type: application/x-www-form-urlencoded" \
     -d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"

回應中包含一個 access_token,您將在後續呼叫中使用它。

2. 上傳來源 Excel 檔案

curl -X PUT "https://api.aspose.cloud/v3.0/cells/storage/file/input.xlsx" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/octet-stream" \
     --data-binary "@input.xlsx"

3. 執行轉換

curl -X POST "https://api.aspose.cloud/v3.0/cells/convert?format=docx" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/json" \
     -d '{"File": "input.xlsx"}' \
     -o output.docx

-o 旗標會將轉換後的 DOCX 檔案儲存至本機。

4. 下載輸出檔案(如果已儲存至雲端儲存)

如果您選擇將結果存儲在雲端儲存,而不是直接下載,請使用:

curl -X GET "https://api.aspose.cloud/v3.0/cells/storage/file/output.docx" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -o output.docx

欲了解更多細節,請參閱官方 API 文件。

最後的想法

一旦配置好 SDK,使用 Aspose.Cells cloud sdk for .NET 將 Excel 資料匯出為 Word 文件就相當簡單。提供的 C# 範例展示了如何讀取 XLSX 檔案、呼叫轉換端點,並寫入產生的 DOCX 檔案。相同的操作也可以透過簡單的 cURL 命令完成,讓您能靈活地將轉換整合到 server‑side 腳本或 CI pipelines 中。

當您將應用程式投入生產環境時,請記得購買正式授權;在評估函式庫時,您可以先從臨時授權頁面取得臨時授權。擁有授權版本後,您即可使用更高的配額、優先支援以及完整功能集。

常見問題

如何使用 Aspose.Cells cloud sdk for .NET 將 Excel 資料匯出為 Word 文件?

使用 CellsApi.PutConvertWorkbook 方法,搭配 PutConvertWorkbookRequest,指定來源 Excel 位元組陣列以及目標格式 "docx"。此方法會回傳一個串流,您可以將其寫入 .docx 檔案。

Aspose.Cells Cloud 可以轉換成 Word 的檔案格式有哪些?

除了 DOCX,該服務還可以將試算表轉換為 PDF、HTML 以及其他文件類型。請參閱 官方文件 以獲取完整清單。

是否需要許可證才能在生產環境中使用?

是的。對於生產部署,您必須取得商業許可證。臨時許可證可用於評估,請前往臨時許可證頁面。

我可以在不編寫任何 C# 代碼的情況下執行轉換嗎?

當然可以。正如前一節所示,您可以使用 cURL 命令透過 REST API 執行相同的轉換。

相關文章