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 ...
随机推荐
- Windows换行符问题
MAC 和 Windows的换行符不一样,导致有些情况下,MAC编辑的多行文本,在windows的TXT中只是一行. 使用nodepad++可以正确识别出换行符,而且可以将其转为Windows格式,使 ...
- Codeforces Round #385 (Div. 1) C. Hongcow Buys a Deck of Cards
地址:http://codeforces.com/problemset/problem/744/C 题目: C. Hongcow Buys a Deck of Cards time limit per ...
- Loopback接口的作用
Loopback接口是虚拟接口,是一种纯软件性质的虚拟接口.任何送到该接口的网络数据报文都会被认为是送往设备自身的.大多数平台都支持使用这种接口来模拟真正的接口.这样做的好处是虚拟接口不会像物理接口那 ...
- adas--智能驾驶辅助系统
先进驾驶辅助系统(Advanced Driver AssistantSystem),简称ADAS,是利用安装于车上的各式各样的传感器(可侦测光.热.压力等变数), 在第一时间收集车内外的环境数据, 进 ...
- powerdesign初级入门教程
首先我们需要创建一个测试数据库,为了简单,我们在这个数据库中只创建一个Student表和一个Major表.其表结构和关系如下所示. 看看怎样用PowerDesigner快速的创建出这个数据库吧. 1. ...
- Bean的id、name、ref、refid
Spring中Bean的命名 1.每个Bean可以有一个id属性,并可以根据该id在IoC容器中查找该Bean,该id属性值必须在IoC容器中唯一: 2.可以不指定id属性,只指定全限定类名,如: & ...
- Rreact Native 常见错误总结
1.invariant violation:expected a component class,got[object object] 创建自定义组件首字母要大写,否则会报错. ...
- Linux系统中的screen命令基本使用教程
作为linux服务器管理员,经常要使用ssh登陆到远程linux机器上做一些耗时的操作. 也许你遇到过使用telnet或SSH远程登录linux,运行一些程序.如果这些程序需要运行很长时间(几个小时) ...
- jquery在线引用
转载:http://www.cnblogs.com/lzx-1024/p/7716615.html jquery-3.1.1(最新)官网jquery压缩版引用地址:<script src=&qu ...
- web API help pages with Swagger / OpenAPI
https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?view=aspnetc ...