This video shows the steps to create an excel file from the ListView data. In this demo it first creates the ListView with some sample string data.
Then in the button onClick method, it reads the listView data one by one iteratively and populate the respective cells of an excel file.
It then saves the file in download folder using the StorageManager APIs.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Complete source code and other details:
package com.programmerworld.excelfilefromlistview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.FileObserver;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
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.FileNotFoundException;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
String[] stringArray = {"This", "is", "an", "example", "Excel", "Sheet", "from", "programmer", "world"};
listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,
stringArray));
}
public void buttonCreateExcelFile(View view){
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet hssfSheet = hssfWorkbook.createSheet("MySheet");
HSSFRow hssfRow = hssfSheet.createRow(0);
for (int i = 0; i<listView.getCount(); i++){
HSSFCell hssfCell = hssfRow.createCell(i);
hssfCell.setCellValue(listView.getItemAtPosition(i).toString());
}
saveWorkBook(hssfWorkbook);
}
private void saveWorkBook(HSSFWorkbook hssfWorkbook){
StorageManager storageManager = (StorageManager) getSystemService(STORAGE_SERVICE);
StorageVolume storageVolume = storageManager.getStorageVolumes().get(0); // internal storage
File fileOutput = new File(storageVolume.getDirectory().getPath() +"/Download/ProgrammerWorld.xls");
try {
FileOutputStream fileOutputStream = new FileOutputStream(fileOutput);
hssfWorkbook.write(fileOutputStream);
fileOutputStream.close();
hssfWorkbook.close();
Toast.makeText(this, "File Created Successfully", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, "File Creation Failed", Toast.LENGTH_LONG).show();
throw new RuntimeException(e);
}
}
}
plugins {
id 'com.android.application'
}
android {
namespace 'com.programmerworld.excelfilefromlistview'
compileSdk 33
defaultConfig {
applicationId "com.programmerworld.excelfilefromlistview"
minSdk 33
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation 'org.apache.poi:poi:5.0.0'
}
No update required in Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ExcelFileFromListView"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="357dp"
android:layout_height="355dp"
android:layout_marginStart="27dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="128dp"
android:layout_marginTop="56dp"
android:onClick="buttonCreateExcelFile"
android:text="Create Excel File"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/listView" />
</androidx.constraintlayout.widget.ConstraintLayout>
Screenshots: