从android设备导出CSV文件时无法显示中文

时间:2018-01-24 06:56:27

标签: android android-sqlite

我使用这个jar文件在我的android项目上创建一个CSV文件。 https://code.google.com/archive/p/opencsv/downloads

我可以使用Intent创建并使用附件发送邮件,但是当我打开csv文件时,中文不正确(就像照片一样) enter image description here

评论内容正确的字词应为今天好棒棒1 enter image description here

我尝试了两种解决方案,第一种是在写行时更改字符串格式。

以下是我的Excel课程,我更改了我的评论数据listdata.get(9),并将其保存为formatComment,无效。

public class ExportDatabaseCSVTask extends AsyncTask<String, String, String> {

    private Context mContext;
    private ProgressDialog dialog = null;

    private DatabaseHandler db;
    private List<Contact> contactList;

    public ExportDatabaseCSVTask(Context context) {
        mContext = context;
    }

    @Override
    protected void onPreExecute() {
        dialog = ProgressDialog.show(mContext, null, "Exporting database...");
    }

    protected String doInBackground(final String... args) {
        String path = mContext.getApplicationContext().getExternalCacheDir().getPath() + File.separator + "sugar";
        File exportDir = new File(path);
        //File exportDir = new File(Environment.getExternalStorageDirectory(), "");
        if (!exportDir.exists()) {
            exportDir.mkdirs();
        } else {
            exportDir.delete();
            exportDir.mkdirs();
        }

        File file = new File(exportDir, "MyDailySugar.csv");
        try {

            file.createNewFile();
            CSVWriter csvWrite = new CSVWriter(new FileWriter(file));

            //data
            db = new DatabaseHandler(mContext);
            //my data
            contactList = db.sortingDate();
            //Headers
            String arrStr1[] = {"Date", "Before Morning", "After Morning",
                    "Before Noon", "After Noon",
                    "Before Night", "After Night",
                    "Before Exercise", "After Exercise", "Comment"};

            csvWrite.writeNext(arrStr1);

            for (int i = 0; i < contactList.size(); i++) {
                ArrayList<String> listdata = new ArrayList<String>();
                listdata.add(contactList.get(i).getDate());
                listdata.add(contactList.get(i).getBeforeMorning());
                listdata.add(contactList.get(i).getAfterMorning());
                listdata.add(contactList.get(i).getBeforeNoon());
                listdata.add(contactList.get(i).getAfterNoon());
                listdata.add(contactList.get(i).getBeforeNight());
                listdata.add(contactList.get(i).getAfterNight());
                listdata.add(contactList.get(i).getBeforeExercise());
                listdata.add(contactList.get(i).getAfterExercise());
                listdata.add(contactList.get(i).getComment());
                String formatComment = listdata.get(9);
                formatComment = new String(formatComment.getBytes("UTF-8"), "ISO-8859-1");
                System.out.println("formatComment=>" + formatComment);
                String arrStr[] = {listdata.get(0), listdata.get(1), listdata.get(2),
                        listdata.get(3), listdata.get(4),
                        listdata.get(5), listdata.get(6),
                        listdata.get(7), listdata.get(8), formatComment};
                csvWrite.writeNext(arrStr);
            }
            csvWrite.close();
            return "";
        } catch (IOException e) {
            Log.e("MainActivity", e.getMessage(), e);
            return "";
        }
    }

    @SuppressLint("NewApi")
    @Override
    protected void onPostExecute(final String success) {

        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
        }
        if (success.isEmpty()) {
            Toast.makeText(mContext, "Export successful!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(mContext, "Export failed!", Toast.LENGTH_SHORT).show();
        }
    }
}

我尝试这样的另一种方式:

    OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
    out.write(0xFEFF);
    CSVWriter csvWrite = new CSVWriter(out, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER, CSVWriter.DEFAULT_LINE_END);

没有用......

我的第二个解决方案是尝试将数据库更改为uft8

这是我的创建数据库类,我尝试添加DEFAULT CHARSET=utf8,它在DEFAULT附近显示语法错误,我不知道为什么......

//Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    System.out.println("table is here");
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT,"
            + KEY_BMORNING + " TEXT," + KEY_AMORNING + " TEXT,"
            + KEY_BNOON + " TEXT," + KEY_ANOON + " TEXT,"
            + KEY_BNIGHT + " TEXT," + KEY_ANIGHT + " TEXT,"
            + KEY_BEXERCISE + " TEXT NOT NULL DEFAULT ''," + KEY_AEXERCISE + " TEXT NOT NULL DEFAULT '',"
            + KEY_COMMENT + " TEXT NOT NULL DEFAULT ''" +
            ")" + " DEFAULT CHARSET=utf8;";
    db.execSQL(CREATE_CONTACTS_TABLE);

}

我不知道有没有其他解决方案我可以尝试。 任何帮助,将不胜感激 。提前谢谢。

0 个答案:

没有答案