Skip to content

Commit

Permalink
release v1.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
li-yu committed Jul 21, 2017
1 parent e2a27a0 commit a53e7fc
Show file tree
Hide file tree
Showing 15 changed files with 572 additions and 483 deletions.
86 changes: 25 additions & 61 deletions README-EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,85 +6,49 @@

The SQLiteToExcel library integrates [Apache POI](http://poi.apache.org/) and some basic database query operations, making it easier to convert between SQLite and Excel.

From v1.0.5, **not support xlsx** format any more, because poi ooxml lib and other dependencies are so big( > 10 MB), and there are some strange problems on Android.

## Version history
[Release Notes](https://github.com/li-yu/SQLiteToExcel/releases)

## How to use
#### 1.Add Gradle dependencies or download the Jar file as libs to the project
#### 1.Add Gradle dependencies
``` Gradle
compile 'com.liyu.tools:sqlitetoexcel:1.0.4'
```
[SqliteToExcel-v1.0.4.jar](https://github.com/li-yu/SQLiteToExcel/releases)
#### 2.Add SD card read and write permissions to AndroidManifest.xml (Android 6.0 and above need to deal with run-time permissions)
```xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
compile 'com.liyu.tools:sqlitetoexcel:1.0.5'
```

#### 3.SQLite -> Excel Sample code(Specific examples can be found in [demo](https://github.com/li-yu/SQLiteToExcel/blob/master/app/src/main/java/com/liyu/demo/MainActivity.java))
* Initialize (Default export path is external SD card root directory ```Environment.getExternalStorageDirectory()```)
```java
SqliteToExcel ste = new SqliteToExcel(this, "helloworld.db");
```
or(Specifies the root directory of the export)
```java
SqliteToExcel ste = new SqliteToExcel(this, "helloworld.db", "/mnt/sdcard/myfiles/");
```
* Export a single table to excel
```java
ste.startExportSingleTable(String table, String fileName, ExportListener listener);
```
* Export multiple tables to excel
```java
ste.startExportTables(List<String> tables, String fileName, ExportListener listener);
```
* Export all tables to excel
```java
ste.startExportAllTables(String fileName, ExportListener listener);
```
* Task listener interface
#### 2.SQLite -> Excel Sample code(Specific examples can be found in [demo](https://github.com/li-yu/SQLiteToExcel/blob/master/app/src/main/java/com/liyu/demo/MainActivity.java))
```java
public interface ExportListener {
void onStart();

void onCompleted(String filePath);

void onError(Exception e);
}
new SQLiteToExcel
.Builder(this)
.setDataBase(databasePath) //Required. Tips: internal database path can be got by context.getDatabasePath("internal.db").getPath()
.setTables(table1, table2) //Optional, if null, all tables will be export.
.setPath(outoutPath) //Optional, if null, default output path is app ExternalFilesDir.
.setFileName("test.xls") //Optional, if null, default output file name is xxx.db.xls
.setEncryptKey("1234567") //Optional, if you want to encrypt the output file.
.setProtectKey("9876543") //Optional, if you want to set the sheet read only.
.start(ExportListener); // or .start() for synchronous method.
```

#### 4.Excel -> SQLite Sample code(Specific examples can be found in [demo](https://github.com/li-yu/SQLiteToExcel/blob/master/app/src/main/java/com/liyu/demo/MainActivity.java))
* Initialize
#### 3.Excel -> SQLite Sample code(Specific examples can be found in [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");
```
* From the assets directory
```java
ets.startFromAsset(String assetFileName, ImportListener listener);
```
* For any excel files
```java
ets.startFromFile(File file, ImportListener listener);
```
* Task listener interface
```java
public interface ImportListener {
void onStart();

void onCompleted(String dbName);

void onError(Exception e);
}
new ExcelToSQLite
.Builder(this)
.setDataBase(databasePath) //Required.
.setAssetFileName("user.xls") // if it is a asset file.
.setFilePath("/storage/doc/user.xls") // if it is a normal file.
.start(ImportListener); // or .start() for synchronous method.
```

#### 5.Thanks
#### 4.Thanks (how to support xlsx?)
- [https://github.com/centic9/poi-on-android](https://github.com/centic9/poi-on-android)
- [https://github.com/FasterXML/aalto-xml](https://github.com/FasterXML/aalto-xml)
- [https://github.com/johnrengelman/shadow](https://github.com/johnrengelman/shadow)

#### 6.Precautions
* When convert Excel to SQLite,the default take excel sheet in the first row as the database table column name, please refer to the style [demo](https://github.com/li-yu/SQLiteToExcel/blob/master/app/src/main/assets/user.xls).
#### 5.Precautions
* When convert Excel to SQLite, take excel sheet first row as the database table column name, please refer to the style [demo](https://github.com/li-yu/SQLiteToExcel/blob/master/app/src/main/assets/user.xls).
* Currently only blob field is supported as a picture, because I do not know whether byte [] is a file or a picture.
* The database files must be located under ```/data/data/package name/databases/` ``, and are usually located in this directory.
* ~~The database files must be located under ```/data/data/package name/databases/` ``, and are usually located in this directory.~~

## About me
* Email: [[email protected]](mailto:[email protected])
Expand Down
82 changes: 23 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,51 @@

[English README](README-EN.md)

SQLiteToExcel 库整合了 [Apache POI](http://poi.apache.org/) 和一些基本的数据库查询操作,使得 SQLite 和 Excel 之间相互转换更加便捷。
SQLiteToExcel 库整合了 [Apache POI](http://poi.apache.org/) 和一些基本的数据库操作,使得 SQLite 和 Excel 之间相互转换更加便捷。

从 v1.0.5 版本开始,不再支持 **xlsx 格式**,因为 poi ooxml 库和其他一些相关的依赖太大了,体积超过了 10MB,同时开发过程中也发现 poi 对于 Android 支持不够全面,放弃 xlsx 也是挺无奈的,个人觉得 xls 格式对于我们来说已经够用。

## 更新历史
[Release Notes](https://github.com/li-yu/SQLiteToExcel/releases)

## 如何使用
#### 1.添加 Gradle 依赖或者下载 Jar 文件作为 libs 添加到工程中
#### 1.添加 Gradle 依赖
``` Gradle
compile 'com.liyu.tools:sqlitetoexcel:1.0.4'
```
[SqliteToExcel-v1.0.4.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" />
compile 'com.liyu.tools:sqlitetoexcel:1.0.5'
```

#### 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");
```
或(指定导出的根目录)
```java
SqliteToExcel ste = new SqliteToExcel(this, "helloworld.db", "/mnt/sdcard/myfiles/");
```
* 导出单个表到 excel
```java
ste.startExportSingleTable(String table, String fileName, ExportListener listener);
```
* 导出多个表到 excel
```java
ste.startExportTables(List<String> tables, String fileName, ExportListener listener);
```
* 导出所有表到 excel
```java
ste.startExportAllTables(String fileName, ExportListener listener);
```
* 任务监听器接口
```java
public interface ExportListener {
void onStart();

void onCompleted(String filePath);

void onError(Exception e);
}
new SQLiteToExcel
.Builder(this)
.setDataBase(databasePath) //必须。 小提示:内部数据库可以通过 context.getDatabasePath("internal.db").getPath() 获取。
.setTables(table1, table2) //可选, 如果不设置,则默认导出全部表。
.setPath(outoutPath) //可选, 如果不设置,默认输出路径为 app ExternalFilesDir。
.setFileName("test.xls") //可选, 如果不设置,输出的文件名为 xxx.db.xls。
.setEncryptKey("1234567") //可选,可对导出的文件进行加密。
.setProtectKey("9876543") //可选,可对导出的表格进行只读的保护。
.start(ExportListener); // 或者使用 .start() 同步方法。
```

#### 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);
}
new ExcelToSQLite
.Builder(this)
.setDataBase(databasePath) //必须。
.setAssetFileName("user.xls") // 如果文件在 asset 目录。
.setFilePath("/storage/doc/user.xls") // 如果文件在其他目录。
.start(ImportListener); // 或者使用 .start() 同步方法。
```

#### 5.感谢
#### 4.感谢(如何支持 xlsx 可以参考以下仓库)
- [https://github.com/centic9/poi-on-android](https://github.com/centic9/poi-on-android)
- [https://github.com/FasterXML/aalto-xml](https://github.com/FasterXML/aalto-xml)
- [https://github.com/johnrengelman/shadow](https://github.com/johnrengelman/shadow)

#### 6.注意事项
#### 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/```下,一般都是位于这个目录下。
* ~~数据库文件须位于```/data/data/包名/databases/```下,一般都是位于这个目录下。~~

## 关于我
* Email: [[email protected]](mailto:[email protected])
Expand Down
7 changes: 1 addition & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"

defaultConfig {
applicationId "com.liyu.sqlitetoexcel"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
Expand All @@ -29,11 +28,7 @@ android {

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.0'
testCompile 'junit:junit:4.12'
compile 'org.litepal.android:core:1.5.1'
compile project(path: ':sqlitetoexcel')
}
Binary file modified app/src/main/assets/user.xls
Binary file not shown.
Binary file removed app/src/main/assets/user2.xlsx
Binary file not shown.
106 changes: 66 additions & 40 deletions app/src/main/java/com/liyu/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.annotation.DrawableRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

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

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

Expand All @@ -38,57 +40,81 @@ protected void onCreate(Bundle savedInstanceState) {
}

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

@Override
public void onCompleted(String filePath) {
tv.append(makeLog("\n" +
"Export completed--->" + filePath));
}
String databasePath1 = this.getDatabasePath("ste_db.db").getAbsolutePath();

String databasePath2 = Environment.getExternalStorageDirectory().getPath()
+ File.separator + "citychina.db";

new SQLiteToExcel
.Builder(this)
.setDataBase(databasePath2)
// .setTables("user")
// .setPath(Environment.getExternalStorageDirectory().getPath())
// .setFileName("test.xls")
// .setEncryptKey("1234567")
// .setProtectKey("9876543")
.start(new SQLiteToExcel.ExportListener() {
@Override
public void onStart() {
tv.append(makeLog("\n" +
"Export importTables--->"));
}

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

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

}
});
}

public void onImport(View v) {
ExcelToSqlite ets = new ExcelToSqlite(this, "user.db");
ets.startFromAsset("user2.xlsx", 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);
}
final String databasePath1 = this.getDatabasePath("internal.db").getAbsolutePath();

final String databasePath2 = Environment.getExternalStorageDirectory().getPath()
+ File.separator + "external.db";

new ExcelToSQLite
.Builder(this)
.setDataBase(databasePath1)
.setAssetFileName("user.xls")
// .setFilePath(Environment.getExternalStorageDirectory().getPath()
// + File.separator + "test.xls")
.start(new ExcelToSQLite.ImportListener() {
@Override
public void onStart() {
tv.append(makeLog("\n" +
"Import importTables--->"));
}

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

@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);
database = SQLiteDatabase.openOrCreateDatabase(dbName, 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) + " ");
Expand Down
Loading

0 comments on commit a53e7fc

Please sign in to comment.