How to Convert XLS to XLSX File in C#

Microsoft Excel is a spreadsheet software that stores data in tabular form. If you are a C# developer, you must have come across working with excel files. It is a tedious task to build an application from scratch to work with excel files. Sometimes you need to convert between different Excel formats, especially from the older XLS file to the XLSX file.

Difference between XLS and XLSX formats

XLS is the extension of Excel file which was default for Microsoft Office 97-2003. Microsoft 2007 and onwards use XLSX as the default format. XLS file format uses the standard binary format, while the latest XLSX file format is the updated version and is based on XML format.

This XLS to XLSX can be sped up using a .NET DLL library. There are a bunch of libraries available now which makes the task easier. One of those libraries is IronXL.

Here in this article, we are going to explore the IronXL library to convert XLS to XLSX format in the 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 in terms of 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.

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 Excel spreadsheets.

Create a Console Application

We are going to use Visual Studio 2022 IDE for creating an application to start with. Visual Studio is the official IDE for C# development, and you must have installed it. You can download it from the Microsoft Visual Studio website, if not 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

  2. Select Console Application and Click Next

    How to Convert XLS to XLSX File in C#: Figure 2

  3. Set the name of the Project

    How to Convert XLS to XLSX File in C#: Figure 3

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

    How to Convert XLS to XLSX File in C#: Figure 4

Install IronXL Library

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

  1. Open NuGet Package Manager for Solutions either from Solution explorer or Tools.

    How to Convert XLS to XLSX File in C#: Figure 5

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

    How to Convert XLS to XLSX File in C#: Figure 6

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

using IronXL;
using IronXL;
Imports IronXL
VB   C#

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.

// Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
WorkBook workBook = WorkBook.LoadExcel("sample.xls");
// Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
WorkBook workBook = WorkBook.LoadExcel("sample.xls");
' Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
Dim workBook As WorkBook = WorkBook.LoadExcel("sample.xls")
VB   C#

The sample file loaded is as follows:

How to Convert XLS to XLSX File in C#: Figure 7

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 XLS to an XLSX file, IronXL just uses the Save method to do this conversion. The following code shows how to save an XLS file to an XLSX file:

workBook.SaveAs("converted_sample.xlsx");
workBook.SaveAs("converted_sample.xlsx");
workBook.SaveAs("converted_sample.xlsx")
VB   C#

That's it. We have successfully converted XLS to XLSX format. To convert to other formats you can visit this code examples page.

Output

How to Convert XLS to XLSX File in C#: Figure 8

You can see that the data is the same even after conversion. IronXL provides ease-of-use, speed, and accuracy.

Summary

In this article, we looked at 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 in usually a one-line process. It's fast, ease to use, and accurate. It also allows you to create new Excel files and write data to it 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 free for a free trial for commercial use. Download the software from here.