In this tutorial we will learn how to update Excel file in Java. Although we can update Excel file in MS Excel manually, however it is not always possible or feasible to update Excel files manually say a large data is to be updated or data is to be exported from other source for example a database. Here arises the need for an automated way of performing this task which you can do using steps in this how to guide. Here are the steps to update Excel file in Java and finally saving it to an Excel format say XLSX in this tutorial.
Steps to Update Excel File in Java
- Using Maven repository, add reference to Aspose.Cells library in the project
- Add a reference to Workbook and Cell classes using Import in the program
- Load the workbook that is to be updated
- Get reference to the desired cell in first sheet and update target cell value
- Get reference to an other cell in second sheet and update formula in the cell
- Call calculateFormula function as formula is changed
- Save the updated file as XLSX
In the above steps we observed that an Excel file is loaded and then first sheet is accessed. You can refer to any sheet whose data is to be updated using sheet name or zero-based index. Using the sheet reference, access the cells collection in the sheet and get access to any cell and update value or formula into it. Finally call the related functions which are required according to the changes in the file. Here in this tutorial we will call calculateFormula function as formula is updated. Finally we save the workbook on to the disc. Here is the sample code that update existing Excel file in Java.
Code to Write Data to Excel in Java
In this sample code we reviewed the Java code to write data in Excel sheet. If you want to write data to Excel file using any other language say C#, refer to the article how to edit Excel file in C#.
We do not need any third part tool to be installed like MS Excel or there is no need to use interop for this process.