Android OpenCSV
OpenCSV
https://sourceforge.net/projects/opencsv/
使用参考
http://stackoverflow.com/questions/16672074/import-csv-file-to-sqlite-in-android
导出
public class ExportDatabaseToCSV extends AsyncTask<Void, Boolean, Boolean> {
Context context;
ProgressDialog dialog;
public ExportDatabaseToCSV(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(context);
dialog.setTitle("导出CSV文件");
dialog.setMessage("请稍后...");
dialog.setCancelable(false);
dialog.setIcon(android.R.drawable.ic_dialog_info);
dialog.show();
}
@Override
protected Boolean doInBackground(Void... params) {
File exportDir = new File(Environment.getExternalStorageDirectory(), "");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
Date date = new Date(System.currentTimeMillis());
String filename = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
File file = new File(exportDir, filename + ".csv");
try {
file.createNewFile();
CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
StringBuffer check = new StringBuffer();
check.append("SELECT * FROM NOTE");
DataBaseHelper db = DataBaseHelper.getInstance(GlobalField.getApplicationContext());
db.openDataBase();
Cursor curCSV = db.queryData(check.toString());
//key -> get the cursor and then using opencsv
csvWrite.writeNext(curCSV.getColumnNames());
while (curCSV.moveToNext()) {
//Which column you want to export you can add over here...
List<String> list = new ArrayList<>();
for (int i = 0, length = curCSV.getColumnCount(); i < length; i++) {
list.add(curCSV.getString(i));
}
String[] arrStr = list.toArray(new String[list.size()]);
csvWrite.writeNext(arrStr);
}
csvWrite.close();
curCSV.close();
return true;
} catch (Exception sqlEx) {
System.err.println(sqlEx.getMessage());
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
if (dialog.isShowing()) {
dialog.dismiss();
}
if (result) {
Toast.makeText(context, "SqLite Data has been Exported!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "SqLite Data has not Exported", Toast.LENGTH_LONG).show();
}
}
}
导入
public class ImportCVSToSQLiteDataBase extends AsyncTask<String, String, String> {
Activity activity;
Context context;
File file=null;
private ProgressDialog dialog;
public ImportCVSToSQLiteDataBase(Context context, Activity activity,File file) {
this.context=context;
this.activity=activity;
this.file=file;
}
@Override
protected void onPreExecute()
{
dialog=new ProgressDialog(context);
dialog.setTitle("Importing Data into SecureIt DataBase");
dialog.setMessage("Please wait...");
dialog.setCancelable(false);
dialog.setIcon(android.R.drawable.ic_dialog_info);
dialog.show();
}
@Override
protected String doInBackground(String... params) {
String data="";
Log.d(getClass().getName(), file.toString());
try{
CSVReader reader = new CSVReader(new FileReader(file));
String [] nextLine;
//here I am just displaying the CSV file contents, and you can store your file content into db from while loop...
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
String accId=nextLine[0];
String acc_name=nextLine[1];
data=data+"AccId:"+accId +" Account_name:"+acc_name+"\n";//change to save to datebase instead of showing
}
return data;
} catch (Exception e) {
Log.e("Error", "Error for importing file");
}
return data="";
}
protected void onPostExecute(String data)
{
if (dialog.isShowing())
{
dialog.dismiss();
}
if (data.length()!=0)
{
Toast.makeText(context, "File is built Successfully!"+"\n"+data, Toast.LENGTH_LONG).show();
}else{
Toast.makeText(context, "File fail to build", Toast.LENGTH_SHORT).show();
}
}
}
问题
导出的CSV文件如果使用Excel直接打开,可能出现中文乱码
解决办法:
使用记事本打开CSV文件,“文件”->“另存为”,编码方式选择ANSI,保存完毕后,用EXCEL打开这个文件就不会出现乱码的情况。
Android OpenCSV的更多相关文章
- 【英文版本】Android开源项目分类汇总
Action Bars ActionBarSherlock Extended ActionBar FadingActionBar GlassActionBar v7 appcompat library ...
- android github
Action Bars ActionBarSherlock Extended ActionBar FadingActionBar GlassActionBar v7 appcompat library ...
- Android开发免费类库和工具集合
用于Android开发的免费类库和工具集合,按目录分类. Action Bars ActionBarSherlock Extended ActionBar FadingActionBar GlassA ...
- 【原】Android热更新开源项目Tinker源码解析系列之三:so热更新
本系列将从以下三个方面对Tinker进行源码解析: Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Android热更新开源项目Tinker源码解析系列之二:资源文件热更新 A ...
- 配置android sdk 环境
1:下载adnroid sdk安装包 官方下载地址无法打开,没有vpn,使用下面这个地址下载,地址:http://www.android-studio.org/
- Android SwipeRefreshLayout 下拉刷新——Hi_博客 Android App 开发笔记
以前写下拉刷新 感觉好费劲,要判断ListView是否滚到顶部,还要加载头布局,还要控制 头布局的状态,等等一大堆.感觉麻烦死了.今天学习了SwipeRefreshLayout 的用法,来分享一下,有 ...
- Android Studio配置 AndroidAnnotations——Hi_博客 Android App 开发笔记
以前用Eclicps 用习惯了现在 想学学 用Android Studio 两天的钻研终于 在我电脑上装了一个Android Studio 并完成了AndroidAnnotations 的配置. An ...
- Android请求网络共通类——Hi_博客 Android App 开发笔记
今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...
- 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新
[原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...
随机推荐
- url监控
#!/usr/bin/env python #coding:utf-8 import MySQLdb,requests import time from datetime import datetim ...
- python 的两个模块xlwt,xlrd,写入和读取Excel数据
http://www.cnblogs.com/fireme/p/3887284.html 这上面写的很全,不过我只需要简单的读和写的操作就ok了,下面是我写的读和写入Excel操作 读取Excel数据 ...
- appcmd应用
appcmd资料: http://www.jb51.net/article/36024.htm 官方文档:https://docs.microsoft.com/zh-cn/iis/get-starte ...
- nodejs入门-静态文件服务器
本文展示是基于node.js的静态文件服务器,代码参考自这里,主要是练习node http.文件模块的使用,另外,对理解http协议也很有帮助除了实现了基本的路由控制,还实现了MIME类型.304缓存 ...
- gh-ost测试
gh-ost测试 1.不支持没有主键或者唯一索引的表 2018-08-24 09:53:33 FATAL No PRIMARY nor UNIQUE key found in table! Baili ...
- 一种BIM缺失多态性介导的酪氨酸激酶抑制剂的耐药性
论文名称:A common BIM deletion polymorphism mediates intrinsic resistance and inferior responses to tyro ...
- EFM32JG系列MCU内部温度传感器使用方法
在很多电子类应用场合中,我们经常需要采集产品工作的周围环境温度,一般采取的方式有两种: 1)外加温度传感器 2)采用MCU内部温度传感器 外加温度传感器会增加产品的成本以及布板空间,所以在很多场合,我 ...
- Win32 API编程:显示系统进程列表
#include <windows.h> #include <tlhelp32.h> // 声明快照函数的头文件 #include "tchar.h" #i ...
- 在Linux系统中使用蓝牙功能的基本方法
首先确定硬件上有支持蓝牙的设备,然后运行如下命令,就可以开到我们的蓝牙设备了: lsusb 运行hciconfig可以看到:从上图可以看出,我们的蓝牙设备是hci0运行hcitool dev可以看到我 ...
- SpringBoot @Annotation
Annotation简介 Annotation是JDK1.5引入的特性,包含在java.lang.annotation包中. 它是附加在代码中的一些元信息,将一个类的外部信息与内部成员联系起来,在 编 ...