How to Insert/Append Data to Excel using Android?
Last Updated :
24 Apr, 2025
File handling in Java is used to read and write data to a file. The particular file class from the package called java.io allows us to handle and work with different formats of files. File handling allows users to store data permanently in a file. And the best or suggested document for storing data is Microsoft Excel because it provides the organized storage of data i.e. in rows and columns, easy calculations on data, and easy data analysis.
Note: In order, to view an excel sheet, any excel sheet viewer must be installed in the emulator or real device e.g. Microsoft Excel
Requirements
Excel File handling in java requires the implementation of the following programming concepts:
- FileInputStream Class
- FileOutputStream Class
- Apache POI Library
FileInputStream Class
Class of java.io package which is used to read byte data from a file. In order to create a file input stream, import the given package in the java file
import java.io.FileInputStream;
Syntax:
FileInputStream input = new FileInputStream(filePath);
FileOutputStream Class
Class of java.io package which is used to write byte data to file. In order to create a file output stream, import the given package in the java file
import java.io.FileOutputStream;
Syntax:
FileOutputStream output = new FileOutputStream(filePath);
Apache POI Library
Apache POI is a Java Library that is used to handle Microsoft Office Documents. Apache POI is open source, and can be used by JVM-based programming languages. Apache POI provides the basis and advanced functionality of Apache POI Technology.
Dependency (build.gradle:app):
implementation 'org.apache.poi:poi:3.17'
Implementation Steps
Step 1: Add implementation 'org.apache.poi:poi:3.17' dependency in build.gradle:app in Android Studio and Sync Project to commit changes.
Step 2: Add code for asking permission to read and write in the external storage of the device from the user in the AndroidManifest.xml file in Android Studio.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Step 3: Write the given code in your xml layout file (e.g. activity_main)
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:hint="Enter Name"
android:id="@+id/eName"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:hint="Enter File Name"
android:id="@+id/eFileName"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:text="Add to Excel"
android:backgroundTint="#34AF00"
android:id="@+id/btnCreate" />
</LinearLayout>
Step 4: Write the given code in your java activity file (e.g. MainActivity.java)
Java
// program to create/append data to excel file using apache poi
import static java.nio.file.StandardOpenOption.APPEND;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.StandardOpenOption;
public class MainActivity extends AppCompatActivity {
EditText eName, eFileName;
Button btnCreate;
File filePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eName = findViewById(R.id.eName);
eFileName = findViewById(R.id.eFileName);
btnCreate = findViewById(R.id.btnCreate);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}
, PackageManager.PERMISSION_GRANTED);
btnCreate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
filePath = new File(Environment.getExternalStorageDirectory() + "/" + eFileName.getText().toString() + ".xls");
try {
if (!filePath.exists()) {
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet hssfSheet = hssfWorkbook.createSheet("MySheet");
HSSFRow hssfRow = hssfSheet.createRow(0);
HSSFCell hssfCell = hssfRow.createCell(0);
hssfCell.setCellValue(eName.getText().toString());
filePath.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
hssfWorkbook.write(fileOutputStream);
eName.setText("");
Toast.makeText(MainActivity.this, "File Created", Toast.LENGTH_SHORT).show();
if (fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
}
else{
FileInputStream fileInputStream = new FileInputStream(filePath);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fileInputStream);
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
int lastRowNum = hssfSheet.getLastRowNum();
HSSFRow hssfRow = hssfSheet.createRow(++lastRowNum);
hssfRow.createCell(0).setCellValue(eName.getText().toString());
fileInputStream.close();
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
hssfWorkbook.write(fileOutputStream);
eName.setText("");
Toast.makeText(MainActivity.this, "File Updated", Toast.LENGTH_SHORT).show();
fileOutputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Step 5: Run the app
Screenshot(s) app:
Inserting new record to GeeksForGeeks.xls
appending new record to GeeksForGeeks.xls
Screenshot(s) Excel:
Created excel file : GeeksForGeeks.xls
Similar Reads
How to Post Data to API using Retrofit in Android?
We have seen reading data from API in our Android app in Android Studio. For reading data from API, we use GET request to read our data which is in JSON format. In this article, we will take a look at adding data to REST API in our Android App in Android Studio. What we are going to build in this ar
6 min read
How to Post Data to API using Volley in Android?
We have seen reading the data from API using Volley request with the help of GET request in Android. With the help of GET Request, we can display data from API in JSON format and use that data inside our application. In this article, we will take a look at posting our data to API using the POST requ
5 min read
How to Update Data in API using Retrofit in Android?
We have seen reading data from API as well as posting data to our database with the help of the API. In this article, we will take a look at updating our data in our API. We will be using the Retrofit library for updating our data in our API. What we are going to build in this article? We will be
6 min read
How to Update Data in API using Volley in Android?
Prerequisite: JSON Parsing in Android using Volley LibraryHow to Post Data to API using Volley in Android? We have seen reading data from API as well as posting data to our database with the help of the API. In this article, we will take a look at updating our data in our API. We will be using the V
5 min read
How to Update Data to SQLite Database in Android?
We have seen How to Create and Add Data to SQLite Database in Android as well as How to Read Data from SQLite Database in Android. We have performed different SQL queries for reading and writing our data to SQLite database. In this article, we will take a look at updating data to SQLite database in
10 min read
How to Post Data to API using Retrofit in Android using Jetpack Compose?
APIs are used within Android Applications to interact with databases to perform various CRUD operations on data within the database such as adding new data, reading the existing data, and updating and deleting existing data. In this article, we will take a look at How to Post Data to API using Retro
5 min read
How to build a simple Calculator app using Android Studio?
Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication, or division depending upon the user input. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project
6 min read
How to pre populate database in Android using SQLite Database
Introduction : Often, there is a need to initiate an Android app with an already existing database. This is called prepopulating a database. In this article, we will see how to pre-populate database in Android using SQLite Database. The database used in this example can be downloaded as Demo Databas
7 min read
How to Get Saved Contacts in Android Application using Cursor?
Phone Contacts are very important source of data for everyone and hence accessing phone contacts is the main feature for many applications like Truecaller to help their users and provide them a better experience. We all have a default application in our mobile to manage contacts but what happens if
5 min read
How to Add Data to Back4App Database in Android?
Prerequisite: How to Connect Android App with Back4App? Back4App is an online database providing platform that provides us services with which we can manage the data of our app inside the database. This is a series of 4 articles in which we are going to perform the basic CRUD (Create, Read, Update,
4 min read