@Override protected Boolean doInBackground(Void... params){ File exportDir = new File(Environment.getExternalStorageDirectory(), ""); if (!exportDir.exists()) { exportDir.mkdirs(); }
Date date = new Date(System.currentTimeMillis()); String filename = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date); File file = new File(exportDir, filename + ".csv");
try { file.createNewFile(); CSVWriter csvWrite = new CSVWriter(new FileWriter(file)); StringBuffer check = new StringBuffer(); check.append("SELECT * FROM NOTE"); DataBaseHelper db = DataBaseHelper.getInstance(GlobalField.getApplicationContext()); db.openDataBase(); Cursor curCSV = db.queryData(check.toString()); //key -> get the cursor and then using opencsv csvWrite.writeNext(curCSV.getColumnNames()); while (curCSV.moveToNext()) { //Which column you want to export you can add over here... List<String> list = new ArrayList<>(); for (int i = 0, length = curCSV.getColumnCount(); i < length; i++) { list.add(curCSV.getString(i)); } String[] arrStr = list.toArray(new String[list.size()]); csvWrite.writeNext(arrStr); }
@Override protectedvoidonPostExecute(Boolean result){ if (dialog.isShowing()) { dialog.dismiss(); }
if (result) { Toast.makeText(context, "SqLite Data has been Exported!", Toast.LENGTH_LONG).show(); } else { Toast.makeText(context, "SqLite Data has not Exported", Toast.LENGTH_LONG).show(); } } }
if (data.length()!=0) { Toast.makeText(context, "File is built Successfully!"+"\n"+data, Toast.LENGTH_LONG).show(); }else{ Toast.makeText(context, "File fail to build", Toast.LENGTH_SHORT).show(); } }