Zum Fußzeileninhalt springen
IRONPDF NUTZEN
Wie konvertiert man Excel-Dateien in PDF in C#

Excel zu PDF in C# konvertieren (Schritt-für-Schritt) Tutorial

Excel files can be difficult to share because they can be easily altered and are not compatible with all devices and programs. PDFs (Portable Document Format), on the other hand, are universally compatible and cannot be edited without specialized software. For this reason, many businesses require that developers convert Excel files (.xls, .xlsx) to ISO standardized version PDF format programmatically.

There are several reasons why this is important. First, it ensures that the data contained in the Excel workbook cannot be altered. Second, it allows the file to be viewed on any device or program. And finally, it makes it possible to password-protect the file, preventing unauthorized access. Overall, converting Excel files to PDF format is an important way to protect data and ensure its compatibility across all platforms.

Converting Excel to PDF document formats can be a simple task if you have the right tools and knowledge. Let's see how to convert using the IronPDF C# library in .NET Core.

IronPDF: .NET PDF Library

IronPDF is a .NET library that makes it easy to generate, edit, and merge PDF files in C# and VB.NET. With IronPDF, you can create PDF in C# from scratch or convert existing HTML, CSS, and JavaScript files into PDFs. You can also add headers, footers, watermarks, and page numbers to PDF files. If you need to collaborate with others on a project, IronPDF makes it easy to merge multiple PDF files into one.

Throughout this article, IronPDF will be used to show how to convert an Excel document into a PDF document using C# and .NET Core.

Prerequisites

There are some prerequisites to convert an XLSX file to a PDF document.

  1. Visual Studio 2022 (Recommended)
  2. A running .NET application with the latest .NET Framework (Recommended)
  3. Microsoft Office installed
  4. A stable internet connection to install the IronPDF library for the Excel to PDF conversion process

Let's move to the main steps to convert an Excel file to PDF.

Step 1: Export your Excel Workbooks as HTML

To export your Excel file to HTML format, do the following steps:

  1. Launch Microsoft Excel and load the Excel spreadsheets there.

    Convert Excel to PDF in C# (Step-by-Step) Tutorial, Figure 1: Load Excel Spreadsheets Load Excel Spreadsheets

  2. Select "Save As" from the "File" menu.

    Convert Excel to PDF in C# (Step-by-Step) Tutorial, Figure 2: SaveAs Option SaveAs Option

  3. Click on the 'Browse' button and navigate to your desired location. From the File type dropdown, select the 'HTML Page'.

    Convert Excel to PDF in C# (Step-by-Step) Tutorial, Figure 3: File Type Selection File Type Selection

Your Excel file will be converted to an HTML file if you complete the procedures above. The exported HTML file can now be used to convert to PDF.

Step 2: Add IronPDF to your solution

The next step is to install IronPDF in this project using the NuGet Package Manager Console. Click on the Tools from the top menu and select the NuGet Package Manager followed by "Package Manager Console".

Convert Excel to PDF in C# (Step-by-Step) Tutorial, Figure 4: NuGet Package Manager NuGet Package Manager

In the Console, write the following command to install IronPDF.

Install-Package IronPdf

This command will install the latest version of IronPDF in the project.

Step 3: Code Examples to Convert Excel file to PDF

Open the Program.cs file and write the following code fragments to convert the Excel file to PDF using the IronPDF C# library.

Step 3.1: Include IronPDF

Import the IronPDF in the code file to use it in the program.

using IronPdf;
using IronPdf;
Imports IronPdf
$vbLabelText   $csharpLabel

The above code enables IronPDF to take part in the code.

Step 3.2: Instantiate a ChromePdfRenderer Object

Instantiate a ChromePdfRenderer object in the program. It'll enable the use of IronPDF to convert the HTML file to PDF.

var renderer = new ChromePdfRenderer();
var renderer = new ChromePdfRenderer();
Dim renderer = New ChromePdfRenderer()
$vbLabelText   $csharpLabel

Step 3.3: Convert the HTML (Exported from Excel File) to PDF

Use the RenderHtmlFileAsPdf function to convert HTML files to PDFs. In the parameter list, pass the HTML file path. IronPDF will read the contents of the HTML file and convert it into a PDF document.

var document = renderer.RenderHtmlFileAsPdf(@"C:\Users\Administrator\Desktop\Inventory.html");
var document = renderer.RenderHtmlFileAsPdf(@"C:\Users\Administrator\Desktop\Inventory.html");
Dim document = renderer.RenderHtmlFileAsPdf("C:\Users\Administrator\Desktop\Inventory.html")
$vbLabelText   $csharpLabel

IronPDF also supports adding passwords to the PDF file, preventing the PDF file from editing, opening, printing, and other operations. Let's have a look at how to add passwords using IronPDF.

Add Password to PDF File

Use the SecuritySettings property on the PDF document created in the previous step to set a password. It helps to secure the PDF file from data breaches.

document.SecuritySettings.UserPassword = "editable";
document.SecuritySettings.UserPassword = "editable";
document.SecuritySettings.UserPassword = "editable"
$vbLabelText   $csharpLabel

Step 3.4: Save the PDF File

Now, it's time to save the generated PDF file on the local machine by using the following code.

document.SaveAs("C:\\excelToPdf.pdf");
document.SaveAs("C:\\excelToPdf.pdf");
document.SaveAs("C:\excelToPdf.pdf")
$vbLabelText   $csharpLabel

Output PDF File

When you try to open the file, it'll require a password.

Convert Excel to PDF in C# (Step-by-Step) Tutorial, Figure 5: Enter Password for File Enter Password for File

After entering the password, you can see the converted PDF file from an Excel file. IronPDF beautifully converts Excel documents to PDFs while retaining the original formatting.

Convert Excel to PDF in C# (Step-by-Step) Tutorial, Figure 6: Output PDF File Output PDF File

You can see all the worksheets there. You can open any worksheet by clicking on any worksheet name.

Convert Excel to PDF in C# (Step-by-Step) Tutorial, Figure 6: PDF Worksheets PDF Worksheets

Summary

IronPDF is an easy-to-use PDF converter that can be used in C# .NET. In this blog post, you were shown how to convert Excel to PDF using IronPDF. If you want to learn more about the capabilities of IronPDF or see some examples of how it can be used, check out some of the other articles on how to create a PDF file from HTML.

You can start with a free trial of IronPDF, and then purchase IronPDF for as little as $799. Or, if you need more than one Iron Software product, you can buy the Iron Suite bundle of five software programs for the price of two.

Häufig gestellte Fragen

Warum ist die Umwandlung von Excel in PDF vorteilhaft für Unternehmen?

Die Umwandlung von Excel-Dateien in PDF ist für Unternehmen vorteilhaft, da PDFs universell kompatibel sind und sicherstellen, dass gemeinsam genutzte Dokumente ihr beabsichtigtes Format beibehalten und vor unbefugten Bearbeitungen geschützt sind.

Wie kann ich Excel in PDF in C# umwandeln, ohne das Format zu verlieren?

Um Excel in PDF in C# umzuwandeln, ohne das Format zu verlieren, können Sie die IronPDF-Bibliothek verwenden, die es ermöglicht, Excel-Dateien zuerst als HTML darzustellen und sie dann mit der RenderHtmlFileAsPdf-Methode in PDF zu konvertieren.

Welche Schritte sind nötig, um Excel in PDF mit IronPDF in .NET Core umzuwandeln?

Die Schritte umfassen das Exportieren der Excel-Datei als HTML, die Installation der IronPDF-Bibliothek über NuGet und die Verwendung der ChromePdfRenderer-Klasse, um das HTML in eine PDF zu konvertieren, während das ursprüngliche Format beibehalten wird.

Kann ich ein aus einer Excel-Datei erstelltes PDF mit einem Passwort in C# sichern?

Ja, IronPDF ermöglicht es, die Sicherheit eines PDFs zu erhöhen, indem man mit der SecuritySettings-Eigenschaft ein Benutzerpasswort setzt, was hilft, unbefugten Zugriff zu verhindern.

Gibt es eine Möglichkeit, die PDF-Umwandlung in C# auszuprobieren, bevor man den Kauf einer Bibliothek in Betracht zieht?

Ja, IronPDF bietet eine kostenlose Testversion an, die es Ihnen ermöglicht, seine Funktionen und Fähigkeiten zur PDF-Umwandlung und -Verwaltung in C#-Projekten zu erkunden, bevor Sie einen Kauf tätigen.

Welche Voraussetzungen gibt es für die Installation von IronPDF zur Umwandlung von Excel in PDF?

Zur Installation von IronPDF für die Umwandlung von Excel in PDF benötigen Sie Visual Studio 2022, eine .NET-Anwendung mit dem neuesten .NET Framework, Microsoft Office und eine Internetverbindung, um die Bibliothek von NuGet herunterzuladen.

Unterstützt die IronPDF-Bibliothek das Zusammenführen mehrerer PDFs?

Ja, IronPDF erleichtert nicht nur die Umwandlung von Excel in PDF, sondern bietet auch die Funktionalität zum nahtlosen Zusammenführen mehrerer PDF-Dokumente.

Wie kann ich sicherstellen, dass das ursprüngliche Excel-Format im PDF beibehalten wird?

IronPDF ist darauf ausgelegt, das ursprüngliche Format von Excel-Dateien während der Umwandlung zu erhalten, indem es das Excel zuerst als HTML exportiert und dann das HTML als PDF rendert.

Welche Methode in IronPDF sollte verwendet werden, um HTML-Dateien in PDF zu konvertieren?

Die RenderHtmlFileAsPdf-Methode in IronPDF wird verwendet, um HTML-Dateien, die aus Excel exportiert werden, in PDFs umzuwandeln, während das Format des Dokuments erhalten bleibt.

.NET 10: Unterstützt IronPDF die Konvertierung von Excel in PDF innerhalb eines .NET 10-Projekts ohne Kompatibilitätsprobleme?

Ja. Laut der Dokumentation von IronPDF ist IronPDF vollständig kompatibel mit .NET 10 und bietet standardmäßig Unterstützung für alle modernen .NET-Versionen, einschließlich .NET 10, wodurch eine reibungslose Excel-zu-PDF-Konvertierung ohne spezielle Umwege gewährleistet wird.

Curtis Chau
Technischer Autor

Curtis Chau hat einen Bachelor-Abschluss in Informatik von der Carleton University und ist spezialisiert auf Frontend-Entwicklung mit Expertise in Node.js, TypeScript, JavaScript und React. Leidenschaftlich widmet er sich der Erstellung intuitiver und ästhetisch ansprechender Benutzerschnittstellen und arbeitet gerne mit modernen Frameworks sowie der Erstellung gut strukturierter, optisch ansprechender ...

Weiterlesen