Apache POI Update Excel File Content

Tags: Apache POI

Java Code Examples for:

  • org.apache.poi.ss.usermodel.WorkbookFactory.create(InputStream inputStream) method
  • org.apache.poi.ss.usermodel.Workbook.write(OutputStream stream) method

In this article we show you how to update existing Excel file content in Java using Apache POI library.

Apache POI Library Dependencies

Define below dependencies in build.gradle if you are using Gradle build.

compile group: 'org.apache.poi', name: 'poi', version: '4.0.1'
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.0.1'

Define this dependencies in pom.xml if you are using Maven build.

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.1</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.1</version>
</dependency>

Example Excel File to be Updated

Below is the example Excel file named sample.xlsx with simple text content at cell B2.

Apache POI Update Excel File Content

In next Java code we update the content at cell B2 from “Example Text” to “SimpleSolution.dev”.

Example source code application to update Excel file at cell B2

package simplesolution.dev;

import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ApachePOIUpdateExcelFileExample {

    public static void main(String... args) {
        try(InputStream inputStream = new FileInputStream("sample.xlsx")) {
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);
            Row row = sheet.getRow(1);
            Cell cell = row.getCell(1);
            if(cell == null) {
                cell = row.createCell(1);
            }

            cell.setCellValue("SimpleSolution.dev");

            try(OutputStream outputStream = new FileOutputStream("sample.xlsx")) {
                workbook.write(outputStream);
            }
        }catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Output Excel file:

Apache POI Update Excel File Content Output

Download Source Code

The source code in this article can be found at: github.com/simplesolutiondev/ApachePOIUpdateExcelFile

or download at:

Download Source Code

Happy Coding 😊

Writing Excel File Using Apache POI Library in Java

Apache POI to Create Excel Text Cell

Apache POI Create new Excel sheet

Java Create Excel File .xlsx using Apache POI

Java Read Excel File using Apache POI

Java Read Excel Workbook from File using Apache POI

Java Read Excel Workbook from InputStream using Apache POI

Java Read Password Protected Excel File using Apache POI

Java How to Iterate over Sheets Rows and Cells of Excel file using Apache POI

Java Add Rows to Existing Excel File using Apache POI

Java Add Sheet to Existing Excel File using Apache POI

Java Remove Sheet from Existing Excel File using Apache POI

Java Create Formula Excel Cells using Apache POI