I am trying to get all my contacts in .csv file.I have done the first part of fetching contacts now what i have left with is writing the file to .csv file.please tell me how to create .csv file in android.
Asked
Active
Viewed 3.8k times
12
-
you can use library openCSV – Pragnani Mar 14 '13 at 06:55
-
@Pragnani: is there any working example for this – Supreet Mar 14 '13 at 06:57
-
3Check this http://www.mkyong.com/java/how-to-export-data-to-csv-file-java/ , I have found lot of tutorial when I google just now. – Pragnani Mar 14 '13 at 07:01
-
I searched for `[android] write csv` and found 158 results. http://stackoverflow.com/search?q=%5Bandroid%5D+write+csv. I'm sure Google will give you a few more. – Simon Mar 14 '13 at 07:48
1 Answers
23
check below code to generate CSV file. no need to use jar file.
you have to save one csv file in to SD-CARD.
public void exportEmailInCSV() throws IOException {
{
File folder = new File(Environment.getExternalStorageDirectory()
+ "/Folder");
boolean var = false;
if (!folder.exists())
var = folder.mkdir();
System.out.println("" + var);
final String filename = folder.toString() + "/" + "Test.csv";
// show waiting screen
CharSequence contentTitle = getString(R.string.app_name);
final ProgressDialog progDailog = ProgressDialog.show(
MailConfiguration.this, contentTitle, "even geduld aub...",
true);//please wait
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
}
};
new Thread() {
public void run() {
try {
FileWriter fw = new FileWriter(filename);
Cursor cursor = db.selectAll();
fw.append("No");
fw.append(',');
fw.append("code");
fw.append(',');
fw.append("nr");
fw.append(',');
fw.append("Orde");
fw.append(',');
fw.append("Da");
fw.append(',');
fw.append("Date");
fw.append(',');
fw.append("Leverancier");
fw.append(',');
fw.append("Baaln");
fw.append(',');
fw.append("asd");
fw.append(',');
fw.append("Kwaliteit");
fw.append(',');
fw.append("asd");
fw.append(',');
fw.append('\n');
if (cursor.moveToFirst()) {
do {
fw.append(cursor.getString(0));
fw.append(',');
fw.append(cursor.getString(1));
fw.append(',');
fw.append(cursor.getString(2));
fw.append(',');
fw.append(cursor.getString(3));
fw.append(',');
fw.append(cursor.getString(4));
fw.append(',');
fw.append(cursor.getString(5));
fw.append(',');
fw.append(cursor.getString(6));
fw.append(',');
fw.append(cursor.getString(7));
fw.append(',');
fw.append(cursor.getString(8));
fw.append(',');
fw.append(cursor.getString(9));
fw.append(',');
fw.append(cursor.getString(10));
fw.append(',');
fw.append('\n');
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
// fw.flush();
fw.close();
} catch (Exception e) {
}
handler.sendEmptyMessage(0);
progDailog.dismiss();
}
}.start();
}
}
add this permission in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

zdesam
- 2,936
- 3
- 25
- 32
-
1
-
-
What if, the string contains a comma, will it save into different cell? – Sandeep Apr 11 '18 at 05:56
-
5
-
This did not set email given subject & body. I'm using gmail app to send email. – Dinith Rukshan Kumara Jan 06 '21 at 23:42