Java: Convert Excel to Office Open XML and Vice Versa

2023-12-06 02:02:22

Install with Maven

<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.xls</artifactId>
    <version>14.1.3</version>
</dependency>
    

Related Links

Office Open XML (also referred to as OOXML) is a zipped, XML-based format for Excel, Word and Presentation documents. Sometimes, you may need to convert an Excel file to Office Open XML in order to make it readable on various applications and platforms. Likewise, you might also want to convert Office Open XML to Excel for data calculations. In this article, you will learn how to Convert Excel to Office Open XML and vice versa in Java using Spire.XLS for Java library.

Install Spire.XLS for Java

First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls</artifactId>
        <version>14.11.0</version>
    </dependency>
</dependencies>
    

Convert Excel to Office Open XML in Java

The following are the steps to convert an Excel file to Office Open XML:

  • Create an instance of Workbook class.
  • Load an Excel file using Workbook.loadFromFile() method.
  • Call Workbook.saveAsXml() method to save the Excel file as Office Open XML.
  • Java
import com.spire.xls.Workbook;
    
    public class ExcelToOpenXML {
        public static void main(String []args){
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.loadFromFile("Sample.xlsx");
    
            //Save as Office Open XML file format
            workbook.saveAsXml("ToXML.xml");
        }
    }

Java: Convert Excel to Office Open XML and Vice Versa

Convert Office Open XML to Excel in Java

The following are the steps to convert an Office Open XML file to Excel:

  • Create an instance of Workbook class.
  • Load an Office Open XML file using Workbook.loadFromXml() file.
  • Call Workbook.saveToFile() method to save the Office Open XML file as Excel.
  • Java
import com.spire.xls.ExcelVersion;
    import com.spire.xls.Workbook;
    
    public class OpenXmlToExcel {
        public static void main(String []args){
            //Create an instance of Workbook class
            Workbook workbook = new Workbook();
            //Load an Office Open XML file
            workbook.loadFromXml("ToXML.xml");
    
            //Save as Excel XLSX file format
            workbook.saveToFile("ToExcel.xlsx", ExcelVersion.Version2016);
        }
    }

Java: Convert Excel to Office Open XML and Vice Versa

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

.

See Also