抬头仰望星空,是否能发现自己的渺小。

伪斜杠青年

人们总是混淆了欲望和理想

Android SQLite与CSV表格的导入导出

csv表格是一种方便数据导入导出的Excel表格文件,为什么呢,因为,它只是用逗号分隔开了而已。。。。

一般从数据库读出来的一条数据都是一个实体,如果你的不是,就自己写个转换,我直接复写了toString,至于这个方法是不是用来这样做的,我就不得而知了,哈哈

toString方法:一共是6列,这样返回的就是首尾不带逗号

@Override
public String toString() {
 //收支 类别 金额 备注 日期 时间
 return class+","+type+","+cost+","+comment+","+date+","+time;
}

输出格式为:收支,类别,金额,备注,日期,时间

工具类

/**
 * Created by root on 17-8-18.
 */
public class CSVUtil {
 private static final String TAG = "info";
 private static final int RECORD_CLASSES = 0;
 private static final int RECORD_TYPE = 1;
 private static final int RECORD_COST = 2;
 private static final int RECORD_COMMENT = 3;
 private static final int RECORD_DATE = 4;
 private static final int RECORD_TIME = 5;
 private static final String DEFAULT_STATUS = "0";
 private static final int ERROR = -1;

 /**
 * 导出为CSV文件
 * @param record 对象集合
 * @param msg 文件名
 * @return true or false
 */ public static boolean exportCSV(List<Record> record, String msg) {
 //新建一个文件对象 
 File file = new File(Environment.getExternalStorageDirectory() + "/Weekly/exportData/" + getDate() + msg + ".csv");
 //判断文件是否存在
 if (!file.exists()) {
 //不存在则创建多级目录
 boolean mkdir = file.getParentFile().mkdirs();
 Log.d(TAG, "CSVUtil exportCSV mkdir: " + mkdir);
 } else {
 //存在则删除旧文件
 boolean delete = file.delete();
 Log.d(TAG, "CSVUtil exportCSV delete: " + delete);
 }
 BufferedWriter bufferedWriter=null;
 try {
 bufferedWriter = new BufferedWriter(new FileWriter(file, true));
 //第一行
 bufferedWriter.append("收支,类别,金额,备注,日期,时间");
 bufferedWriter.newLine();
 for (Record r : record) {
 bufferedWriter.append(r.toString());
 bufferedWriter.newLine();
 }
 bufferedWriter.flush();
 return true;
 } catch (IOException e) {
 e.printStackTrace();
 }finally {
 try {
 if (bufferedWriter != null) {
 bufferedWriter.close();
 }
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 return false;
 }

 /**
 * 导入到数据库
 * @param file 文件来源
 * @return 错误或者导入条数
 */ public static int importCSV(String file) {
 File csv = new File(file);
 if (!csv.exists()) {
 Log.d(TAG, "CSVUtil importCSV: file not found");
 return ERROR;
 }
 int count = -1;
 BufferedReader bufferedReader = null;
 try {
 bufferedReader = new BufferedReader(new FileReader(csv));
 String line = "";
 while ((line = bufferedReader.readLine()) != null) {
 String[] datas = line.split(",");
 if (datas.length != 6) {
 return ERROR;
 }
 //第一次存入的是列名而不是数据,需要排除
 if (count >= 0) {
 //重新存入数据库
 Record record = new Record();
 record.setClasses(datas[RECORD_CLASSES]);
 record.setType(datas[RECORD_TYPE]);
 record.setCost(datas[RECORD_COST]);
 record.setComment(datas[RECORD_COMMENT]);
 record.setDate(datas[RECORD_DATE]);
 record.setTime(datas[RECORD_TIME]);
 record.setStatus(DEFAULT_STATUS);
 record.setUUID(newUUID());
 record.save();
 }
 count++;
 }
 } catch (Exception e) {
 e.printStackTrace();
 return ERROR;
 } finally {
 try {
 if (bufferedReader != null) {
 bufferedReader.close();
 }
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 return count;
 }
}

导出结果为:

收支,类别,金额,备注,日期,时间

支出,日常饮食,10.0,日常饮食,2017-08-20,09:32

支出,日常饮食,10.0,日常饮食,2017-08-20,09:32

支出,日常饮食,10.0,日常饮食,2017-08-20,09:32

支出,日常饮食,10.0,日常饮食,2017-08-20,09:32

其实很简单,其实并不难,所以网上的数据也很少,记录下。当然可以再写复杂点什么的  ,按需求来,不秀操作。。。


本站由以下主机服务商提供服务支持:

0条评论

发表评论