Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
li-yu committed Jul 22, 2020
1 parent 3c72790 commit 71f31ad
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README-EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ From v1.0.8, support custom SQL query.
## How to use
#### 1. Add Gradle dependencies
``` Gradle
compile 'com.liyu.tools:sqlitetoexcel:1.0.9'
compile 'com.liyu.tools:sqlitetoexcel:1.0.10'
```

#### 2. SQLite -> Excel [demo](https://github.com/li-yu/SQLiteToExcel/blob/master/app/src/main/java/com/liyu/demo/MainActivity.java)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ v1.0.8 版本开始支持自定义 SQL 查询导出了。
## 如何使用
#### 1. 添加 Gradle 依赖
``` Gradle
implementation 'com.liyu.tools:sqlitetoexcel:1.0.9'
implementation 'com.liyu.tools:sqlitetoexcel:1.0.10'
```

#### 2. SQLite -> Excel [demo](https://github.com/li-yu/SQLiteToExcel/blob/master/app/src/main/java/com/liyu/demo/MainActivity.java)
Expand Down
3 changes: 2 additions & 1 deletion app-kt/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.liyu.demokt">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
34 changes: 33 additions & 1 deletion app-kt/src/main/java/com/liyu/demokt/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,44 @@
package com.liyu.demokt

import android.support.v7.app.AppCompatActivity
import android.app.ProgressDialog
import android.os.Bundle
import android.os.Environment
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Toast
import com.liyu.sqlitetoexcel.kt.SQLiteToExcel
import java.io.File

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}

fun onClick(view: View) {
val progressDialog = ProgressDialog(this)
progressDialog.setMessage("导出中...")
val dbpath = (Environment.getExternalStorageDirectory().path
+ File.separator + "citys.db")

SQLiteToExcel(this).start {
databasePath = dbpath
tables = arrayOf("citys")
outputPath = Environment.getExternalStorageDirectory().path
outputFileName = "2020citys.xls"
encryptKey = "12345678"
protectKey = "87654321"
onStart {
progressDialog.show()
}
onCompleted {
progressDialog.dismiss()
Toast.makeText(this@MainActivity, "导出成功!$it", Toast.LENGTH_SHORT).show()
}
onError {
Toast.makeText(this@MainActivity, "$it", Toast.LENGTH_SHORT).show()
}
}
}
}
3 changes: 2 additions & 1 deletion app-kt/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:onClick="onClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.liyu.sqlitetoexcel.kt
import android.content.Context
import java.lang.Exception

class SQLiteToExcel(val context: Context) {
class SQLiteToExcel(private val context: Context) {

var databasePath: String? = null
var tables: Array<String> = arrayOf("")
Expand All @@ -16,31 +16,36 @@ class SQLiteToExcel(val context: Context) {
private var onError: ((Throwable) -> Unit)? = null
private var onStart: (() -> Unit)? = null

fun start(block: SQLiteToExcel.() -> Unit) {
fun start(block: SQLiteToExcel.() -> Unit): String? {
block()
com.liyu.sqlitetoexcel.SQLiteToExcel.Builder(context)
val sqLiteToExcel = com.liyu.sqlitetoexcel.SQLiteToExcel.Builder(context)
.setDataBase(databasePath)
.setTables(*tables)
.setOutputPath(outputPath)
.setOutputFileName(outputFileName)
.setEncryptKey(encryptKey)
.setProtectKey(protectKey)
.start(object : com.liyu.sqlitetoexcel.SQLiteToExcel.ExportListener {
override fun onStart() {
onStartInner()
}
.build()
if (onCompleted == null && onError == null && onStart == null) {
return sqLiteToExcel.start()
} else {
sqLiteToExcel.start(object : com.liyu.sqlitetoexcel.SQLiteToExcel.ExportListener {
override fun onStart() {
onStartInner()
}

override fun onCompleted(filePath: String) {
onCompletedInner(filePath)
}
override fun onCompleted(filePath: String) {
onCompletedInner(filePath)
}

override fun onError(e: Exception) {
onErrorInner(e)
}
})
override fun onError(e: Exception) {
onErrorInner(e)
}
})
return null
}
}


fun onCompleted(block: (String) -> Unit) {
onCompleted = block
}
Expand Down
10 changes: 5 additions & 5 deletions sqlitetoexcel/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 27
versionCode 9
versionName "1.0.9"
versionCode 10
versionName "1.0.10"
}
buildTypes {
release {
Expand All @@ -28,7 +28,7 @@ dependencies {
def siteUrl = 'https://github.com/li-yu/SQLiteToExcel'
def gitUrl = 'https://github.com/li-yu/SQLiteToExcel.git'

version = "1.0.9"
version = "1.0.10"
group = "com.liyu.tools"

Properties properties = new Properties()
Expand All @@ -45,9 +45,9 @@ bintray {
licenses = ["Apache-2.0"]
publish = true
version {
name = '1.0.9'
name = '1.0.10'
released = new Date()
vcsTag = 'v1.0.9'
vcsTag = 'v1.0.10'
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,27 @@ public static class Builder {
private List<String> tables;
private String sql;
private String sheetName;
private Context context;

public Builder(Context context) {
this.filePath = context.getExternalFilesDir(null).getPath();
this.context = context;
}

public SQLiteToExcel build() {
if (TextUtils.isEmpty(dataBaseName)) {
throw new IllegalArgumentException("Database name must not be null.");
}
if (TextUtils.isEmpty(filePath)) {
this.filePath = context.getExternalFilesDir(null).getPath();
}
if (TextUtils.isEmpty(fileName)) {
throw new IllegalArgumentException("Output file name must not be null.");
this.fileName = new File(dataBaseName).getName() + ".xls";
}
return new SQLiteToExcel(tables, protectKey, encryptKey, fileName, dataBaseName, filePath, sql, sheetName);
}

public Builder setDataBase(String dataBaseName) {
this.dataBaseName = dataBaseName;
this.fileName = new File(dataBaseName).getName() + ".xls";
return this;
}

Expand Down Expand Up @@ -128,7 +131,7 @@ public Builder setSQL(@NonNull String sql) {
return setSQL("Sheet1", sql);
}

public String start() {
public String start() throws Exception {
final SQLiteToExcel sqliteToExcel = build();
return sqliteToExcel.start();
}
Expand All @@ -144,7 +147,7 @@ public void start(ExportListener listener) {
*
* @return output file path
*/
public String start() {
public String start() throws Exception {
try {
if (tables == null || tables.size() == 0) {
tables = getTablesName(database);
Expand All @@ -154,7 +157,7 @@ public String start() {
if (database != null && database.isOpen()) {
database.close();
}
return null;
throw e;
}
}

Expand All @@ -175,12 +178,12 @@ public void run() {
if (tables == null || tables.size() == 0) {
tables = getTablesName(database);
}
final String filePath = exportTables(tables, fileName);
final String finalFilePath = exportTables(tables, fileName);
if (listener != null) {
handler.post(new Runnable() {
@Override
public void run() {
listener.onCompleted(filePath);
listener.onCompleted(finalFilePath);
}
});
}
Expand Down Expand Up @@ -208,7 +211,7 @@ private SQLiteToExcel(List<String> tables, String protectKey, String encryptKey,
this.filePath = filePath;
this.sql = sql;
this.sheetName = sheetName;

this.tables = tables;
try {
database = SQLiteDatabase.openOrCreateDatabase(dataBaseName, null);
} catch (Exception e) {
Expand Down

0 comments on commit 71f31ad

Please sign in to comment.