我想将我的联系人导出到android studio中的Excel文件。现在我访问我的联系人并将它们保存到Sqlite中的表中。我还有一个导出按钮,用于将我的数据导出到excel文件。但是如何查询我的表并将结果写入Excel文件?
btnExport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CSVWriter writer = null;
try
{
//read from db
mydb=openOrCreateDatabase(DbName,Context.MODE_PRIVATE,null);
Cursor crs= mydb.rawQuery("Select * from tblContact",null);
String[] array = new String[crs.getCount()];
int i = 0;
writer = new CSVWriter(new FileWriter("/sdcard/myfile.csv"), ',');
while(crs.moveToNext()) {
String uname = crs.getString(crs.getColumnIndex("FirstName"));
array[i] = uname;
i++;
writer.writeNext(new String[]{array[i]});
}
writer.close();
Toast.makeText(getApplicationContext(),"Exported",Toast.LENGTH_LONG).show();
mydb.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
});