android项目开发中有时需要把数据导出到Excel表中
这里用到了.csv文件格式
度娘是这样解释csv的:
csv是逗号分隔值文件格式,一般用WORDPAD或记事本(NOTE),EXCEL打开。csv(逗号分隔值)是一种用来存储数据的纯文本文件,通常都是用于存放电子表格或数据的一种文件格式。
比如在excel中,我们需要存储一个班级学生的信息,姓名,学号,性别。
这样每一行即代表一个学生的信息。
在csv文件中我们可以这样写
String str1 = "张三" + "," + "10241" + "," + "男" + "\n";
String str1 = "李四" + "," + "10242" + "," + "女" + "\n";
最后面的换行符代表一条数据的结束,下一条数据从下一行开始。
public static void writeToFile(String data) { File file = new File(Environment.getExternalStorageDirectory(), "data.csv");//根目录下 OutputStream os = null; try { os = new FileOutputStream(file.getAbsoluteFile(),true);//true 为添加 不覆盖 os.write(data.getBytes()); os.flush(); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
本文出自 “爬过山见过海” 博客,请务必保留此出处http://670176656.blog.51cto.com/4500575/1700966
原文:http://670176656.blog.51cto.com/4500575/1700966