How to Store Values in Excel using Java

Excel (XLSX or XLS) is a powerful tool widely used by large to small-scale organizations for storing data, financial data analysis, and organizing data. An Excel file may contain one or multiple spreadsheets, and a sheet contains a grid of cells. Sometimes, you might have to store important figures or automated calculations (by a software application) in an Excel spreadsheet and save them as activity logs. To implement this record-keeping use case, we will discuss how to store values in Excel using Java. We’ll also see how to store different types of data in Excel cells programmatically.

The following points will be covered in this article:

Java Excel Writer Library - Installation

In this section, we will discuss the installation procedure of this Java Excel writer library. If you are using Maven to manage a Java project or app, add the following configurations in the pom.xml file.

<repository>
	<id>ConholdateJavaAPI</id>
	<name>Conholdate Java API</name>
	<url>https://releases.conholdate.com/java/repo</url>
</repository>        
<dependency>
	<groupId>com.conholdate</groupId>
	<artifactId>conholdate-total</artifactId>
	<version>23.6</version>
	<type>pom</type>
</dependency>

Then run the following commands.

mvn clean    
mvn install -U

For configurations about other Java frameworks, please visit releases.conholdate.com.

How to Store Values in Excel: Step-by-Step Java Guide

This section demonstrates how to store values in Excel using Java. Since every cell’s position is recognized by a reference to a row number and column letter conjunction, the cell’s identity should be remembered before accessing the code editor.

To store data in an Excel sheet, follow the steps given below:

  • Instantiate the object of Workbook class.
Workbook workbook = new Workbook();
  • Create and add Worksheet object into the workbook and obtain the reference of cells collection in a Worksheet object.
int sheetIndex = workbook.getWorksheets().add();
com.aspose.cells.Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
com.aspose.cells.Cells cells = worksheet.getCells();
  • At this point, we are ready to store various kinds of data in cells of the worksheet. Firstly, let’s add a string value in a specific cell.
com.aspose.cells.Cell cell = cells.get("A1");
cell.setValue("Hello World");
  • Next, add an integer value like below:
cell = cells.get("A3");
cell.setValue(15);
  • We can add a boolean value to the cell as follows:
cell = cells.get("A4");
cell.setValue(true);
  • Date/time can be added as follows:
cell = cells.get("A5");
cell.setValue(java.util.Calendar.getInstance());
  • Finally, it is time to save the data in the Excel worksheet.
workbook.save("AddingDataToCells_out.xls");

The following code sample sums up the above steps to demonstrate how to store values in Excel using Java.

Get a Free License

You can get a free temporary license to try the API without evaluation limitations.

Conclusion

To conclude, we hope you have learned how to store values of various data types in the cells of an Excel worksheet in Java using an Excel writer library. We have explained step by step and provided examples of how to store values of different data types. In the end, we gave a complete code sample to demonstrate how to store values in Excel using Java.

You may visit the documentation to learn more about the Java Excel writer API.

Finally, please stay tuned at conholdate.com for regular updates.

Ask a Question

You can let us know your questions or queries on our forum.

See Also