Skip to content

Commit

Permalink
新增 Excel 导入 SQLite 功能
Browse files Browse the repository at this point in the history
  • Loading branch information
li-yu committed Mar 31, 2017
1 parent 523fa21 commit 7bd717c
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 39 deletions.
46 changes: 37 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# SQLiteToExcel
SQLiteToExcel 库整合了 [Apache POI](http://poi.apache.org/) 和一些基本的数据库查询操作,使得生成 excel 文件更加便捷
SQLiteToExcel 库整合了 [Apache POI](http://poi.apache.org/) 和一些基本的数据库查询操作,使得 SQLite 和 Excel 之间相互转换更加便捷

## 更新历史
2017-03-31 : v1.0.3
- 新增 Excel 导入 SQLite 数据库的功能

2017-03-28 : v1.0.2
- 上传到 JCenter

Expand All @@ -15,21 +18,20 @@ SQLiteToExcel 库整合了 [Apache POI](http://poi.apache.org/) 和一些基本
- Apache POI 版本同步更新到 v3.13

## 主要功能
* 导出单个表
* 导出多个表
* 导出所有表
* SQLite <-> Excel 相互转换

## 如何使用
#### 1.添加 Gradle 依赖或者下载 Jar 文件作为 libs 添加到工程中
``` Gradle
compile 'com.liyu.tools:sqlitetoexcel:1.0.2'
compile 'com.liyu.tools:sqlitetoexcel:1.0.3'
```
[SqliteToExcel-v1.0.2.jar](https://github.com/li-yu/SQLiteToExcel/raw/master/SqliteToExcel-v1.0.2.jar)
#### 2.添加 SD 卡读写权限到 AndroidManifest.xml(Android 6.0 需要处理运行时权限
[SqliteToExcel-v1.0.3.jar](https://github.com/li-yu/SQLiteToExcel/releases)
#### 2.添加 SD 卡读写权限到 AndroidManifest.xml(Android 6.0 及以上需要处理运行时权限
```xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
#### 3.示例代码(具体示例可参考 [demo](https://github.com/li-yu/SQLiteToExcel/blob/master/app/src/main/java/com/liyu/demo/MainActivity.java) 工程)

#### 3.SQLite -> Excel 示例代码(具体示例可参考 [demo](https://github.com/li-yu/SQLiteToExcel/blob/master/app/src/main/java/com/liyu/demo/MainActivity.java) 工程)
* 初始化(默认导出路径为外部 SD 卡根目录 ```Environment.getExternalStorageDirectory()```
```java
SqliteToExcel ste = new SqliteToExcel(this, "helloworld.db");
Expand Down Expand Up @@ -60,7 +62,33 @@ public interface ExportListener {
void onError(Exception e);
}
```
#### 4.注意事项

#### 4.Excel -> SQLite 示例代码(具体示例可参考 [demo](https://github.com/li-yu/SQLiteToExcel/blob/master/app/src/main/java/com/liyu/demo/MainActivity.java) 工程)
* 初始化)
```java
ExcelToSqlite ets = new ExcelToSqlite(this, "user.db");
```
* 从 assets 目录传入 excel 文件
```java
ets.startFromAsset(String assetFileName, ImportListener listener);
```
* 以 File 形式传入任意 excel 文件
```java
ets.startFromFile(File file, ImportListener listener);
```
* 任务监听器接口
```java
public interface ImportListener {
void onStart();

void onCompleted(String dbName);

void onError(Exception e);
}
```

#### 5.注意事项
* Excel 导入 SQLite 时,默认取 excel 中 sheet 的**第一行**作为数据库表的列名,样式请参考 [demo](https://github.com/li-yu/SQLiteToExcel/blob/master/app/src/main/assets/user.xls)
* 目前仅支持 blob 字段导出为图片,因为我也不知道 byte[] 是文件还是图片。
* 数据库文件须位于```/data/data/包名/databases/```下,一般都是位于这个目录下。

Expand Down
Binary file removed SqliteToExcel-v1.0.2.jar
Binary file not shown.
Binary file added app/src/main/assets/user.xls
Binary file not shown.
69 changes: 60 additions & 9 deletions app/src/main/java/com/liyu/demo/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.liyu.demo;

import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.DrawableRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.TextView;

import com.liyu.sqlitetoexcel.ExcelToSqlite;
import com.liyu.sqlitetoexcel.SqliteToExcel;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

Expand All @@ -33,34 +37,81 @@ protected void onCreate(Bundle savedInstanceState) {

}

public void onClick(View v) {
public void onExport(View v) {
SqliteToExcel ste = new SqliteToExcel(this, "ste_db.db");
ste.startExportAllTables("test.xls", new SqliteToExcel.ExportListener() {
@Override
public void onStart() {
tv.append(makeLog("\n" +
"start--->"));
"Export start--->"));
}

@Override
public void onCompleted(String filePath) {
tv.append(makeLog("\n" +
"completed--->" + filePath));
"Export completed--->" + filePath));
}

@Override
public void onError(Exception e) {
tv.append(makeLog("\n" +
"error--->" + e.toString()));
"Export error--->" + e.toString()));

}
});
}

public void onImport(View v) {
ExcelToSqlite ets = new ExcelToSqlite(this, "user.db");
ets.startFromAsset("user.xls", new ExcelToSqlite.ImportListener() {
@Override
public void onStart() {
tv.append(makeLog("\n" +
"Import start--->"));
}

@Override
public void onCompleted(String dbName) {
tv.append(makeLog("\n" +
"Import completed--->"));
showDbMsg(dbName);
}

@Override
public void onError(Exception e) {
tv.append(makeLog("\n" +
"Import error--->" + e.toString()));
}
});
}

private void showDbMsg(String dbName) {
SQLiteDatabase database;
try {
database = SQLiteDatabase.openOrCreateDatabase(MainActivity.this.getDatabasePath(dbName).getAbsolutePath(), null);
Cursor cursor = database.rawQuery("select name from sqlite_master where type='table' order by name", null);
while (cursor.moveToNext()) {
tv.append("\nNew tables is : " + cursor.getString(0) + " ");
Cursor cursor2 = database.rawQuery("select * from " + cursor.getString(0), null);
while (cursor2.moveToNext()) {
tv.append("\n");
for (int i = 0; i < cursor2.getColumnCount(); i++) {
tv.append(cursor2.getString(i) + " ");
}
}
cursor2.close();
}
cursor.close();
database.close();
} catch (Exception e) {
e.printStackTrace();
}
}

private String makeLog(String log) {
return log + DateUtils.formatDateTime(MainActivity.this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_YEAR |
DateUtils.FORMAT_SHOW_DATE |
DateUtils.FORMAT_SHOW_WEEKDAY |
DateUtils.FORMAT_SHOW_TIME);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
return log + " " + sdf.format(now);
}

private byte[] getResByte(@DrawableRes int id) {
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="onClick"
android:text="Start"/>
android:onClick="onExport"
android:text="Start export"/>

<Button
android:text="Start import"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:onClick="onImport"
android:id="@+id/button2"/>
</RelativeLayout>
10 changes: 5 additions & 5 deletions sqlitetoexcel/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 25
versionCode 3
versionName "1.0.2"
versionCode 4
versionName "1.0.3"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand All @@ -32,7 +32,7 @@ dependencies {
def siteUrl = 'https://github.com/li-yu/SQLiteToExcel'
def gitUrl = 'https://github.com/li-yu/SQLiteToExcel.git'

version = "1.0.2"
version = "1.0.3"
group = "com.liyu.tools"

Properties properties = new Properties()
Expand All @@ -49,9 +49,9 @@ bintray {
licenses = ["Apache-2.0"]
publish = true
version {
name = '1.0.2'
name = '1.0.3'
released = new Date()
vcsTag = 'v1.0.2'
vcsTag = 'v1.0.3'
attributes = ['gradle-plugin': 'com.use.less:com.use.less.gradle:gradle-useless-plugin']
desc = 'A simple lib for Android to export SQLite to Excel.'
}
Expand Down
Loading

0 comments on commit 7bd717c

Please sign in to comment.