Convertir PDF en Excel dans .NET

Aperçu

Cet article explique comment convertir des PDF en formats Excel en utilisant C#. Il couvre les sujets suivants.

Le code suivant fonctionne également avec la bibliothèque Aspose.PDF.Drawing.

Conversions PDF en Excel C#

Aspose.PDF for .NET prend en charge la fonctionnalité de conversion de fichiers PDF en formats Excel 2007, CSV et SpeadsheetML.

Aspose.PDF for .NET est un composant de manipulation PDF, nous avons introduit une fonctionnalité qui rend le fichier PDF en classeur Excel (fichiers XLSX). Lors de cette conversion, les pages individuelles du fichier PDF sont converties en feuilles de calcul Excel.

Pour convertir des fichiers PDF en format XLSX, Aspose.PDF dispose d’une classe appelée ExcelSaveOptions. Un objet de la classe ExcelSaveOptions est passé comme deuxième argument au constructeur Document.Save(..).

Le code suivant montre le processus de conversion d’un fichier PDF en format XLS ou XLSX avec Aspose.PDF for .NET.

Convertir PDF en XLS

  1. Créez une instance de l’objet Document avec le document PDF source.
  2. Créez une instance de ExcelSaveOptions.
  3. Enregistrez-le au format XLS en spécifiant l’extension .xls en appelant la méthode Document.Save() et en lui passant ExcelSaveOptions.

Convertir PDF en XLSX

  1. Créez une instance de l’objet Document avec le document PDF source.
  2. Créez une instance de ExcelSaveOptions.
  3. Enregistrez-le au format XLSX en spécifiant l’extension .xlsx en appelant la méthode Document.Save() et en lui passant ExcelSaveOptions.
  // 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);
     }
 }

Convertir PDF en XLS avec contrôle de colonne

Lors de la conversion d’un PDF en format XLS, une colonne vide est ajoutée au fichier de sortie en tant que première colonne. L’option InsertBlankColumnAtFirst de la classe ExcelSaveOptions est utilisée pour contrôler cette colonne. La valeur par défaut est false, ce qui signifie que les colonnes vides ne seront pas insérées.

// 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);
    }
}

Convertir PDF en une seule feuille de calcul Excel

Lors de l’exportation d’un fichier PDF avec de nombreuses pages en XLS, chaque page est exportée vers une feuille différente dans le fichier Excel. Cela est dû au fait que la propriété MinimizeTheNumberOfWorksheets est définie sur false par défaut. Pour s’assurer que toutes les pages sont exportées vers une seule feuille dans le fichier Excel de sortie, définissez la propriété MinimizeTheNumberOfWorksheets sur true.

Convertir PDF en XLS ou XLSX avec une seule feuille de calcul

  1. Créez une instance de l’objet Document avec le document PDF source.
  2. Créez une instance de ExcelSaveOptions avec MinimizeTheNumberOfWorksheets = true.
  3. Enregistrez-le au format XLS ou XLSX ayant une seule feuille de calcul en appelant la méthode Document.Save() et en lui passant ExcelSaveOptions.
 // 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);
    }
}

Convertir en d’autres formats de feuille de calcul

Convertir en format XML Spreadsheet 2003

Depuis la version 20.8, Aspose.PDF utilise le format de fichier Microsoft Excel Open XML Spreadsheet 2007 par défaut pour stocker des données. Pour convertir des fichiers PDF en format XML Spreadsheet 2003, Aspose.PDF dispose d’une classe appelée ExcelSaveOptions avec Format. Un objet de la classe ExcelSaveOptions est passé comme deuxième argument à la méthode Document.Save(..).

Le code suivant montre le processus de conversion d’un fichier PDF en format XLS Excel 2003 XML.

Convertir PDF en format Excel 2003 XML

  1. Créez une instance de l’objet Document avec le document PDF source.
  2. Créez une instance de ExcelSaveOptions avec Format = ExcelSaveOptions.ExcelFormat.XMLSpreadSheet2003.
  3. Enregistrez-le au format XLS - Format Excel 2003 XML en appelant la méthode Document.Save() et en lui passant ExcelSaveOptions.
  // 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);
     }
 }

Convertir en CSV

La conversion en format CSV s’effectue de la même manière que ci-dessus. Tout ce que vous devez faire est de définir le format approprié.

Convertir PDF en CSV

  1. Créez une instance de l’objet Document avec le document PDF source.
  2. Créez une instance de ExcelSaveOptions avec Format = ExcelSaveOptions.ExcelFormat.CSV.
  3. Enregistrez-le au format CSV en appelant la méthode Document.Save() et en lui passant ExcelSaveOptions.
// 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);
    }
}

Convertir en ODS

Convertir PDF en ODS

  1. Créez une instance de l’objet Document avec le document PDF source.
  2. Créez une instance de ExcelSaveOptions avec Format = ExcelSaveOptions.ExcelFormat.ODS.
  3. Enregistrez-le au format ODS en appelant la méthode Document.Save() et en lui passant ExcelSaveOptions.

La conversion en format ODS s’effectue de la même manière que tous les autres formats.

// 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);
    }
}

Convertir en XLSM

Convertir PDF en XLSM

  1. Créez une instance de l’objet Document avec le document PDF source.
  2. Créez une instance de ExcelSaveOptions avec Format = ExcelSaveOptions.ExcelFormat.XLSM.
  3. Enregistrez-le au format XLSM en appelant la méthode Document.Save() et en lui passant ExcelSaveOptions.

La conversion en format XLSM s’effectue de la même manière que tous les autres formats.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFToXLSM()
{
    // 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.XLSM
        };

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