Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create a csv file in android [duplicate]

Tags:

android

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.

like image 748
Supreet Avatar asked Mar 14 '13 06:03

Supreet


1 Answers

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" />
like image 52
zdesam Avatar answered Oct 11 '22 08:10

zdesam