I use this jar file to create a CSV file on my android project. https://code.google.com/archive/p/opencsv/downloads
I can create it and mail it with an attachment by using Intent
, but when i open the csv file chinese is not right(just like the photo)
The Comment content correct word should be 今天好棒棒1
I try two solutions, the first is change the string format when writing row.
Here is my Excel class, i change my comment data listdata.get(9)
and save it with formatComment
, its no working.
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();
}
}
}
I try another way like this:
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);
Its no working...
and my second solution is try to change the database to uft8
Here is my create database class, i try to add DEFAULT CHARSET=utf8
, it show syntax error near DEFAULT , i don't know why...
//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);
}
I have no idea that is there another solution i can try. Any help would be appreciated . Thanks in advance.