6 minutes

How To Create Excel File In Android

By Nilanchala @nilan, On Sep 17, 2023 Android 2.91K Views

Excel is a widely used software for creating and managing data. In this tutorial, we will show you how to create an Excel file in Android using Java for Excel library. The Java for Excel library provides a simple and easy-to-use API for working with Excel files in Java.

android SQLite database tutorial. We recommend you to have a glance at it before going through this example.

In our previous example, we have explained you how to create an TODO application in android using SQLite database. Now in this post we will export the data saved in database to an excel file and will store it on SDCard.


Download Java for Excel library from below path.


Export and save data from cursor into excel spreadsheet and saves the spreadsheet into external storage directory.

private void exportToExcel(Cursor cursor) {		
	final String fileName = "TodoList.xls";
	//Saving file in external storage
	File sdCard = Environment.getExternalStorageDirectory();	
	File directory = new File(sdCard.getAbsolutePath() + "/javatechig.todo");
	//create directory if not exist
	//file path
	File file = new File(directory, fileName);
	WorkbookSettings wbSettings = new WorkbookSettings();
	wbSettings.setLocale(new Locale("en", "EN"));		
	WritableWorkbook workbook;
	try {
		workbook = Workbook.createWorkbook(file, wbSettings);			
		//Excel sheet name. 0 represents first sheet
		WritableSheet sheet = workbook.createSheet("MyShoppingList", 0);

		try {
			sheet.addCell(new Label(0, 0, "Subject")); // column and row
			sheet.addCell(new Label(1, 0, "Description"));				
			if (cursor.moveToFirst()) {
				do {
					String title = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TODO_SUBJECT));
					String desc = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TODO_DESC));

					int i = cursor.getPosition() + 1;						
					sheet.addCell(new Label(0, i, title));
					sheet.addCell(new Label(1, i, desc));						
				} while (cursor.moveToNext());
			//closing cursor
		} catch (RowsExceededException e) {
		} catch (WriteException e) {
		try {
		} catch (WriteException e) {
	} catch (IOException e) {

This code exports the contents of a Cursor object to an Excel file in the external storage of an Android device.

The method exportToExcel takes a Cursor object as a parameter, which contains the data to be exported. It first creates a file with the name "TodoList.xls" in a directory named "javatechig.todo" in the external storage of the device.

After creating the file, it creates a new Workbook object using the jxl library to create a new Excel workbook. It then creates a new sheet called "MyShoppingList" in the workbook.

The code then loops through each row in the Cursor object, retrieves the data from the columns, and adds the data to the cells in the Excel sheet using the addCell method. The first row of the sheet contains the column headers "Subject" and "Description".

Finally, the workbook is written to the file, and the workbook and cursor objects are closed to release resources.

nilan avtar


I'm a blogger, educator and a full stack developer. Mainly focused on Java, Spring and Micro-service architecture. I love to learn, code, make and break things.