在 C# 应用程序中将 Excel 转换为 PDF

发布: (2025年12月5日 GMT+8 21:38)
3 min read
原文: Dev.to

Source: Dev.to

概述

将 Excel 文件转换为精美、可共享的 PDF 并不一定要慢或复杂。借助 GroupDocs.Conversion Cloud SDK for .NET,您可以直接在 C# 应用程序中集成 Excel 到 PDF 的转换,并以精准的方式自动化文档工作流。如果您的项目涉及报告、分析导出或数据打包,这个 SDK 能提供所需的控制力和速度,帮助您简化整个流程。

与依赖第三方桌面工具或笨重库不同,您可以在任何 .NET 项目中调用 GroupDocs Cloud API,瞬间将 XLSX 或 XLS 文件转换为高质量的 PDF 文档。SDK 提供完整的自定义选项——页面方向、分辨率、工作表选择以及渲染偏好——让您生成的 PDF 完全符合用户或业务流程所需的格式。这使其成为金融仪表盘、企业报告系统、文档管道以及对一致性要求高的 SaaS 平台的强大资产。

SDK 的独特之处在于其云原生设计。您不再需要管理依赖项或担心操作系统特定的行为。无论您的应用运行在 Windows、Linux 还是 macOS 上,转换引擎的表现都一致。这种可靠性为开发者提供了自信的扩展自由,能够构建处理大量或重复 Excel 到 PDF 转换的自动化工作流,而无需人工干预。如果您希望构建更高效、可投入生产的系统,将此 SDK 集成到 C# 环境中是提升文档管理能力的最快途径之一。

入门指南

完整分步指南。

代码示例

using System;
using GroupDocs.Conversion.Cloud.Sdk.Api;
using GroupDocs.Conversion.Cloud.Sdk.Client;
using GroupDocs.Conversion.Cloud.Sdk.Model;
using GroupDocs.Conversion.Cloud.Sdk.Model.Requests;

namespace ExcelToPDFConversion
{
    class Program
    {
        static void Main(string[] args)
        {
            // Configure API credentials
            string MyAppID = "your-app-id";
            string MyAppSecret = "your-app-secret";

            var config = new Configuration(MyAppID, MyAppSecret);
            var convertApi = new ConvertApi(config);

            // Prepare conversion settings
            var convertSettings = new ConvertSettings
            {
                // Source file path in cloud storage
                FilePath = "SampleFiles/source.xlsx",
                // Set output format
                Format = "pdf",
                ConvertOptions = new PdfConvertOptions
                {
                    PageSize = PdfConvertOptions.PageSizeEnum.A4,
                    MarginLeft = 5,
                    MarginTop = 5,
                },
                // Output file path in cloud storage
                OutputPath = "conversion/converted.pdf",
            };

            try
            {
                // Perform Excel to PDF conversion
                var request = new ConvertDocumentRequest(convertSettings);
                var response = convertApi.ConvertDocument(request);

                Console.WriteLine(response != null
                    ? "Excel to PDF conversion was successful!"
                    : "Excel to PDF conversion failed.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred: " + ex.Message);
            }
        }
    }
}
Back to Blog

相关文章

阅读更多 »