如何将数据导出到csv文件在Android中? [英] How to export data to csv file in Android?
本文介绍了如何将数据导出到csv文件在Android中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下格式创建了一个csv文件,其目标是输出到设备的sd卡:
I created a csv file with the following format which I'm aiming to output to the device's sd card:
Ship Name,Scientist Name,Scientist Email,Sample Volume,Sample Colour,Longitude,Latitude,Material,Date
< csv中的每个值都将是字符串类型,除了date的最后一个值。
csv文件的名称是 AnalysisData.csv
我看过Stackoverflow的例子因此,从应用程序中导出我的CSV文件数据android < a>但是这会创建一个我不想要的新文件。
I've looked examples on Stackoverflow such as this, Export my data on CSV file from app android but this creates a new file which I don't want.
我已经将opencsv jar添加到我的项目,所以只需要一个相关的例子。
I have already added the opencsv jar to my project so just need a relevant example.
有没有人在Android中实现这一点有任何建议?
Does anyone have any advice on achieving this in Android?
推荐答案
此代码段:
String baseDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "AnalysisData.csv";
String filePath = baseDir + File.separator + fileName;
File f = new File(filePath );
CSVWriter writer;
// File exist
if(f.exists() && !f.isDirectory()){
mFileWriter = new FileWriter(filePath , true);
writer = new CSVWriter(mFileWriter);
}
else {
writer = new CSVWriter(new FileWriter(filePath));
}
String[] data = {"Ship Name","Scientist Name", "...",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").formatter.format(date)});
writer.writeNext(data);
writer.close();
这篇关于如何将数据导出到csv文件在Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文