public void exportToExcel() { final String fileName = "TodoList2.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); Cursor cursor = mydb.rawQuery("select * from Contact", null); try { sheet.addCell(new Label(0, 0, "id")); // column and row sheet.addCell(new Label(1, 0, "name")); sheet.addCell(new Label(2,0,"ff ")); sheet.addCell(new Label(3,0,"uu")); if (cursor.moveToFirst()) { do { String title =cursor.getString(0) ; String desc = cursor.getString(1); String name=cursor.getString(2); String family=cursor.getString(3); int i = cursor.getPosition() + 1; sheet.addCell(new Label(0, i, title)); sheet.addCell(new Label(1, i, desc)); sheet.addCell(new Label(2,i,name)); sheet.addCell(new Label(3,i,family)); } 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(); } }
Kavos khajavi
source share