我试图从数据库返回数据并将其保存在SD卡中的CSV文件中并附加文件并通过电子邮件发送,所有这些都很完美,数据在Excel上应该是这样的。
示例
COLUMN A ........... COLUMN B
123123232 ..........约翰
964803400 ..........史密斯
657484738 ..........麦克
我的问题是,当我打开Excel时,数据显示如下
COLUMN A ........... COLUMN B
123123232 ..........
964803400 ..........
657484738 ..........
约翰
史密斯
麦克
以下是代码 Names.Java
dbUser.open();
File root=null;
try {
// check for SDcard
root = Environment.getExternalStorageDirectory();
Log.i(TAG,"path.." +root.getAbsolutePath());
//check sdcard permission
if (root.canWrite()){
File fileDir = new File(root.getAbsolutePath()+"/fun/");
fileDir.mkdirs();
Log.d("PATH",fileDir.toString());
File file= new File(fileDir, "itisfun.csv");
FileWriter filewriter = new FileWriter(file);
BufferedWriter out = new BufferedWriter(filewriter);
String[] div = dbUser.getVname_Mnumber(sharedUsername1,product).toString().split(",");
Log.d(sharedUsername1, product);
Log.d("VVVVVVVVVV", dbUser.getVname_Mnumber(sharedUsername1,product).toString());
for (int i =0; i<div.length;i++)
out.write( div[i] +"\n" );
out.close();
}
} catch (IOException e) {
Log.e("ERROR:---", "Could not write file to SDCard" + e.getMessage());
}
这是我的 DBadapter
public String getVname_Mnumber(String date , String Vname) throws SQLException {
String[] whereArgs = new String[]{date,Vname};
Cursor mcursor = db.rawQuery("SELECT MeterNumber,VendorName FROM " + SCAN_TABLE + " WHERE Date = ? AND VendorName = ? ",whereArgs);
ArrayList<String> results = new ArrayList<String>();
ArrayList<String> results1 = new ArrayList<String>();
while (mcursor.moveToNext()) {
results.add(mcursor.getString(0));
results1.add(mcursor.getString(1));
}
return results + ","+ results1;
}
非常感谢您的帮助
答案 0 :(得分:3)
你基本上是在创建两个ArrayLists - 第一个只包含Cursor中的第0列,第二个只包含第1列。然后返回所有第0列结果,后跟“,”,后跟列的所有结果1。
尝试StringBuilder
整个事情。实施例...
StringBuilder sb = new StringBuilder();
while (mcursor.moveToNext()) {
sb.append(mcursor.getString(0) + "," + mcursor.getString(1) + "\n");
}
return sb.toString();