How to Convert XLS to XLSX File in 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".
Open Visual Studio and click on "Create a New Project".
Open Visual Studio
Select "Console Application" and Click "Next".
Create a new project in Visual Studio
Set the name of the Project.
Configure your new project
Select the .NET version. Choose the stable version, .NET 6.0.
.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.
Open "Manage NuGet Packages for Solutions" either from Solution Explorer or Tools.
Navigate to NuGet Package Manager
Browse for the IronXL Library and select the current project. Click install.
Search and install the IronXL package in NuGet Package Manager UI
Add the following namespace at the top of the Program.cs
file:
using IronXL;
using IronXL;
Imports IronXL
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")
The sample file loaded is as follows:
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. The following code shows how to save an XLS file to an XLSX file:
// 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")
That's it. We have successfully converted an XLS to XLSX format. To convert to other formats, you can visit this code examples page.
Output
The exported XLSX file
You can see that the data is the same even after conversion. IronXL provides ease of use, speed, and accuracy.
Summary
This article demonstrated how to convert XLS Excel files to XLSX files using IronXL in C#. IronXL provides the facility to work with existing Excel files without any hassle. Conversion between different spreadsheet formats is usually a one-line process. It's fast, easy to use, and accurate. It also allows you to create new Excel files and write data to them with easy syntax. IronXL can also be used to read Excel files without Microsoft Office installed. To read data from Excel files, you can see this code example page.
IronXL is free for development and can be licensed for commercial use. You can also try IronXL for a free trial for commercial use. Download the software from the IronXL website.
Frequently Asked Questions
How can I convert an XLS file to an XLSX file in C#?
You can convert an XLS file to an XLSX file using the IronXL library in C#. Load the XLS file with the LoadExcel
method and save it as an XLSX file using the SaveAs
method.
Is Microsoft Excel required to convert XLS files to XLSX in C#?
No, Microsoft Excel is not required. The IronXL library allows you to convert XLS files to XLSX format without needing Excel installed on your machine.
What are the benefits of using IronXL for Excel file conversions?
IronXL provides efficient and straightforward methods for converting Excel files, supports multiple Excel formats, offers features like password protection and image extraction, and is compatible with various .NET versions.
Can IronXL handle different Excel file formats?
Yes, IronXL supports multiple Excel file formats including XLS, XLSX, XLSM, CSV, and TSV, allowing for versatile data manipulation and conversion.
What steps are needed to install IronXL in a C# project?
To install IronXL, open your C# project in Visual Studio, use the NuGet Package Manager to search for the IronXL package, and install it for your project.
Does IronXL support cross-platform compatibility?
Yes, IronXL supports cross-platform compatibility and is compatible with .NET 7, 6, 5, .NET Core 2 and 3, and .NET Framework 4.5 and onwards.
What are some advanced features of the IronXL library?
IronXL offers advanced features such as password protection, image extraction, and data manipulation capabilities, making it a powerful tool for handling Excel files.
Is there a licensing cost for using IronXL?
IronXL is free for development use. For commercial use, a license is required, but a free trial is available to explore its features.