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.

Format: XLS

Format: XLSX

Format: Excel

Format: Feuille de calcul Excel unique

Format: Format de feuille de calcul XML 2003

Format: CSV

Format: ODS

Conversions C# PDF en Excel

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 de 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.

Étapes : Convertir PDF en XLS en C#

  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.

Étapes : Convertir PDF en XLSX en C#

  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 comme 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 feuille de calcul Excel unique

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.

Étapes : Convertir PDF en XLS ou XLSX Feuille unique en C#

  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 feuille unique 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.

Étapes : Convertir PDF en format Excel 2003 XML en C#

  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 XML Excel 2003 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 dont vous avez besoin est de définir le format approprié.

Étapes : Convertir PDF en CSV en C#

  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

Étapes : Convertir PDF en ODS en C#

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

Voir aussi

Cet article couvre également ces sujets. Les codes sont les mêmes que ci-dessus.

Format: Excel

Format: XLS

Format: XLSX

Format: CSV

Format: ODS