Przejdź do treści stopki
KORZYSTANIE Z IRONXL

Jak przekonwertować plik XLS na XLSX w języku C#

This article will explore the IronXL library to convert XLS to XLSX format in a C# Console Application.

IronXL - An Excel Library

IronXL is a .NET Excel Library that helps developers create, read, and edit Excel files in C# applications. It is fast and accurate regarding performance and output, respectively. It supports all Excel workbook file formats (XLS, XLSX, XLSM, CSV, TSV). IronXL also allows data to be saved or exported from Excel formats (XLS, XLSX) to JSON, HTML, Binary, Byte Array, DataSet, or DataTable.

IronXL allows developers to work with worksheets and cell ranges elegantly. With IronXL, you can edit formulas. Recalculation of the already given formula in the sheet is very easy to do. Data can be sorted based on range, column, or row. It provides you the facility to edit layouts like freeze panes, auto-size rows/columns, and add and remove rows/columns easily.

With IronXL, Excel files can be protected with a user password and permission to edit the file. Another feature is that you can add, remove, and extract images from Excel worksheets. It also provides a wide range of Excel functions with almost all cell data formats. This makes IronXL one of the most intuitive APIs when it comes to working with Excel files.

IronXL does not need Microsoft Excel to be installed on the machine as it does not require Office Interop or any other dependency. It works on multiple platforms and is compatible with .NET 7, 6 & 5. It also supports .NET Core 2 and 3 along with the standard 2 versions. .NET Framework 4.5 and onwards is also supported for working on Excel spreadsheets.

Create a Console Application

The latest version of Visual Studio is recommended for creating an application to start with. Visual Studio is the official IDE for C# development, and you must have it installed. You can download it from the Microsoft Visual Studio website if it's not already installed.

The following steps will create a new project named "DemoApp".

  1. Open Visual Studio and click on "Create a New Project".

    How to Convert XLS to XLSX File in C#, Figure 1: Open Visual Studio Open Visual Studio

  2. Select "Console Application" and Click "Next".

    How to Convert XLS to XLSX File in C#, Figure 2: Create a new project in Visual Studio Create a new project in Visual Studio

  3. Set the name of the Project.

    How to Convert XLS to XLSX File in C#, Figure 3: Configure your new project Configure your new project

  4. Select the .NET version. Choose the stable version, .NET 6.0.

    How to Convert XLS to XLSX File in C#, Figure 4: .NET Framework selection .NET Framework selection

Install IronXL Library

Once the project is created, the IronXL library needs to be installed in the project for use. Follow these steps to install it.

  1. Open "Manage NuGet Packages for Solutions" either from Solution Explorer or Tools.

    How to Convert XLS to XLSX File in C#, Figure 5: Navigate to NuGet Package Manager Navigate to NuGet Package Manager

  2. Browse for the IronXL Library and select the current project. Click install.

    How to Convert XLS to XLSX File in C#, Figure 6: Search and install the IronXL package in NuGet Package Manager UI Wyszukaj i zainstaluj pakiet IronXL w interfejsie użytkownika NuGet Package Manager

Add the following namespace at the top of the Program.cs file:

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

Open an Existing XLS file

IronXL provides the facility to open an existing Excel file, or you can create a new Excel file. Here we are going to open an existing Excel XLS file using the C# IronXL LoadExcel method. The Load method is a short synonym for LoadExcel.

// Load an existing Excel file. Supported formats include XLSX, XLS, XLSM, XLTX, CSV and TSV.
WorkBook workBook = WorkBook.LoadExcel("sample.xls");
// Load an existing Excel file. Supported formats include XLSX, XLS, XLSM, XLTX, CSV and TSV.
WorkBook workBook = WorkBook.LoadExcel("sample.xls");
' Load an existing Excel file. Supported formats include XLSX, XLS, XLSM, XLTX, CSV and TSV.
Dim workBook As WorkBook = WorkBook.LoadExcel("sample.xls")
$vbLabelText   $csharpLabel

The sample file loaded is as follows:

How to Convert XLS to XLSX File in C#, Figure 7: A sample XLS data file A sample XLS data file

To create Excel files in XLS or XLSX format, you can visit this code examples page here.

Convert XLS File to XLSX File

To convert an XLS to an XLSX file, IronXL just uses the SaveAs method to do this conversion. Poniższy kod pokazuje, jak zapisać plik XLS jako plik XLSX:

// Convert XLS to XLSX format by saving it with a different file extension.
workBook.SaveAs("converted_sample.xlsx");
// Convert XLS to XLSX format by saving it with a different file extension.
workBook.SaveAs("converted_sample.xlsx");
' Convert XLS to XLSX format by saving it with a different file extension.
workBook.SaveAs("converted_sample.xlsx")
$vbLabelText   $csharpLabel

To wszystko. Pomyślnie przekonwertowaliśmy plik XLS do formatu XLSX. Aby przekonwertować na inne formaty, możesz odwiedzić tę stronę z przykładami kodu.

Wynik

How to Convert XLS to XLSX File in C#, Figure 8: The exported XLSX file Eksportowany plik XLSX

Widać, że dane pozostają niezmienione nawet po konwersji. IronXL zapewnia łatwość obsługi, szybkość i dokładność.

Podsumowanie

W tym artykułe pokazano, jak konwertować pliki XLS programu Excel na pliki XLSX przy użyciu biblioteki IronXL w języku C#. IronXL umożliwia bezproblemową pracę z istniejącymi plikami Excel. Konwersja między różnymi formatami arkuszy kalkulacyjnych to zazwyczaj proces zajmujący jedną linię kodu. Jest szybki, łatwy w użyciu i dokładny. Pozwala również na tworzenie nowych plików Excel i zapisywanie w nich danych przy użyciu prostej składni. IronXL może być również używany do odczytu plików Excel bez zainstalowanego pakietu Microsoft Office. Aby odczytać dane z plików Excel, można zapoznać się z tą stroną zawierającą przykładowy kod.

IronXL jest bezpłatny do celów programistycznych, a na cele komercyjne można uzyskać licencję. Możesz również wypróbować IronXL w ramach bezpłatnej wersji próbnej do użytku komercyjnego. Pobierz oprogramowanie ze strony internetowej IronXL.

Często Zadawane Pytania

Jak przekonwertować plik XLS na plik XLSX w języku C#?

Możesz przekonwertować plik XLS na plik XLSX za pomocą biblioteki IronXL w języku C#. Załaduj plik XLS za pomocą metody LoadExcel i zapisz go jako plik XLSX za pomocą metody SaveAs.

Czy do konwersji plików XLS do formatu XLSX w języku C# wymagany jest program Microsoft Excel?

Nie, program Microsoft Excel nie jest wymagany. Biblioteka IronXL umożliwia konwersję plików XLS do formatu XLSX bez konieczności instalowania programu Excel na komputerze.

Jakie są zalety korzystania z IronXL do konwersji plików Excel?

IronXL zapewnia wydajne i proste metody konwersji plików Excel, obsługuje wiele formatów Excel, oferuje funkcje takie jak ochrona hasłem i wyodrębnianie obrazów oraz jest kompatybilny z różnymi wersjami .NET.

Czy IronXL obsługuje różne formaty plików Excel?

Tak, IronXL obsługuje wiele formatów plików Excel, w tym XLS, XLSX, XLSM, CSV i TSV, co pozwala na wszechstronną manipulację danymi i ich konwersję.

Jakie kroki należy wykonać, aby zainstalować IronXL w projekcie C#?

Aby zainstalować IronXL, otwórz projekt C# w Visual Studio, użyj menedżera pakietów NuGet, aby wyszukać pakiet IronXL, a następnie zainstaluj go w swoim projekcie.

Czy IronXL obsługuje kompatybilność międzyplatformową?

Tak, IronXL obsługuje kompatybilność międzyplatformową i jest kompatybilny z .NET 7, 6, 5, .NET Core 2 i 3 oraz .NET Framework 4.5 i nowszymi wersjami.

Jakie są zaawansowane funkcje biblioteki IronXL?

IronXL oferuje zaawansowane funkcje, takie jak ochrona hasłem, wyodrębnianie obrazów i możliwości manipulacji danymi, co czyni go potężnym narzędziem do obsługi plików Excel.

Czy korzystanie z IronXL wiąże się z kosztami licencji?

IronXL jest bezpłatny do użytku deweloperskiego. Do użytku komercyjnego wymagana jest licencja, ale dostępna jest bezpłatna wersja próbna, która pozwala zapoznać się z jego funkcjami.

Jordi Bardia
Inżynier oprogramowania
Jordi jest najbardziej biegły w Pythonie, C# i C++. Kiedy nie wykorzystuje swoich umiejętności w Iron Software, programuje gry. Dzieląc odpowiedzialność za testowanie produktów, rozwój produktów i badania, Jordi wnosi ogromną wartość do ciągłej poprawy produktów. Różnorodne doświadczenia ...
Czytaj więcej

Zespol wsparcia Iron

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