Reputation: 109
I am using following code for export data.
public void onClick(View v) {
File sd = Environment.getExternalStorageDirectory();
String csvFile = "expensesData.xls";
File directory = new File(sd.getAbsolutePath());
//create directory if not exist
if (!directory.isDirectory()) {
directory.mkdirs();
}
try {
//file path
File file = new File(directory, csvFile);
WorkbookSettings wbSettings = new WorkbookSettings();
WritableWorkbook workbook;
workbook = Workbook.createWorkbook(file, wbSettings);
//Excel sheet name. 0 represents first sheet
WritableSheet sheet = workbook.createSheet("userList", 0);
// column and row
sheet.addCell(new Label(0, 0, "Type"));
sheet.addCell(new Label(1, 0, "Amount"));
Cursor cursor = db.rawQuery("SELECT expenses_type,amount" +
"FROM expenses_diary", null);
if (cursor.moveToFirst()) {
do {
String ex_type = cursor.getString(cursor.getColumnIndex("expenses_type"));
String ex_amount = cursor.getString(cursor.getColumnIndex("amount"));
int i = cursor.getPosition() + 1;
sheet.addCell(new Label(0, i, ex_type));
sheet.addCell(new Label(1, i, ex_amount));
} while (cursor.moveToNext());
}
cursor.close();
workbook.write();
workbook.close();
showToast("Exporting...");
showToast("Data Exported - expensesData.xls");
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
the exported file is storing in Root of Internal Storage, but I want to store that file inside the folder.(For example,currently in /expensesData.xls
,but I want ExpensesApp/expensesData.xls
.
I think this is simple,but am new in android, so I don't know.
Upvotes: 1
Views: 218
Reputation: 884
First create a object of the file class for the directory you wish to create
File folder = new File(Environment.getExternalStorageDirectory() + "/ExpensesApp/");
Check if folder exists and create it if it doesn't exist
if (!folder.exists())
folder.mkdir();
Then create your xls file
File file = new File(Environment.getExternalStorageDirectory() + "/ExpensesApp/expensesData.xls");
Upvotes: 3