在 .NET 中将 PDF 转换为 Excel

概述

本文解释了如何 使用 C# 将 PDF 转换为 Excel 格式。它涵盖以下主题。

以下代码片段也适用于 Aspose.PDF.Drawing 库。

格式: XLS

格式: XLSX

格式: Excel

格式: 单个 Excel 工作表

格式: XML 电子表格 2003 格式

格式: CSV

格式: ODS

C# PDF 到 Excel 转换

Aspose.PDF for .NET 支持将 PDF 文件转换为 Excel 2007、CSV 和 SpreadsheetML 格式的功能。

Aspose.PDF for .NET 是一个 PDF 操作组件,我们引入了一项功能,可以将 PDF 文件呈现为 Excel 工作簿(XLSX 文件)。在此转换过程中,PDF 文件的各个页面被转换为 Excel 工作表。

为了将 PDF 文件转换为 XLSX 格式,Aspose.PDF 有一个名为 ExcelSaveOptions 的类。ExcelSaveOptions 类的对象作为第二个参数传递给 Document.Save(..) 构造函数。

以下代码片段展示了使用 Aspose.PDF for .NET 将 PDF 文件转换为 XLS 或 XLSX 格式的过程。

步骤:在 C# 中将 PDF 转换为 XLS

  1. 创建一个 Document 对象的实例,使用源 PDF 文档。
  2. 创建一个 ExcelSaveOptions 的实例。
  3. 通过调用 Document.Save() 方法并传递 ExcelSaveOptions,将其保存为 XLS 格式,指定 .xls 扩展名

步骤:在 C# 中将 PDF 转换为 XLSX

  1. 创建一个 Document 对象的实例,使用源 PDF 文档。
  2. 创建一个 ExcelSaveOptions 的实例。
  3. 通过调用 Document.Save() 方法并传递 ExcelSaveOptions,将其保存为 XLSX 格式,指定 .xlsx 扩展名
  // For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
 private static void ConvertPDFtoExcel()
 {
     // The path to the documents directory
     var dataDir = RunExamples.GetDataDir_AsposePdf();

     // Open PDF document
     using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
     {
         // Instantiate ExcelSaveOptions object
         var saveOptions = new Aspose.Pdf.ExcelSaveOptions();

         // Save the file in XLSX format
         document.Save(dataDir + "PDFToXLS_out.xlsx", saveOptions);
     }
 }

将 PDF 转换为具有控制列的 XLS

将 PDF 转换为 XLS 格式时,输出文件的第一列会添加一个空白列。ExcelSaveOptions 类中的 InsertBlankColumnAtFirst 选项用于控制此列。默认值为 false,这意味着不会插入空白列。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFtoExcelAdvanced_InsertBlankColumnAtFirst()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
    {
        // Instantiate ExcelSaveOptions object
        var saveOptions = new Aspose.Pdf.ExcelSaveOptions
        {
            InsertBlankColumnAtFirst = false
        };

        // Save the file in XLSX format
        document.Save(dataDir + "PDFToXLS_out.xlsx", saveOptions);
    }
}

将 PDF 转换为单个 Excel 工作表

当将包含大量页面的 PDF 文件导出为 XLS 时,每个页面会导出到 Excel 文件中的不同工作表。这是因为 MinimizeTheNumberOfWorksheets 属性默认设置为 false。要确保所有页面都导出到输出 Excel 文件的一个单一工作表中,请将 MinimizeTheNumberOfWorksheets 属性设置为 true。

步骤:在 C# 中将 PDF 转换为 XLS 或 XLSX 单个工作表

  1. 创建一个 Document 对象的实例,使用源 PDF 文档。
  2. 创建一个 ExcelSaveOptions 的实例,设置 MinimizeTheNumberOfWorksheets = true
  3. 通过调用 Document.Save() 方法并传递 ExcelSaveOptions,将其保存为具有单个工作表的 XLSXLSX 格式。
 // For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFtoExcelAdvanced_MinimizeTheNumberOfWorksheets()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
    {
        // Instantiate ExcelSaveOptions object
        var saveOptions = new Aspose.Pdf.ExcelSaveOptions
        {
            MinimizeTheNumberOfWorksheets = true
        };

        // Save the file in XLSX format
        document.Save(dataDir + "PDFToXLS_out.xlsx", saveOptions);
    }
}

转换为其他电子表格格式

转换为 XML 电子表格 2003 格式

自版本 20.8 起,Aspose.PDF 使用 Microsoft Excel Open XML 电子表格 2007 文件格式作为存储数据的默认格式。为了将 PDF 文件转换为 XML 电子表格 2003 格式,Aspose.PDF 有一个名为 ExcelSaveOptions 的类,具有 Format。将 ExcelSaveOptions 类的对象作为第二个参数传递给 Document.Save(..) 方法。

以下代码片段展示了将 PDF 文件转换为 XLS Excel 2003 XML 格式的过程。

步骤:在 C# 中将 PDF 转换为 Excel 2003 XML 格式

  1. 创建一个 Document 对象的实例,使用源 PDF 文档。
  2. 创建一个 ExcelSaveOptions 的实例,设置 Format = ExcelSaveOptions.ExcelFormat.XMLSpreadSheet2003
  3. 通过调用 Document.Save() 方法并传递 ExcelSaveOptions,将其保存为 XLS - Excel 2003 XML 格式
  // For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
 private static void ConvertPDFtoExcelAdvanced_SaveXLS2003()
 {
     // The path to the documents directory
     var dataDir = RunExamples.GetDataDir_AsposePdf();

     // Open PDF document
     using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
     {
         // Instantiate ExcelSaveOptions object
         var saveOptions = new Aspose.Pdf.ExcelSaveOptions
         {
             Format = Aspose.Pdf.ExcelSaveOptions.ExcelFormat.XMLSpreadSheet2003
         };

         // Save the file in XLS format
         document.Save(dataDir + "PDFToXLS_out.xls", saveOptions);
     }
 }

转换为 CSV

转换为 CSV 格式的过程与上述相同。您需要做的就是设置适当的格式。

步骤:在 C# 中将 PDF 转换为 CSV

  1. 创建一个 Document 对象的实例,使用源 PDF 文档。
  2. 创建一个 ExcelSaveOptions 的实例,设置 Format = ExcelSaveOptions.ExcelFormat.CSV
  3. 通过调用 Document.Save() 方法并传递 ExcelSaveOptions,将其保存为 CSV 格式。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFToCSV()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
    {
        // Instantiate ExcelSaveOptions object
        var saveOptions = new Aspose.Pdf.ExcelSaveOptions
        {
            Format = Aspose.Pdf.ExcelSaveOptions.ExcelFormat.CSV
        };
        
        // Save the file in CSV format
        document.Save(dataDir + "PDFToXLS_out.csv", saveOptions);
    }
}

转换为 ODS

步骤:在 C# 中将 PDF 转换为 ODS

  1. 创建一个 Document 对象的实例,使用源 PDF 文档。
  2. 创建一个 ExcelSaveOptions 的实例,设置 Format = ExcelSaveOptions.ExcelFormat.ODS
  3. 通过调用 Document.Save() 方法并传递 ExcelSaveOptions,将其保存为 ODS 格式。

转换为 ODS 格式的过程与所有其他格式相同。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFToODS()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();
    
    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
    {
        // Instantiate ExcelSaveOptions object
        var saveOptions = new Aspose.Pdf.ExcelSaveOptions
        {
            Format = Aspose.Pdf.ExcelSaveOptions.ExcelFormat.ODS
        };

        // Save the file in ODS format
        document.Save(dataDir + "PDFToODS_out.ods", saveOptions);
    }
}

另见

本文还涵盖了这些主题。代码与上述相同。

格式: Excel

格式: XLS

格式: XLSX

格式: CSV

格式: ODS