· 

Creating an Excel Document in Java Application

In this article, I’ll show you how to create a simple Excel document using Free Spire.XLS for Java, which is an independent library for processing Excel documents in Java application. The following code example will include - 

  • How to write data from a two-dimensional array into cells
  • How to vertically and horizontally align text in a cell
  • How to set the border style
  • How to set the row height and column width

Installing Spire.Xls.jar

If you create a Maven project, you can easily import the jar in your application using the following configurations. For non-Maven projects, download the jar file from this link and add it as a dependency in your application. 

<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>2.2.0</version>
    </dependency>
</
dependencies>

Using the code

import com.spire.xls.*;

public class CreateExcel {

   
public static void main(String[] args) throws Exception {

       
//Create a Workbook instance
       
Workbook wb = new Workbook();

       
//Remove default worksheets
       
wb.getWorksheets().clear();

       
//Add a worksheet, specifying sheet name
       
Worksheet sheet = wb.getWorksheets().add("Employees");

        
//Merge cells from A1 to G1
       
sheet.getCellRange("A1:G1").merge();

       
//Insert text to A1 and set its formatting
       
sheet.getCellRange("A1").setValue("Employees Table");
        sheet.getCellRange(
"A1").setVerticalAlignment(VerticalAlignType.Center);
        sheet.getCellRange(
"A1").setHorizontalAlignment(HorizontalAlignType.Center);
        sheet.getCellRange(
"A1").getStyle().getFont().isBold(true);
        sheet.getCellRange(
"A1").getStyle().getFont().setSize(12);

       
//Set the first row height
       
sheet.setRowHeight(1, 30);

       
//Create a two dimensional array
       
String[][] twoDimensionalArray = new String[][]{
                {
"Employee Id", "First Name", "Last Name", "Job Id", "Joining Date", "Salary", "Department Id"},
                {
"100", "Renske", "Ladwig", "34837", "20011-05-13", "13000", "20"},
                {
"101", "Jenette", "King", "35230", "2012-07-02", "9800", "30"},
                {
"102", "Sarath", "Sewall", "35477", "2015-01-23", "10500", "40"},
                {
"103", "Anthony", "Cabrio", "35861", "2013-08-10", "9200", "40"},
                {
"104", "Irene", "Mikkilineni", "36501", "2014-11-18", "8500", "40"}
        };

       
//Insert data from array to worksheet, specifying the start row and column
       
sheet.insertArray(twoDimensionalArray, 2, 1);

       
//Set the height of the rows within A2:G7
       
sheet.getCellRange("A2:G7").setRowHeight(15);

       
//Auto fit the column width
       
for (int i = 0; i < sheet.getColumns().length; i++) {

            sheet.autoFitColumn(i +
1);
        }

       
//Set the border style
       
sheet.getCellRange("A2:G7").getBorders().setKnownColor(ExcelColors.Black);
        sheet.getCellRange(
"A2:G7").borderAround(LineStyleType.Medium);
        sheet.getCellRange(
"A2:G7").borderInside(LineStyleType.Thin);
        sheet.getCellRange(
"A2:G2").borderAround(LineStyleType.Medium);

       
//Save the document
       
wb.saveToFile("Employees.xlsx", ExcelVersion.Version2016
);
    }
}

Output

Write a comment

Comments: 0