In this tutorial we will explain how to create excel file in android using Java for excel library. The example provided below is the extended version of our previous 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.

* Exports the cursor value to an excel sheet.
* Recommended to call this method in a separate thread,
* especially if you have more number of threads.
* @param cursor

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) {