如何用 .NET 将 Excel 工作表转换为 PDF

如何用 .NET 将 Excel 工作表转换为 PDF

本文展示了如何将 Excel 文件转换为 PDF,使用 Aspose.Cells LowCode PDF Converter 在 .NET 应用程序中使用一个单页的表格布局。

现实世界问题

金融分析师经常需要将多个表格的Excel财务模型转换为标准化PDF报告,每个工作表显示在自己的页面上,以便更容易审查和打印。 传统的转型方法无论是将所有数据集成到连续的页面的,还是需要每张表的手动处理。

解决方案概述

使用 Aspose.Cells LowCode PDF Converter,我们可以以最小的代码有效地解决这个挑战,这个解决方案是金融专业人士,报告开发人员和企业分析师,他们需要创建专业的PDF报告,同时保持工作表从他们的Excel数据来源的逻辑分离。

原則

在实施解决方案之前,请确保您有:

  • Visual Studio 2019 或以后
  • .NET 6.0 或更高版本(兼容 .Net Framework 4.6.2+)
  • 通过 NuGet 安装的 .NET 包的 Aspose.Cells
  • C#编程的基本理解
PM> Install-Package Aspose.Cells

步骤实施

步骤1:安装和设置 Aspose.Cells

将 Aspose.Cells 包添加到您的项目中,并包含所需的名称空间:

using Aspose.Cells;
using Aspose.Cells.LowCode;
using Aspose.Cells.Rendering;
using System;
using System.IO;

步骤2:准备您的输入数据

为此示例,我们将使用现有 Excel 文件. 确保您的源文件包含多个工作表,您希望在 PDF 中将其转换为单独的页面:

// Define the path to your Excel file
string excelFilePath = "financial-report-template.xlsx";

// Ensure the file exists
if (!File.Exists(excelFilePath))
{
    throw new FileNotFoundException("The specified Excel file was not found.", excelFilePath);
}

// Create output directory if it doesn't exist
string outputDirectory = "result";
if (!Directory.Exists(outputDirectory))
{
    Directory.CreateDirectory(outputDirectory);
}

步骤3:设置 PDF 转换器选项

设置 PDF Converter 过程的选项,指定单页每页设置:

// Create the LowCode load options for the source file
LowCodeLoadOptions loadOptions = new LowCodeLoadOptions();
loadOptions.InputFile = excelFilePath;

// Create the LowCode PDF save options
LowCodePdfSaveOptions pdfSaveOptions = new LowCodePdfSaveOptions();

// Configure PDF-specific settings
PdfSaveOptions pdfOptions = new PdfSaveOptions();
pdfOptions.OnePagePerSheet = true;  // This is the key setting for our requirement

// Optionally set additional PDF options
pdfOptions.Compliance = PdfCompliance.PdfA1b;  // For archival compliance if needed
pdfOptions.AllColumnsInOnePagePerSheet = true; // Ensure all columns fit on one page

// Apply the PDF options to our save options
pdfSaveOptions.PdfOptions = pdfOptions;

// Set the output file path
string outputFilePath = Path.Combine(outputDirectory, "FinancialReport.pdf");
pdfSaveOptions.OutputFile = outputFilePath;

步骤4:执行PDF转换过程

使用配置选项运行 PDF Converter 操作:

try
{
    // Process the conversion using the LowCode PDF Converter
    PdfConverter.Process(loadOptions, pdfSaveOptions);
    
    Console.WriteLine($"Conversion completed successfully. PDF saved to: {outputFilePath}");
}
catch (Exception ex)
{
    Console.WriteLine($"Error during conversion: {ex.Message}");
    // Log the exception or handle it according to your application's requirements
}

步骤5:处理输出

转换后,您可能希望打开 PDF 或进一步处理它:

// Check if the output file was created
if (File.Exists(outputFilePath))
{
    // Open the file using the default PDF viewer (optional)
    try
    {
        System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
        {
            FileName = outputFilePath,
            UseShellExecute = true
        });
    }
    catch (Exception ex)
    {
        Console.WriteLine($"The file was created but could not be opened: {ex.Message}");
    }
}
else
{
    Console.WriteLine("The output file was not created.");
}

步骤6:实施错误处理

添加正确的错误处理,以确保稳定的操作:

try
{
    // Create the load options
    LowCodeLoadOptions loadOptions = new LowCodeLoadOptions();
    
    // Verify input file exists before assigning
    if (!File.Exists(excelFilePath))
    {
        throw new FileNotFoundException("Input Excel file not found", excelFilePath);
    }
    
    loadOptions.InputFile = excelFilePath;
    
    // Create and configure PDF save options
    LowCodePdfSaveOptions pdfSaveOptions = new LowCodePdfSaveOptions();
    PdfSaveOptions pdfOptions = new PdfSaveOptions();
    pdfOptions.OnePagePerSheet = true;
    pdfSaveOptions.PdfOptions = pdfOptions;
    pdfSaveOptions.OutputFile = outputFilePath;
    
    // Execute conversion
    PdfConverter.Process(loadOptions, pdfSaveOptions);
    
    // Verify output was created
    if (!File.Exists(outputFilePath))
    {
        throw new Exception("PDF conversion completed but output file was not created");
    }
    
    Console.WriteLine("Conversion successful!");
}
catch (CellsException cellsEx)
{
    // Handle Aspose.Cells specific exceptions
    Console.WriteLine($"Aspose.Cells error: {cellsEx.Message}");
    // Consider logging the exception or custom handling based on cellsEx.Code
}
catch (IOException ioEx)
{
    // Handle file IO exceptions
    Console.WriteLine($"File operation error: {ioEx.Message}");
}
catch (Exception ex)
{
    // Handle other exceptions
    Console.WriteLine($"General error: {ex.Message}");
}

步骤7:优化性能

考虑这些生产环境优化技术:

  • 使用 MemoryStream 用于高容量处理:
// For high-volume processing, using memory streams can be more efficient
using (MemoryStream outputStream = new MemoryStream())
{
    // Configure save options to use memory stream
    LowCodePdfSaveOptions pdfSaveOptions = new LowCodePdfSaveOptions();
    pdfSaveOptions.OutputStream = outputStream;
    pdfSaveOptions.PdfOptions = new PdfSaveOptions { OnePagePerSheet = true };
    
    // Process the conversion
    PdfConverter.Process(loadOptions, pdfSaveOptions);
    
    // Now you can use the stream as needed (save to file, send via network, etc.)
    outputStream.Position = 0;
    using (FileStream fileStream = File.Create(outputFilePath))
    {
        outputStream.CopyTo(fileStream);
    }
}
  • 用于包装处理,尽可能重复使用物品:
// Create PDF options once and reuse
PdfSaveOptions pdfOptions = new PdfSaveOptions { OnePagePerSheet = true };

// Process multiple files
foreach (string excelFile in Directory.GetFiles("input-directory", "*.xlsx"))
{
    LowCodeLoadOptions loadOptions = new LowCodeLoadOptions { InputFile = excelFile };
    LowCodePdfSaveOptions saveOptions = new LowCodePdfSaveOptions
    {
        PdfOptions = pdfOptions,
        OutputFile = Path.Combine("output-directory", Path.GetFileNameWithoutExtension(excelFile) + ".pdf")
    };
    
    PdfConverter.Process(loadOptions, saveOptions);
}

步骤8:完整实施示例

下面是一个完整的工作例子,展示了整个过程:

using Aspose.Cells;
using Aspose.Cells.LowCode;
using Aspose.Cells.Rendering;
using System;
using System.IO;

namespace ExcelToPdfConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            // Define input and output paths
            string inputDirectory = "input-files";
            string outputDirectory = "result";
            string excelFilePath = Path.Combine(inputDirectory, "financial-report.xlsx");
            
            // Ensure directories exist
            Directory.CreateDirectory(outputDirectory);
            
            try
            {
                Console.WriteLine($"Converting {excelFilePath} to PDF...");
                
                // Simple one-line approach (less customization)
                string quickOutputPath = Path.Combine(outputDirectory, "QuickOutput.pdf");
                PdfConverter.Process(excelFilePath, quickOutputPath);
                Console.WriteLine($"Basic conversion completed: {quickOutputPath}");
                
                // Advanced approach with custom options
                // Setup load options
                LowCodeLoadOptions loadOptions = new LowCodeLoadOptions
                {
                    InputFile = excelFilePath
                };
                
                // Setup PDF save options with specific settings
                LowCodePdfSaveOptions pdfSaveOptions = new LowCodePdfSaveOptions();
                PdfSaveOptions pdfOptions = new PdfSaveOptions
                {
                    OnePagePerSheet = true,
                    Compliance = PdfCompliance.PdfA1b,
                    PrintingPageType = PrintingPageType.ActualSize
                };
                pdfSaveOptions.PdfOptions = pdfOptions;
                
                string customOutputPath = Path.Combine(outputDirectory, "CustomOutput.pdf");
                pdfSaveOptions.OutputFile = customOutputPath;
                
                // Execute the conversion
                PdfConverter.Process(loadOptions, pdfSaveOptions);
                Console.WriteLine($"Advanced conversion completed: {customOutputPath}");
                
                // Batch processing example
                BatchProcessExcelFiles(inputDirectory, outputDirectory);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
            
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
        
        static void BatchProcessExcelFiles(string inputDir, string outputDir)
        {
            // Get all Excel files
            string[] excelFiles = Directory.GetFiles(inputDir, "*.xlsx");
            
            if (excelFiles.Length == 0)
            {
                Console.WriteLine("No Excel files found for batch processing.");
                return;
            }
            
            // Create reusable PDF options
            PdfSaveOptions pdfOptions = new PdfSaveOptions
            {
                OnePagePerSheet = true
            };
            
            Console.WriteLine($"Batch processing {excelFiles.Length} files...");
            
            foreach (string file in excelFiles)
            {
                try
                {
                    string fileName = Path.GetFileNameWithoutExtension(file);
                    string outputPath = Path.Combine(outputDir, $"{fileName}.pdf");
                    
                    // Configure options
                    LowCodeLoadOptions loadOptions = new LowCodeLoadOptions
                    {
                        InputFile = file
                    };
                    
                    LowCodePdfSaveOptions saveOptions = new LowCodePdfSaveOptions
                    {
                        PdfOptions = pdfOptions,
                        OutputFile = outputPath
                    };
                    
                    // Process conversion
                    PdfConverter.Process(loadOptions, saveOptions);
                    Console.WriteLine($"Converted: {Path.GetFileName(file)} -> {Path.GetFileName(outputPath)}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error processing {Path.GetFileName(file)}: {ex.Message}");
                }
            }
            
            Console.WriteLine("Batch processing completed.");
        }
    }
}

使用案例和应用程序

企业报告系统

金融机构可以自动从Excel数据模型中创建标准化PDF报告,每个包含不同的财务测量(平衡表、收入报告、现金流预测)的工作表都会出现在自己的页面上,保持数据的清晰分离,同时确保所有报告的一致格式化。

规则遵守文件一代

必须向监管机构提交标准化财务或运营数据的组织可以将其基于Excel的数据转换为兼容的PDF格式。 单页每页选项确保每个监管表格或数据集在电子或印刷时保持其完整性和适当的布局,从而减少由编辑错误导致的遵守风险。

自动报告分发系统

销售或运营部门可以实施自动化系统,在那里Excel数据定期转换为专业的PDF,用于向客户或内部利益相关者分发。 单页每页格式确保接收者收到有结构的文件,每个业务单位、产品线或时间段出现在自己的页面上,以便更容易进行导航和分析。

共同挑战与解决方案

挑战1:大型工作表扩展页面边界

** 解决方案:** 调整 PDF 选项以通过修改规模设置来处理大工作表:

PdfSaveOptions pdfOptions = new PdfSaveOptions();
pdfOptions.OnePagePerSheet = true;
pdfOptions.AllColumnsInOnePagePerSheet = false; // Let large sheets span multiple pages horizontally
pdfOptions.WidthFitToPagesCount = 2; // Allow up to 2 pages for width if needed

挑战2:自定义标题和脚印不出现在PDF中

** 解决方案:** 在 PDF 输出中应用自定义头和脚:

PdfSaveOptions pdfOptions = new PdfSaveOptions();
pdfOptions.OnePagePerSheet = true;

// Add custom header and footer
pdfOptions.IsCustomPrintAreaSet = true;
pdfOptions.CustomPrintPageTopMargin = 50;
pdfOptions.CustomPrintPageBottomMargin = 50;
pdfOptions.HeaderFooterManager.SetHeader(HeaderFooterType.FirstPage, "Company Financial Report");
pdfOptions.HeaderFooterManager.SetFooter(HeaderFooterType.AllPages, "&P of &N");

挑战3:图像和图表在PDF中出现不正确

** 解决方案:** 通过以下设置提高图像和图形质量:

PdfSaveOptions pdfOptions = new PdfSaveOptions();
pdfOptions.OnePagePerSheet = true;
pdfOptions.Quality = 100; // Highest quality setting
pdfOptions.DefaultEditLanguage = EditLanguage.Default; // Proper text rendering
pdfOptions.CheckFontCompatibility = true; // Ensure fonts are compatible

绩效考虑

  • 对于多表工作簿,这个过程可以是内存密集的;考虑处理文件序列而不是平行在记忆限制的系统。
  • 在转换大 Excel 文件时,使用流程方法来减少内存使用量。
  • 对于不同设置的相同文件的重复转换,将文件一次加载到记忆中并重新使用。
  • 考虑实施用于高容量环境的引导系统,以管理资源利用。

最佳实践

  • 在处理之前,始终验证输入文件以避免运行时间的例外。
  • 在文件 I/O 操作和转换过程周围实施正确的错误处理。
  • 根据您的档案或分发要求,设置适当的 PDF 遵守标准(PDF/A-1b,PDF 1.7)。
  • Cache 经常使用的文件或模板,以提高重复操作的性能。
  • 考虑实施长期行程交换的进展报告。

先进的场景

对于更复杂的要求,请考虑这些先进的实施:

场景1:自定义页面导向的选择性表格转换

// Create load options
LowCodeLoadOptions loadOptions = new LowCodeLoadOptions();
loadOptions.InputFile = "multi-sheet-report.xlsx";

// Create PDF save options with advanced settings
LowCodePdfSaveOptions pdfSaveOptions = new LowCodePdfSaveOptions();
PdfSaveOptions pdfOptions = new PdfSaveOptions();
pdfOptions.OnePagePerSheet = true;

// Only convert specific sheets
Workbook workbook = new Workbook(loadOptions.InputFile);
pdfOptions.SheetSet = new Aspose.Cells.Rendering.SheetSet(new int[] { 0, 2, 3 }); // Select specific sheets by index

// Set page orientation based on sheet content
foreach (Worksheet sheet in workbook.Worksheets)
{
    if (sheet.Cells.MaxColumn > 10) // Wide sheets get landscape orientation
    {
        PageSetup pageSetup = sheet.PageSetup;
        pageSetup.Orientation = PageOrientationType.Landscape;
    }
}

pdfSaveOptions.PdfOptions = pdfOptions;
pdfSaveOptions.OutputFile = "selective-sheets-report.pdf";

// Process the conversion
PdfConverter.Process(loadOptions, pdfSaveOptions);

场景2:将数字签名添加到创建的PDF

// Standard conversion setup
LowCodeLoadOptions loadOptions = new LowCodeLoadOptions();
loadOptions.InputFile = "financial-statement.xlsx";

LowCodePdfSaveOptions pdfSaveOptions = new LowCodePdfSaveOptions();
PdfSaveOptions pdfOptions = new PdfSaveOptions();
pdfOptions.OnePagePerSheet = true;

// Set up digital signature parameters
pdfOptions.DigitalSignature = new DigitalSignature();
pdfOptions.DigitalSignature.CertificateFilePath = "signature-certificate.pfx";
pdfOptions.DigitalSignature.Password = "certificate-password";
pdfOptions.DigitalSignature.Reason = "Financial approval";
pdfOptions.DigitalSignature.Location = "Finance Department";
pdfOptions.DigitalSignature.SignatureDate = DateTime.Now;

pdfSaveOptions.PdfOptions = pdfOptions;
pdfSaveOptions.OutputFile = "signed-financial-report.pdf";

// Process the conversion with digital signature
PdfConverter.Process(loadOptions, pdfSaveOptions);

结论

通过实施 Aspose.Cells LowCode PDF Converter,您可以有效地将多表 Excel 工作簿转化为专业格式化的 PDF 文件,并保持一个单页每表选项的逻辑工作表分离。

要了解更多信息和更多例子,请参阅 Aspose.Cells.LowCode API 参考 .

 中文