Program Guide (129)
Children categories
Worksheets with a lot of complex and detailed information are difficult to read and analyze. To create a more compact and understandable spreadsheet view, you can organize data in groups and collapse the rows with similar content.
This article demonstrates how to programmatically group or ungroup rows and columns in Excel using Spire.XLS for Java.
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>15.12.15</version>
</dependency>
</dependencies>
Group Rows and Columns in Java
The following are the steps to group rows and columns using Spire.XLS for Java.
- Create a Workbook object.
- Load a sample Excel file using Workbook.loadFromFile() method.
- Get the specific sheet using Workbook.getWorksheets().get() method.
- Group rows using Worksheet.groupByRows() method.
- Group columns using Worksheet.groupByColumns() method.
- Save the result to another Excel file using Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class GroupRowsAndColumns {
public static void main(String[] args) {
//Create a Workbook object
Workbook workbook = new Workbook();
//Load a sample Excel file
workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Group rows
sheet.groupByRows(2, 5, false);
sheet.groupByRows(7, 10, false);
//Group columns
sheet.groupByColumns(5, 6, false);
//Save to another Excel file
workbook.saveToFile("GroupRowsAndColumns.xlsx", ExcelVersion.Version2016);
}
}

Ungroup Rows and Columns in Java
The following are the steps to ungroup rows and columns using Spire.XLS for Java.
- Create a Workbook object.
- Load a sample Excel file using Workbook.loadFromFile() method.
- Get the specific sheet using Workbook.getWorksheets().get() method.
- Ungroup rows using Worksheet.ungroupByRows() method.
- Ungroup columns using Worksheet.ungroupByColumns() method.
- Save the result to another Excel file using Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class UngroupRowsAndColumns {
public static void main(String[] args) {
//Create a Workbook object
Workbook workbook = new Workbook();
//Load a sample Excel file
workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\ample.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Ungroup rows
sheet.ungroupByRows(2, 5);
sheet.ungroupByRows(7, 10);
//Ungroup columns
sheet.ungroupByColumns(5, 6);
//Save to a different Excel file
workbook.saveToFile("UngroupRowsAndColumns.xlsx", ExcelVersion.Version2016);
}
}

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.
In Microsoft Excel, suppose a workbook contains lots of worksheets and you need to count them, you can use the Sheets Function, the Define Name Command or a simple VBA code to achieve it. Within this tutorial, I’ll show you how to programmatically count the number of worksheets in Excel using Spire.XLS for Java.
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>15.12.15</version>
</dependency>
</dependencies>
Count the Number of Worksheets in Excel
Spire.XLS for Java supports counting the number of worksheets in Excel using the getCount() method provided by the IWorksheets interface. The following are detailed steps.
- Create a Workbook instance.
- Load a sample Excel document using Workbook.loadFromFile() method.
- Get a collection of worksheets using Workbook.getWorksheets() method and obtain the number of worksheets in the collection using the getCount() method.
- Java
import com.spire.xls.Workbook;
public class CountNumberOfWorsheets {
public static void main(String[] args) {
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load a sample Excel file
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.xlsx");
//Get the number of worksheets
int sheetCount=workbook.getWorksheets().getCount();
//Output the result
System.out.println("The number of sheets is "+sheetCount);
}
}

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.
Sometimes we need to hide some rows and columns in Excel worksheets so that the data appears completely on one screen. At other times, we need to show all the hidden rows and columns to view the data completely. In this article, you will learn how to hide and show rows or columns in Excel in Java applications from the following four parts.
- Hide Excel Rows and Columns
- Show Hidden Rows and Columns in Excel
- Hide Multiple Rows and Columns
- Show All Hidden Rows and Columns
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>15.12.15</version>
</dependency>
</dependencies>
Hide Rows and Columns
The detailed steps are listed as below.
- Create a Workbook instance and load a sample Excel document using Workbook.loadFromFile() method.
- Get a specified worksheet using Workbook.getWorksheets().get() method.
- Hide a specific column using Worksheet.hideColumn(int columnIndex)method.
- Hide a specific row using Worksheet.hideRow(int rowIndex) method.
- Save the document to file using Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class HideRowsColumns {
public static void main(String[] args) throws Exception {
//Load the sample document
Workbook wb = new Workbook();
wb.loadFromFile("Sample.xlsx ");
//Get the first worksheet
Worksheet sheet = wb.getWorksheets().get(0);
//Hide the third column
sheet.hideColumn(3);
//Hide the third row
sheet.hideRow(3);
//Save the document
wb.saveToFile("HideRowsColumns.xlsx", ExcelVersion.Version2016);
}
}

Show Hidden Rows and Columns
- Create a Workbook instance and load a sample Excel document using Workbook.loadFromFile() method.
- Get a specified worksheet using Workbook.getWorksheets().get() method.
- Show a hidden column using Worksheet.showColumn(int columnIndex)method.
- Show a hidden row using Worksheet.showRow(int rowIndex) method.
- Save the document to file using Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class ShowRowsColumns {
public static void main(String[] args) throws Exception {
//Load the sample document
Workbook wb = new Workbook();
wb.loadFromFile("HideRowsColumns.xlsx ");
//Get the first worksheet
Worksheet sheet = wb.getWorksheets().get(0);
//Unhide the third column
sheet.showColumn(3);
//Unhide the third row
sheet.showRow(3);
//Save the document
wb.saveToFile("ShowRowsColumns.xlsx", ExcelVersion.Version2016);
}
}

Hide Multiple Rows and Columns
- Create a Workbook instance and load a sample Excel document using Workbook.loadFromFile() method.
- Get a specified worksheet using Workbook.getWorksheets().get() method.
- Hide multiple columns using Worksheet.hideColumns(int columnIndex, int columnCount)method.
- Hide multiple rows using worksheet.hideRows(int rowIndex, int rowCount) method.
- Save the document to file using Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class HideMultiRowsColumns {
public static void main(String[] args) throws Exception {
//Load the sample document
Workbook wb = new Workbook();
wb.loadFromFile("Sample01.xlsx ");
//Get the first worksheet
Worksheet sheet = wb.getWorksheets().get(0);
//Hide multiple columns
sheet.hideColumns(2,2);
//Hide multiple rows
sheet.hideRows(3,3);
//Save the document
wb.saveToFile("HideMultiRowsColumns.xlsx", ExcelVersion.Version2016);
}
}

Show Multiple Rows and Columns
- Create a Workbook instance and load a sample Excel document using Workbook.loadFromFile() method.
- Get a specified worksheet using Workbook.getWorksheets().get() method.
- Loop through the rows and find the hidden rows using Worksheet.getRowIsHide() method.
- Show all hidden rows using Worksheet.showRow(i) method.
- Save the document to file using Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class ShowMultiRowsColumns {
public static void main(String[] args) throws Exception {
//Load the sample document
Workbook wb = new Workbook();
wb.loadFromFile("HideMultiRowsColumns.xlsx");
//Get the first worksheet
Worksheet sheet = wb.getWorksheets().get(0);
//Traverse all the rows
for (int i = 1; i <= sheet.getLastRow(); i++) {
//detect if the row is hidden
if (sheet.getRowIsHide(i)) {
//Show the hidden rows
sheet.showRow(i);
}
}
//Traverse the columns and show all the hidden columns
for (int j = 1; j <= sheet.getLastColumn(); j++) {
if (sheet.getColumnIsHide(j)) {
sheet.showColumn(j);
}
//Save the document
wb.saveToFile("ShowMultiRowsColumns.xlsx", ExcelVersion.Version2016);
}
}
}

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.
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>15.12.15</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");
}
}

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);
}
}

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.
Subtotal is a built-in function in Microsoft Excel that enables you to quickly calculate a range of data using a summary function, such as SUM, AVERAGE, COUNT, or MIN. This article will demonstrate how to add subtotals to a data range in Excel in Java using Spire.XLS for Java.
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>15.12.15</version>
</dependency>
</dependencies>
Add Subtotals to a Data Range
The XlsWorksheet.subtotal() method is used to add subtotals to a data range. It accepts the following parameters:
- IXLSRange: the specific data range.
- int: the column index (zero-based) that you want to base the subtotals on.
- int[]: an array of column indexes (zero-based) on which the subtotals are calculated.
- SubtotalTypes: the function (SUM, AVERAGE etc.) used to calculate the subtotals.
- boolean: Indicates whether to replace existing subtotals.
- boolean: Indicates whether to insert page breaks between groups.
- boolean: Indicates whether to add summary rows below data.
The following are the steps to add subtotals to a data range:
- Create an instance of Workbook class.
- Load an Excel file using Workbook.loadFromFile() method.
- Get the desired worksheet using Workbook.getWorksheets().get() method.
- Access the range that you wish to subtotal using Worksheet.getCellRange() method.
- Add subtotals to the range using XlsWorksheet.subtotal() method.
- Save the result file using Workbook.saveToFile() method.
- Java
import com.spire.xls.*;
public class AddSubtotalsToDataRange {
public static void main(String []args){
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load an Excel file
workbook.loadFromFile("Report.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Access the range that contains data you wish to subtotal
CellRange range = sheet.getCellRange("A2:C11");
//Add subtotals to the range, the function is Sum and it will be applied to the 3rd column in the range
sheet.subtotal(range, 0, new int[] { 2 }, SubtotalTypes.Sum, true, false, true);
//Save the result file
workbook.saveToFile("AddSubtotal.xlsx", ExcelVersion.Version2016);
workbook.dispose();
}
}

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.
Spire.XLS for Java offers the Workbook.addDigitalSignature() method and the Workbook.removeAllDigitalSignatures() method to add and remove the Excel digital signatures. This article will show you how to detect if an Excel document has been signed or not, and retrieve information about digital signatures in Java applications.
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>15.12.15</version>
</dependency>
</dependencies>
Detect if an Excel is Digitally Signed or Not
Spire.XLS for Java offers the Workbook.isDigitallySigned() method to check whether an Excel document is digitally signed or not.
- Create a Workbook instance and load a sample Excel document using Workbook.loadFromFile() method.
- Determine if the workbook is digitally signed by Workbook.isDigitallySigned() method.
- Print out the results.
- Java
import com.spire.xls.*;
public class Test {
public static void main(String[] args) throws Exception {
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load an Excel document
workbook.loadFromFile("Sample.xlsx");
//Detect if an Excel Document is digitally signed or not
Boolean signature = workbook.isDigitallySigned();
//Print the results
if (signature) {
System.out.println("Document has been signed");
}
else
{
System.out.println("Document has not been signed");
}
}
}

Get the Digital Signature Details in Excel
The following are the steps to get the details of the digital signatures in an Excel document.
- Create a Workbook instance and load a sample Excel document using Workbook.loadFromFile() method.
- Get the collection of digital signatures using Workbook.getDigitalSignatures() method.
- Get the details of a specific digital signature using the methods under IDigitalSignature object.
- Java
import com.spire.xls.*;
import com.spire.xls.core.interfaces.IDigitalSignature;
import com.spire.xls.core.interfaces.IDigitalSignatures;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.Date;
public class Test {
public static void main(String[] args) throws Exception {
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load an Excel document
workbook.loadFromFile("Sample01.xlsx");
//Get the collection of digital signatures
IDigitalSignatures signatures = workbook.getDigitalSignatures();
//Get the details of digital signatures
for (IDigitalSignature digitalSignature : (Iterable) signatures) {
X509Certificate info = digitalSignature.getX509Certificate();
PrivateKey privateKey = digitalSignature.getPrivateKey();
String comment = digitalSignature.getComments();
Date date = digitalSignature.getSignTime();
//Print out the results of the Excel digital signature
System.out.println("Signatature Certificate:" + info + "\n" +
"Signature Comment:" + comment + "\n" +
"Sign Date:" + date + "\n" +
"PrivateKey:" + privateKey + "\n");
}
}
}

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.
HTML files are Hypertext Markup Language files designed for displaying information in web browsers. In some cases, you might need to convert your Excel document to HTML in order to view it on the web. This article will demonstrate how to achieve this task programmatically in Java using Spire.XLS for Java.
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>15.12.15</version>
</dependency>
</dependencies>
Convert Excel to HTML
The following are the steps to convert an Excel file to HTML:
- Create a Workbook instance.
- Load an Excel file using Workbook.loadFromFile() method.
- Save the file to HTML using Workbook.saveToFile() method.
- Java
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
public class ConvertExcelToHTML {
public static void main(String []args){
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load an Excel file
workbook.loadFromFile("Sample1.xlsx");
//Save the file to HTML
workbook.saveToFile("ToHtml.html", FileFormat.HTML);
}
}

Convert Excel to HTML with Image Embedded
The following are the steps to convert an Excel worksheet to HTML with image embedded:
- Create a Workbook instance.
- Load an Excel file using Workbook.loadFromFile() method.
- Get the first worksheet using Workbook.getWorksheets().get() method.
- Create a HTMLOptions instance and enable image embedding using HTMLOptions.setImageEmbedded() method.
- Save the worksheet to HTML with image embedded using Worksheet.saveToHtml(String, HTMLOptions) method.
- Java
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import com.spire.xls.core.spreadsheet.HTMLOptions;
public class ConvertExcelToHtmlWithImageEmbeded {
public static void main(String []args){
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load an excel file
workbook.loadFromFile("Sample2.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Set embedded image as true
HTMLOptions options = new HTMLOptions();
options.setImageEmbedded(true);
//Save the worksheet to HTML
sheet.saveToHtml("ToHtmlWithImageEmbeded.html", options);
}
}

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.
When working with Excel, you may sometimes need to find the common values between two ranges of cells in a worksheet. For this reason, it’s recommended that Java codes can be used to automatically find the intersection of certain ranges. In this article, you'll learn how to achieve the operation using Spire.XLS for Java.
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>15.12.15</version>
</dependency>
</dependencies>
Get the Intersection of Two Cell Ranges in Excel
The following are detailed steps to get the intersection of two cell ranges in an Excel worksheet.
- Create a Workbook instance and load a sample Excel file using Workbook.loadFromFile() method.
- Get a specific worksheet of the file using Workbook.getWorksheets().get() method.
- Specify two ranges of cells using Worksheet.getRange().get() method and get their intersection using XlsRange.intersect() method.
- Create a StringBuilder instance.
- Loop through the intersection and obtain cell values using CellRange.getValue() method.
- Append the result to the StringBuilder instance using StringBuilder.append() method.
- Java
import com.spire.xls.*;
public class getIntersectionOfTwoRanges {
public static void main(String[] args) {
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load a sample Excel file
workbook.loadFromFile( "C:\\Users\\Test1\\Desktop\\sample.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Specify two cell ranges and get their intersection
CellRange range = sheet.getRange().get("B2:E7").intersect(sheet.getRange().get("C3:D7"));
//Create a StringBuilder instance
StringBuilder content = new StringBuilder();
content.append("The intersection of the two ranges \"B2:E7\" and \"C3:D7\" is:"+"\n");
//Loop through the intersection and obtain cell values
for(CellRange r : range.getCellList())
{
content.append(r.getValue()+"\n");
}
//Output the result
System.out.println(content);
}
}
The input Excel:

The output result:

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.
As its name suggests, a Comma Separated Values (CSV) file is a plain text file containing only numbers and letters, usually separated by commas. It can be used for exchanging data between applications. This article demonstrates how to convert Excel to CSV and convert CSV to Excel using Spire.XLS for Java.
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>15.12.15</version>
</dependency>
</dependencies>
Convert Excel to CSV
Spire.XLS for Java supports converting Excel to CSV with only several lines of codes. To get started, follow these steps.
- Create a Workbook instance.
- Load a sample Excel document using Workbook.loadFromFile() method.
- Get a specific worksheet of the document using Workbook.getWorksheets().get() method.
- Save the worksheet to CSV using Worksheet.saveToFile() method.
- Java
import com.spire.xls.*;
import java.nio.charset.Charset;
public class ExcelToCSV {
public static void main(String[] args) {
//Create a workbook
Workbook workbook = new Workbook();
//Load a sample excel file
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.xlsx");
//Calculate formulas if any
workbook.calculateAllValue();
//Get the first sheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Save the document to CSV
sheet.saveToFile("output/ToCSV_out.csv", ",", Charset.forName("UTF-8"));
}
}

Convert CSV to Excel
The following are detailed steps to convert CSV to Excel.
- Create a Workbook instance and load a sample CSV file using Workbook.loadFromFile() method.
- Get a specific worksheet using Workbook.getWorksheets().get() method.
- Specify the cell range using Worksheet.getCellRange() method and ignore errors when setting numbers in the cells as text using CellRange.setIgnoreErrorOptions (java.util.EnumSet ignoreErrorOptions) method.
- Automatically adjust the height of rows and width of columns using methods provided by CellRange class.
- Save the document to an XLSX file using Workbook.saveToFile() method.
- Java
import com.spire.xls.*;
import java.util.EnumSet;
public class CSVToExcel {
public static void main(String[] args) {
//Create a workbook
Workbook workbook = new Workbook();
//Load a sample CSV file
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\test.csv", ",", 1, 1);
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Specify the cell range and ignore errors when setting numbers in the cells as text
sheet.getCellRange("A1:D6").setIgnoreErrorOptions(EnumSet.of(IgnoreErrorType.NumberAsText));
//Automatically adjust the height of the rows and width of the columns
sheet.getAllocatedRange().autoFitColumns();
sheet.getAllocatedRange().autoFitRows();
//Save the document to an XLSX file
workbook.saveToFile("output/CSVToExcel_out.xlsx", ExcelVersion.Version2013);
}
}

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.
XPS (XML Paper Specification) is a fixed-layout document format designed for sharing and publishing purposes. The format was originally created as an attempt to take the place of PDF file format, but it failed for a number of reasons. Regardless, the XPS file format is still being used in some occasions, and sometimes you may need to convert your Excel files to XPS files. This article will introduce how to accomplish this task using Spire.XLS for Java.
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>15.12.15</version>
</dependency>
</dependencies>
Convert Excel to XPS
The Workbook.saveToFile() method offered by Spire.XLS for Java enables you to easily convert Excel files to XPS with just a few lines of code. The detailed steps are as follows.
- Create a Workbook instance.
- Load a sample Excel document using Workbook.loadFromFile() method.
- Save the document to XPS file format using Workbook.saveToFile() method.
- Java
import com.spire.xls.*;
public class toXPS {
public static void main(String[] args) {
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load an Excel document
workbook.loadFromFile("test.xlsx");
//Convert Excel to XPS
workbook.saveToFile("ToXPS.xps", FileFormat.XPS);
}
}

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.