Friday, 28 January 2022

Convert Excel to Office Open XML and Vice Versa in Java

Office Open XML (also informally called OOXML) is a zipped, XML-based file format used to represent spreadsheets, charts, presentations and word processing documents. This article will show you how to convert Excel to Office Open XML and vice versa using Java codes.

DEPENDENCY

First of all, you're required to download the package of Free Spire.XLS for Java from this link, and then add the Spire.Xls.jar file as a dependency in your Java program. If you use Maven, you can add the following code to the pom.xml file to import the JAR 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.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

Convert Excel to Office Open XML

Free Spire.XLS for Java offers the Workbook.saveAsXml() method to convert Excel to Office Open XML. The following are detailed steps.

l  Create a Workbook instance.

l  Load a sample Excel document using Workbook.loadFromFile() method.

l  Save the document as Office Open XML using Workbook.saveAsXml() method.

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("C:\\Users\\Test1\\Desktop\\Sample.xlsx");

//Save as Office Open XML file format
workbook.saveAsXml("output/ToOpenXML.xml");
}
}


Convert Office Open XML to Excel

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

l  Create a Workbook instance.

l  Load an Office Open XML file using Workbook.loadFromXml() file.

l  Use Workbook.saveToFile() method to save the Office Open XML file as Excel.

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("C:\\Users\\Test1\\Desktop\\ToOpenXML.xml");

//Save as Excel XLSX file format
workbook.saveToFile("output/ToExcel.xlsx", ExcelVersion.Version2016);
}
}







No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...