Monday, 16 November 2020

Create an Excel file in Java using Free API

An Excel file is typically used to organize, format and calculate data with formulas and functions. In Java, there is no direct API to write, read or manipulate Microsoft Excel documents, so we have to rely on the third-party library called Free Spire.XLS for Java. In this article, I’ll show you how to create an Excel file and how to write data in it using the Java library.

Spire.XLS for Java Library

Spire.XLS for Java is a professional Java Excel API that enables developers to create, manipulate, convert and print Excel worksheets without using Microsoft Excel.

Add Spire.Xls.jar as dependency

If you are working on a Maven project, you can include the dependency in pom.xml file using this:

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

If you are not using Maven, then you can download the package from Spire.XLS for Java download

page. Include Spire.Xls.jar to run the sample code given in this tutorial.

Create an Excel file in Java

In the following program, Spire.XLS for Java library provides the class named Workbook to create a blank file.

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;

public class CreateAnExcelFile {
public static void main(String[] args) {
//Create an instance of Workbook class
Workbook workbook = new Workbook();

//Save the file to the specified location
workbook.saveToFile("output/ABlankFile.xlsx", ExcelVersion.Version2010);

}
}

Insert Data in the Excel file

Usually, it is not enough that create a blank excel file only. We need to insert some data in the worksheet.

Let’s see how to insert data from DataTable in an excel file through the library.

import com.spire.data.table.*;
import com.spire.xls.*;

public class InsertData {
public static void main(String[] args) throws Exception {
//Load an Excel file
Workbook workbook = new Workbook();
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\ABlankFile.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Set name for the first worksheet
sheet.setName("Country List");
//Create a DataTable object
DataTable dataTable = new DataTable();
dataTable.getColumns().add("No.", Integer.class);
dataTable.getColumns().add("Name", String.class);
dataTable.getColumns().add("Capital", String.class);
//Create rows and add data
DataRow dr = dataTable.newRow();
dr.setInt(0,1);
dr.setString(1,"Indonesia");
dr.setString(2,"Jakarta");
dataTable.getRows().add(dr);
dr = dataTable.newRow();
dr.setInt(0,2);
dr.setString(1,"Bangladesh");
dr.setString(2,"Dhaka");
dataTable.getRows().add(dr);
dr = dataTable.newRow();
dr.setInt(0,3);
dr.setString(1,"Mexican");
dr.setString(2,"Mexico City");
dataTable.getRows().add(dr);
dr = dataTable.newRow();
dr.setInt(0,4);
dr.setString(1,"China");
dr.setString(2,"Beijing");
dataTable.getRows().add(dr);
//Import the two columns of the data table to worksheet
DataColumn[] columns={dataTable.getColumns().get(1),dataTable.getColumns().get(2)};
sheet.insertDataColumns(columns, true, 1, 1);

//Save the Excel file
String result = "output/importDataFromDataColumn.xlsx";
workbook.saveToFile(result, ExcelVersion.Version2013);

}
}

Output


Conclusion

In addition to creating an Excel file and inserting data in it, Spire.XLS for Java supports numerous functions

to manipulate Excel files, such as create, read, edit, convert and print Excel worksheets, find andreplace data,

create charts, create auto filters, read and write hyperlinks, merge/unmerge cells and files, group/ungroup rows

and columns, freeze/unfreeze Panes, encrypt/decrypt Excel workbooks etc.


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