如何在android中创建一个csv文件

时间:2013-03-14 06:53:54

标签: android

我正在尝试将所有联系人都放在.csv文件中。我已经完成了取消联系人的第一部分,我现在剩下的就是将文件写入.csv文件。请告诉我如何创建.csv文件机器人。

1 个答案:

答案 0 :(得分:11)

检查以下代码以生成CSV文件。无需使用jar文件。

你必须将一个csv文件保存到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();

    }

}

在清单

中添加此权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />