我正在使用以下代码导出数据。
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();
}
}
});
导出的文件存储在Root of Internal Storage中,但我想将该文件存储在该文件夹中。(例如,当前位于/expensesData.xls
,但我想要ExpensesApp/expensesData.xls
。
我认为这很简单,但我是android的新手,所以我不知道。
答案 0 :(得分:3)
首先为您要创建的目录创建文件类的对象
>=576px
检查文件夹是否存在,如果文件夹不存在则创建
File folder = new File(Environment.getExternalStorageDirectory() + "/ExpensesApp/");
然后创建你的xls文件
if (!folder.exists())
folder.mkdir();