· 

Java - How to Write Data to Excel

The Excel spreadsheet is a very popular file format created by Microsoft. In this article, you will learn how to programmatically create an Excel in Java and how to write data in various forms into the specified cell or cell range by using Free Spire.XLS for Java.

Install Free Spire.XLS for Java

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

Example 1. Write Data to Specific Cells

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

public class WriteToCells {

   
public static void main(String[] args) {

       
//Create a Workbook object
       
Workbook workbook = new Workbook();

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

       
//Add a worksheet and name it
       
Worksheet worksheet = workbook.getWorksheets().add("JavaExamples");

       
//Write data to specific cells
       
worksheet.get(1,1).setValue("Student Name");
        worksheet.get(
1,2).setValue("Math");
        worksheet.get(
1,3).setValue("English");
        worksheet.get(
1,4).setValue("Total Marks");
        worksheet.get(
2,1).setValue("Hazel");
        worksheet.get(
2,2).setNumberValue(80);
        worksheet.get(
2,3).setNumberValue(78);
        worksheet.get(
2,4).setNumberValue(158);
        worksheet.get(
3,1).setValue("Tina");
        worksheet.get(
3,2).setNumberValue(98);
        worksheet.get(
3,3).setNumberValue(72);
        worksheet.get(
3,4).setNumberValue(170);

       
//Auto fit column widths
       
worksheet.autoFitColumn(1);
        worksheet.autoFitColumn(
2);
        worksheet.autoFitColumn(
3);
        worksheet.autoFitColumn(
4);

       
//Save to an Excel file
       
workbook.saveToFile("output/WriteToCells.xlsx", ExcelVersion.Version2016
);
    }
}

Example 2. Write Arrays to a Worksheet

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

public class WriteArrayToWorksheet {

   
public static void main(String[] args) {

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

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

       
//Add a worksheet and name it
       
Worksheet worksheet = workbook.getWorksheets().add("JavaExamples");

       
//Create a one-dimensional array
       
String[] oneDimensionalArray = new String[]{"January", "February", "March", "April","May", "June"};

       
//Write the array to the first row of the worksheet
       
worksheet.insertArray(oneDimensionalArray, 1, 1, false);

       
//Create a two-dimensional array
       
String[][] twoDimensionalArray = new String[][]{
                {
"Name", "Age", "Sex", "Dept.", "Tel."},
                {
"John", "25", "Male", "Development","654214"},
                {
"Albert", "24", "Male", "Support","624847"},
                {
"Amy", "26", "Female", "Sales","624758"}
        };

       
//Write the array to the worksheet starting from the cell A3
       
worksheet.insertArray(twoDimensionalArray, 3, 1);


       
//Auto fit column width in the located range
       
CellRange locatedRange = worksheet.getAllocatedRange();
        locatedRange.autoFitColumns();

       
//Save to an Excel file
       
workbook.saveToFile("output/InsertArrays.xlsx", ExcelVersion.Version2016
);
    }
}

Example 3. Write DataTable to a Worksheet

import com.spire.data.table.DataRow;
import com.spire.data.table.DataTable;
import com.spire.xls.CellRange;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class WriteDataTableToWorksheet {

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

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

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

       
//Add a worksheet and name it
       
Worksheet worksheet = workbook.getWorksheets().add("JavaExamples");

       
//Create a DataTable object
       
DataTable dataTable = new DataTable();
        dataTable.getColumns().add(
"No", Integer.class);
        dataTable.getColumns().add(
"Name", String.class);
        dataTable.getColumns().add(
"City", String.class);

       
//Create rows and add data
       
DataRow dr = dataTable.newRow();
        dr.setInt(
0,1);
        dr.setString(
1,"Tom");
        dr.setString(
2,"New York");
        dataTable.getRows().add(dr);
        dr = dataTable.newRow();
        dr.setInt(
0,2);
        dr.setString(
1,"Jerry");
        dr.setString(
2,"China");
        dataTable.getRows().add(dr);
        dr = dataTable.newRow();
        dr.setInt(
0,3);
        dr.setString(
1,"Dive Time");
        dr.setString(
2,"Berkely");
        dataTable.getRows().add(dr);
        dr = dataTable.newRow();
        dr.setInt(
0,4);
        dr.setString(
1,"Amor Aqua");
        dr.setString(
2,"Florida");
        dataTable.getRows().add(dr);

       
//Write datatable to the worksheet
       
worksheet.insertDataTable(dataTable,true,1,1,true);

       
//Auto fit column width in the located range
       
CellRange locatedRange = worksheet.getAllocatedRange();
        locatedRange.autoFitColumns();

       
//Save to an Excel file
       
workbook.saveToFile("output/InsertDataTable.xlsx", ExcelVersion.Version2016
);
    }
}

Write a comment

Comments: 0