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.
Step-1
Download Java for Excel library
Step-2
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 if(!directory.isDirectory()){ directory.mkdirs(); } //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 cursor.close(); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } workbook.write(); try { workbook.close(); } catch (WriteException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } }