Przejdź do treści stopki
KORZYSTANIE Z IRONPDF

Jak stworzyć PDF w ASP .NET

Microsoft Excel is a spreadsheet software that stores and organizes data, and presents it in various formats. It is widely used for financial data due to its useful formulas. The IronXL software library can be used to import and read Excel files in C#.

IronXL — biblioteka IronXL

IronXL - .NET Excel Library is a .NET Excel library that prioritizes ease of use, accuracy, and speed for its users. It helps you import and read Excel documents and create and edit an Excel file efficiently with lightning-fast performance. It works without MS Office Interop. This means even without Excel installed, it provides all the functionalities to read Excel files. This makes IronXL a powerful tool for developers to import and read Excel files in C#.

IronXL is available on all platforms like Windows, Linux, macOS, Docker, Azure, and AWS. It is compatible with all .NET Frameworks. IronXL is a versatile library that can be integrated into Console, Desktop, and Web ASP.NET Applications. It supports different workbook formats like XLS and XLSX files, XSLT and XLSM, CSV, and TSV.

Some Important Features

  • Open, read Excel files, and search data from different spreadsheet formats like XLS/CSV/TSV/XLSX files.
  • Export Excel Worksheets to XLS/XLSX/CSV/TSV/JSON.
  • Encrypt and decrypt XLSM/XLTX/XLSX files with passwords.
  • Import Excel sheets as System.Data.DataSet and System.Data.DataTable objects.
  • Excel file formulas are recalculated every time a sheet is edited.
  • Intuitive cell range settings with a WorkSheet["A1:B10"] easy syntax.
  • Sort Cell Ranges, Columns, and Rows.
  • Styling Cells - Font, Font Size, Background color, Border, Alignment, and Numbering formats.

How to Import Excel Workbook in C

Wymagania wstępne

To use IronXL in C# to read Excel files, the first step is to ensure the following components are installed on the local computer:

  1. Visual Studio - It is the official IDE for developing C# .NET applications. You can download and install Visual Studio from the Visual Studio Download Page.
  2. IronXL - It is the library that helps work with Excel sheets in C#. It must be installed in a C# program before using it. IronXL can be downloaded from the IronXL NuGet Package or Manage NuGet packages in Visual Studio tools. You can also download the .NET Excel DLL from Iron Software's website.

Adding Necessary Namespaces

Once Visual Studio and IronXL are installed, the IronXL assembly reference for using IronXL should be included in the source code. Dodaj następujący wiersz kodu na początku pliku w nowym projekcie, w którym będą używane funkcje IronXL:

using IronXL;
using IronXL;
Imports IronXL
$vbLabelText   $csharpLabel

Open an Existing Excel file in C

Microsoft Excel Spreadsheets are also referred to as Excel Workbook. Each workbook contains multiple worksheets, and a single worksheet contains tabular cells with its value. To open and read an Excel file using IronXL, it should be loaded using the WorkBook class and Load method present in the IronXL library. Kod wygląda następująco:

// Supported Excel spreadsheet formats for reading include: XLSX, XLS, CSV, and TSV
WorkBook workbook = WorkBook.Load("test.xlsx");
// Supported Excel spreadsheet formats for reading include: XLSX, XLS, CSV, and TSV
WorkBook workbook = WorkBook.Load("test.xlsx");
' Supported Excel spreadsheet formats for reading include: XLSX, XLS, CSV, and TSV
Dim workbook As WorkBook = WorkBook.Load("test.xlsx")
$vbLabelText   $csharpLabel

Powoduje to otwarcie pliku Excel w zmiennej odwołującej się do instancji skoroszytu. As it can have multiple worksheets, it can be used to open a specific worksheet or all at once. The following code opens the first WorkSheet in the sheet instance variable:

WorkSheet sheet = workbook.WorkSheets.First();
WorkSheet sheet = workbook.WorkSheets.First();
Dim sheet As WorkSheet = workbook.WorkSheets.First()
$vbLabelText   $csharpLabel

Spowoduje to otwarcie pierwszego arkusza w pliku Excel, a teraz dane Excelu mogą być odczytywane z tego arkusza i zapisywane w nim.

Opened Excel file

How to Create PDF in ASP .NET, Figure 1: Excel file Excel file

Read Data from Imported Excel file

Once the Excel file is imported, it is ready for reading data. Odczytywanie danych z plików Excel w języku C# przy użyciu IronXL jest bardzo proste i łatwe. You can read Excel cell values by simply mentioning the cell reference number.

Poniższy kod pobiera wartość komórki o numerze odniesienia "C2":

// Select cells easily in Excel-notation and return the value
int cellValue = sheet["C2"].IntValue;

// Display the value
Console.WriteLine(cellValue);
// Select cells easily in Excel-notation and return the value
int cellValue = sheet["C2"].IntValue;

// Display the value
Console.WriteLine(cellValue);
' Select cells easily in Excel-notation and return the value
Dim cellValue As Integer = sheet("C2").IntValue

' Display the value
Console.WriteLine(cellValue)
$vbLabelText   $csharpLabel

Oto wynik:

How to Create PDF in ASP .NET, Figure 2: Read Excel Read Excel

Now, let's read data from a range of cells in the opened Excel file. Kod wygląda następująco:

// Read from a range of cells elegantly.
foreach (var cell in sheet["A2:A6"])
{
    Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
// Read from a range of cells elegantly.
foreach (var cell in sheet["A2:A6"])
{
    Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
' Read from a range of cells elegantly.
For Each cell In sheet("A2:A6")
	Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text)
Next cell
$vbLabelText   $csharpLabel

Kod jest bardzo prosty, przejrzysty i zrozumiały. The range of cells can be referenced with simple syntax as shown in a foreach loop: sheet["A2:A6"] and each cell can be iterated using a foreach loop to get its value. Here, you will see the names in column A from row 2 to row 6 on the console output:

How to Create PDF in ASP .NET, Figure 3: Read Range of Cells Read Range of Cells

For more details on reading and writing cell values, check this tutorial on reading Excel files in C#.

Import all Data from an Excel file

IronXL może być używany do odczytu arkuszy Excel jednocześnie przy użyciu indeksów wierszy i kolumn. The following IronXL code samples help to get the entire Excel file data in the same format on the console output:

WorkBook workbook = WorkBook.Load("test.xlsx");
WorkSheet sheet = workbook.WorkSheets.First();

// Traverse all rows of Excel WorkSheet
for (int i = 0; i < sheet.Rows.Count(); i++)
{
    // Traverse all columns of specific Row
    for (int j = 0; j < sheet.Columns.Count(); j++)
    {
        // Get the value as a string
        string val = sheet.Rows[i].Columns[j].Value.ToString();
        Console.Write("{0}\t", val);
    }
    Console.WriteLine();
}
WorkBook workbook = WorkBook.Load("test.xlsx");
WorkSheet sheet = workbook.WorkSheets.First();

// Traverse all rows of Excel WorkSheet
for (int i = 0; i < sheet.Rows.Count(); i++)
{
    // Traverse all columns of specific Row
    for (int j = 0; j < sheet.Columns.Count(); j++)
    {
        // Get the value as a string
        string val = sheet.Rows[i].Columns[j].Value.ToString();
        Console.Write("{0}\t", val);
    }
    Console.WriteLine();
}
Imports Microsoft.VisualBasic

Dim workbook As WorkBook = WorkBook.Load("test.xlsx")
Dim sheet As WorkSheet = workbook.WorkSheets.First()

' Traverse all rows of Excel WorkSheet
For i As Integer = 0 To sheet.Rows.Count() - 1
	' Traverse all columns of specific Row
	For j As Integer = 0 To sheet.Columns.Count() - 1
		' Get the value as a string
		Dim val As String = sheet.Rows(i).Columns(j).Value.ToString()
		Console.Write("{0}" & vbTab, val)
	Next j
	Console.WriteLine()
Next i
$vbLabelText   $csharpLabel

Plik wyjściowy

How to Create PDF in ASP .NET, Figure 4: The Console output of reading an Excel file The Console output of reading an Excel file

Podsumowanie

In this article, we learned how to import and read an Excel file in C# without any Microsoft Excel installed. Then we considered multiple ways to read data from an Excel spreadsheet. IronXL also helps create Excel files in C# without any Excel installed.

IronXL provides an all-in-one solution for all MS Excel document-related tasks to be implemented programmatically. You can perform formula calculation, string or number sorting, trimming and appending, find and replace, merge and unmerge, save files, etc. You can edit cell values and also set cell data formats along with validating spreadsheet data. Obsługuje również pliki CSV i ułatwia pracę z danymi w formacie podobnym do Excela.

Wypróbuj IronXL za darmo i poznaj jego funkcje. It can be licensed for commercial use with its Lite package starting at only $799.

Często Zadawane Pytania

Jak mogę utworzyć plik PDF z aplikacji ASP.NET bez utraty formatowania?

Za pomocą IronPDF można tworzyć pliki PDF z aplikacji ASP.NET, zachowując oryginalne formatowanie. IronPDF zapewnia, że układ i style treści HTML są dokładnie odwzorowane w wynikowym pliku PDF.

Jaki jest najlepszy sposób konwersji HTML do PDF w aplikacji internetowej napisanej w języku C#?

IronPDF to doskonały wybór do konwersji HTML na PDF w aplikacji internetowej napisanej w języku C#. Oferuje on metody, takie jak RenderHtmlAsPdf, służące do konwersji ciągów znaków HTML i stron internetowych na wysokiej jakości dokumenty PDF.

Czy możliwe jest generowanie plików PDF w ASP.NET bez użycia wtyczek innych firm?

Tak, korzystając z IronPDF, można generować pliki PDF w aplikacjach ASP.NET bez konieczności korzystania z wtyczek innych firm lub oprogramowania zewnętrznego. IronPDF integruje się bezpośrednio z aplikacją, obsługując tworzenie plików PDF.

Jak zapewnić dokładność układu pliku PDF podczas konwersji z HTML w języku C#?

IronPDF oferuje precyzyjną kontrolę nad układem i formatowaniem plików PDF podczas konwersji HTML do PDF w języku C#. Zachowuje stylizację CSS, podziały stron i zapytania o media, aby zapewnić zgodność wyniku z oryginalnym projektem.

Czy IronPDF może służyć do szyfrowania i zabezpieczania dokumentów PDF?

Tak, IronPDF oferuje funkcje szyfrowania i zabezpieczania dokumentów PDF. Można zastosować ochronę hasłem oraz ustawić uprawnienia, aby kontrolować dostęp i możliwości edycji plików PDF.

Jakie platformy wspiera IronPDF dla generowania PDF?

IronPDF obsługuje generowanie plików PDF na wielu platformach, w tym Windows, Linux, macOS, Docker, Azure i AWS, dzięki czemu jest wszechstronnym rozwiązaniem dla różnych środowisk wdrożeniowych.

Jak radzisz sobie z obrazami podczas tworzenia plików PDF z HTML w ASP.NET?

Podczas korzystania z IronPDF obrazy zawarte w treści HTML są automatycznie osadzane w pliku PDF podczas konwersji. Obsługuje różne formaty obrazów, zapewniając ich prawidłowe wyświetlanie w końcowym dokumencie.

Czy możliwe jest zautomatyzowanie tworzenia plików PDF w aplikacjach ASP.NET?

Oczywiście, IronPDF umożliwia automatyzację tworzenia plików PDF w aplikacjach ASP.NET. Można zaplanować lub uruchomić generowanie plików PDF na podstawie zdarzeń lub określonych warunków w aplikacji.

Czy IronPDF w pełni obsługuje .NET 10 dla ASP.NET i innych typów projektów .NET?

Tak. IronPDF jest w pełni kompatybilny z .NET 10. Obsługuje wszystkie nowoczesne wersje .NET, w tym .NET 10, w projektach internetowych, desktopowych, konsolowych, Blazor i MAUI. Można korzystać z tych samych interfejsów API, takich jak ChromePdfRenderer, RenderHtmlAsPdf i metod asynchronicznych, tak jak w poprzednich wersjach .NET.

Curtis Chau
Autor tekstów technicznych

Curtis Chau posiada tytuł licencjata z informatyki (Uniwersytet Carleton) i specjalizuje się w front-endowym rozwoju, z ekspertką w Node.js, TypeScript, JavaScript i React. Pasjonuje się tworzeniem intuicyjnych i estetycznie przyjemnych interfejsów użytkownika, Curtis cieszy się pracą z nowoczesnymi frameworkami i tworzeniem dobrze zorganizowanych, atrakcyjnych wizualnie podrę...

Czytaj więcej

Zespol wsparcia Iron

Jestesmy online 24 godziny, 5 dni w tygodniu.
Czat
Email
Zadzwon do mnie