将电子表格内容导出为 Word 报告是业务应用程序中常见的需求,这类应用需要将数据分析与丰富的文本格式相结合。Aspose.Cells Cloud SDK for .NET 提供了强大的基于云的库,能够轻松处理此转换。在本指南中,您将学习如何使用 Aspose.Cells cloud sdk for .NET 将 Excel 数据导出为 Word 文档,提供完整的 C# 示例以及等效的 cURL 调用。完成后,您将能够在 .NET 项目或通过 REST API 生成 DOCX 文件,来源于 XLSX。
设置 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 命令完成,让您能够灵活地将转换集成到服务器端脚本或 CI 流水线中。
当您进入生产环境时,请记得购买正式许可证;在评估库时,您可以先从临时许可证页面获取临时许可证。拥有授权版本后,您将获得更高的配额、优先支持以及完整的功能集。
常见问题
如何使用 Aspose.Cells cloud sdk for .NET 将 Excel 数据导出为 Word 文档?
使用 CellsApi.PutConvertWorkbook 方法并传入 PutConvertWorkbookRequest,该请求指定源 Excel 字节数组和目标格式 "docx"。该方法返回一个流,您可以将其写入 .docx 文件。
Aspose.Cells Cloud 可以将哪些文件格式转换为 Word?
除了 DOCX,服务还可以将电子表格转换为 PDF、HTML 和其他文档类型。请参阅官方文档以获取完整列表。
是否需要许可证才能用于生产环境?
是的。对于生产部署,您必须获取商业许可证。临时许可证可用于评估,详见temporary license page。
我可以在不编写任何 C# 代码的情况下执行转换吗?
当然。正如前一节所示,可以通过使用 cURL 命令的 REST API 来执行相同的转换。