DEV Community

Cover image for How to Convert XLS to XLSX and Vice Versa Using Java
Leon Davis
Leon Davis

Posted on

How to Convert XLS to XLSX and Vice Versa Using Java

When working with Excel files, it's common to encounter two primary formats: .xls and .xlsx. The .xls format is used by older versions of Excel, while .xlsx is the default format for Excel 2007 and later. Converting between these formats is often necessary, whether you're upgrading legacy files or ensuring compatibility with different systems.

In this article, we'll explore how to convert Excel files from .xls to .xlsx and vice versa using Java. This guide is suitable for developers who need an efficient, reliable solution for handling Excel files programmatically.

Why Convert XLS to XLSX?

Before diving into the conversion process, let's briefly discuss why you might need to convert an .xls file to .xlsx. The newer .xlsx format has several advantages over .xls:

  • Smaller file sizes : The .xlsx format uses ZIP compression, reducing the overall file size.
  • Better performance : Excel 2007 and later versions offer improved performance with .xlsx files, especially when dealing with large datasets.
  • Enhanced features : The .xlsx format supports new Excel features such as more than 65,000 rows and 16,000 columns, along with advanced formulas and formatting options.

However, older systems and some third-party applications may still require .xls files. For these cases, converting from .xlsx to .xls is also useful.

Tools for Converting Excel Files in Java

Java developers can use libraries like Apache POI or Spire.XLS to handle Excel file conversions. Apache POI is a popular open-source library for working with Microsoft Office formats, while Spire.XLS offers a more streamlined approach, especially when dealing with both .xls and .xlsx formats.

Here, we'll focus on Spire.XLS , as it provides an easy-to-use API for both file reading and writing. It supports a wide range of Excel functionalities, including format conversion, and is compatible with both .xls and .xlsx formats.

Steps to Convert XLS to XLSX Using Java

Here is a step-by-step guide on how to convert an .xls file to .xlsx using Java:

  • Add Spire.XLS to Your Project

First, you'll need to add the required library to your Java project. You can download the library from the official website or use Maven to include it in your project.

If you're using Maven, add the following dependency to your pom.xml:

   <repositories>
       <repository>
           <id>com.e-iceblue</id>
           <name>e-iceblue</name>
           <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
       </repository>
   </repositories>
   <dependency>
       <groupId>e-iceblue</groupId>
       <artifactId>spire.xls</artifactId>
       <version>15.10.5</version>
   </dependency>
Enter fullscreen mode Exit fullscreen mode
  • Load the XLS File and Save it as an XLSX File

Once you have installed the library, the next step is to load the .xls file you want to convert and save it as a .xlsx file. Here’s how to do that:

   import com.spire.xls.*;

   public class ExcelConversion {
       public static void main(String[] args) {
           // Create a workbook object
           Workbook workbook = new Workbook();

           // Load the .xls file
           workbook.loadFromFile("input.xls");

           // Convert and save as .xlsx
           workbook.saveToFile("output.xlsx", ExcelVersion.Version2013);
       }
   }
Enter fullscreen mode Exit fullscreen mode

In this example, the loadFromFilemethod is used to load the.xlsfile, and then thesaveToFilemethod converts and saves it as an.xlsxfile. TheExcelVersion.Version2013argument specifies the.xlsx format.

  • Run the Program

Now, simply run your program, and the .xls file will be converted to .xlsx and saved as output.xlsx.

Converting XLSX to XLS in Java

To convert .xlsx to .xls, the process is similar. You just need to swap the input and output formats:

import com.spire.xls.*;

public class ExcelConversion {
    public static void main(String[] args) {
        // Create a workbook object
        Workbook workbook = new Workbook();

        // Load the .xlsx file
        workbook.loadFromFile("input.xlsx");

        // Convert and save as .xls
        workbook.saveToFile("output.xls", ExcelVersion.Version97To2003);
    }
}
Enter fullscreen mode Exit fullscreen mode

In this code, the ExcelVersion.Version97To2003 argument specifies the older .xls format.

Important Notes

  • File Size and Features : When converting from .xlsx to .xls, keep in mind that the .xls format has limitations. For example, it supports fewer rows and columns and lacks support for some newer Excel features compared with the .xlsx format. Therefore, some data or formatting may be lost in the conversion.
  • Compatibility : Always test your converted files in the target version of Excel to ensure they work as expected.
  • Error Handling : It’s a good practice to add error handling to manage issues like invalid file formats or file access errors.

Conclusion

Converting Excel files between .xls and .xlsx formats is a common requirement in many Java applications. By following the steps outlined in this guide, you can easily integrate Excel file conversion into your Java applications and work with both .xls and .xlsx formats with ease.

Top comments (0)