PDF를 Excel로 변환하기 (.NET)

개요

이 문서에서는 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 및 스프레드시트ML 형식으로 변환하는 기능을 지원합니다.

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. 소스 PDF 문서로 Document 객체의 인스턴스를 생성합니다.
  2. ExcelSaveOptions의 인스턴스를 생성합니다.
  3. Document.Save() 메서드를 호출하고 ExcelSaveOptions를 전달하여 .xls 확장자를 지정하여 XLS 형식으로 저장합니다.

단계: C#에서 PDF를 XLSX로 변환하기

  1. 소스 PDF 문서로 Document 객체의 인스턴스를 생성합니다.
  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. 소스 PDF 문서로 Document 객체의 인스턴스를 생성합니다.
  2. MinimizeTheNumberOfWorksheets = trueExcelSaveOptions의 인스턴스를 생성합니다.
  3. Document.Save() 메서드를 호출하고 ExcelSaveOptions를 전달하여 단일 워크시트를 가진 XLS 또는 XLSX 형식으로 저장합니다.
 // 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. 소스 PDF 문서로 Document 객체의 인스턴스를 생성합니다.
  2. Format = ExcelSaveOptions.ExcelFormat.XMLSpreadSheet2003ExcelSaveOptions의 인스턴스를 생성합니다.
  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. 소스 PDF 문서로 Document 객체의 인스턴스를 생성합니다.
  2. Format = ExcelSaveOptions.ExcelFormat.CSVExcelSaveOptions의 인스턴스를 생성합니다.
  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. 소스 PDF 문서로 Document 객체의 인스턴스를 생성합니다.
  2. Format = ExcelSaveOptions.ExcelFormat.ODSExcelSaveOptions의 인스턴스를 생성합니다.
  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