我现在已经工作了很长一段时间,而我能得到的最远的是将整个查询从数据库写入一行中的.CSV文件。
当我使用writeNext(string [])和下面的代码时,.csv写在一个长行上。
//Export the orders in the database to a CSV file. Can be transferred from device either by USB or bluetooth
public void exportOrders(View view)
{
try
{
CSVWriter writer;
writer = new CSVWriter(new FileWriter("/sdcard/orders.csv"), ',');
writer.writeNext(cursor.getColumnNames()); //Write the column headings first before the looping starts
//Loop through all results (use row count of table)
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
{
String[] entries = (cursor.getString(0)+ "," +cursor.getString(1)+ "," +cursor.getString(2)+ "," +cursor.getString(3)
+ "," +cursor.getString(4)).split(","); //Split the string by the delimiter
writer.writeNext(entries); //Write current row of DB to file
writer.flush(); //Flush the stream
}
Toast.makeText(this, "Orders exported to .CSV successfully!", Toast.LENGTH_SHORT).show();
writer.close();
}
catch (Exception erMsg)
{
Toast.makeText(this, erMsg.toString(), Toast.LENGTH_LONG).show();
erMsg.printStackTrace();
}
}
从我正在阅读的内容来看,这似乎就是这样做的方式,但我不知道如何从该代码生成新行?有没有办法将Cursor与新行一起使用,或者使用ResultSet和writeAll(结果)是标准的吗?
答案 0 :(得分:0)
执行此操作的决议是使用正确的参数声明CSVWriter。这是新的构造函数:
writer = new CSVWriter(new FileWriter("/sdcard/orders.csv"), ',', CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER, "\r\n");
这将允许为每一行创建一个新行。为确保此方法有效,请使用" \ r \ n" ,而不仅仅是" \ n" ,因为有些编辑不会这样做; t识别" \ n" 。