desc:compose excel相关

main
xiaowusky 2 years ago
parent e0feb94d2a
commit e28d3afb95

@ -26,6 +26,7 @@ android {
dependencies {
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
var lifeCycle_version = '2.5.1'
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifeCycle_version"
@ -44,7 +45,15 @@ dependencies {
var accompanist_version = '0.30.1'
implementation "com.google.accompanist:accompanist-permissions:$accompanist_version"
//excel
implementation rootProject.ext.dependencies.jxl
implementation project(path: ':library-push')
implementation project(path: ':library-ijkplayer')
// documentfile 访U
implementation "androidx.documentfile:documentfile:1.0.1"
implementation(name: 'libuvccamera-release', ext: 'aar') {
exclude module: 'support-v4'

Binary file not shown.

@ -9,13 +9,13 @@
android:required="false" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"

@ -0,0 +1,13 @@
package com.yinuo.safetywatcher
import android.app.Application
import com.lztek.toolkit.Lztek
import com.yinuo.safetywatcher.utils.LztekUtil
class App : Application() {
override fun onCreate() {
super.onCreate()
LztekUtil.setObject(Lztek.create(this))
}
}

@ -7,4 +7,5 @@ package com.yinuo.safetywatcher.navi
sealed class ModelPath(val route: String) {
object Home : ModelPath("home")
object Setting : ModelPath("setting")
object Cloud : ModelPath("cloud")
}

@ -10,6 +10,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import com.yinuo.safetywatcher.ui.cloud.CloudView
import com.yinuo.safetywatcher.ui.home.HomeView
import com.yinuo.safetywatcher.ui.setting.SettingView
@ -27,5 +28,8 @@ fun NavigationView() {
composable(ModelPath.Setting.route) {
SettingView(Modifier.fillMaxSize())
}
composable(ModelPath.Cloud.route){
CloudView()
}
}
}

@ -0,0 +1,32 @@
package com.yinuo.safetywatcher.ui.cloud
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.viewmodel.compose.viewModel
@Composable
fun CloudView() {
// rememberLauncherForActivityResult(contract = ActivityResultContracts.OpenDocumentTree(), onResult ={} )
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
val context = LocalContext.current;
val viewModel: CloudViewModel = viewModel()
Button(onClick = { viewModel.exportExcel(context) }) {
Text(text = "导出数据")
}
}
}

@ -0,0 +1,28 @@
package com.yinuo.safetywatcher.ui.cloud
import android.content.Context
import androidx.lifecycle.ViewModel
import com.yinuo.safetywatcher.xls.SimpleCellValue
import com.yinuo.safetywatcher.xls.utils.ExcelUtils
class CloudViewModel : ViewModel() {
fun exportExcel(context: Context){
val allData: MutableList<List<SimpleCellValue>> = ArrayList()
val row1: MutableList<SimpleCellValue> = ArrayList()
row1.add(SimpleCellValue("5.22"))
row1.add(SimpleCellValue("1"))
row1.add(SimpleCellValue("2"))
row1.add(SimpleCellValue("3"))
val row2: MutableList<SimpleCellValue> = ArrayList()
row2.add(SimpleCellValue("5.23"))
row2.add(SimpleCellValue("4"))
row2.add(SimpleCellValue("5"))
row2.add(SimpleCellValue("6"))
allData.add(row1)
allData.add(row2)
ExcelUtils.writeStringListToExcel(allData, context)
}
}

@ -10,7 +10,6 @@ import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
@ -18,7 +17,6 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.viewmodel.compose.viewModel
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.rememberMultiplePermissionsState
import com.yinuo.safetywatcher.navi.ModelPath
import com.yinuo.safetywatcher.navi.NavigationUtil
@ -26,14 +24,13 @@ import com.yinuo.safetywatcher.navi.NavigationUtil
@Composable
fun HomeView() {
// 权限
val permissionsState = rememberMultiplePermissionsState(
permissions = listOf(
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.CAMERA,
)
)
if (permissionsState.allPermissionsGranted) {
// val permissionsState = rememberMultiplePermissionsState(
// permissions = listOf(
// android.Manifest.permission.MANAGE_EXTERNAL_STORAGE,
// android.Manifest.permission.CAMERA,
// )
// )
// if (permissionsState.allPermissionsGranted) {
val viewModel: HomeViewModel = viewModel()
val context = LocalContext.current
Row(modifier = Modifier.fillMaxSize()) {
@ -54,11 +51,14 @@ fun HomeView() {
Button(onClick = { NavigationUtil.to(ModelPath.Setting) }) {
Text(text = "设置")
}
Button(onClick = { NavigationUtil.to(ModelPath.Cloud) }) {
Text(text = "云平台")
}
}
} else {
LaunchedEffect(Unit) {
permissionsState.launchMultiplePermissionRequest()
}
}
// } else {
// LaunchedEffect(Unit) {
// permissionsState.launchMultiplePermissionRequest()
// }
// }
}

@ -0,0 +1,16 @@
package com.yinuo.safetywatcher.utils
import com.lztek.toolkit.Lztek
object LztekUtil {
private var mLztek: Lztek? = null;
fun setObject(value: Lztek) {
mLztek = value
}
fun getLztek(): Lztek {
return mLztek!!
}
}

@ -0,0 +1,5 @@
package com.yinuo.safetywatcher.xls
interface ICellValue {
fun getValue(): String
}

@ -0,0 +1,7 @@
package com.yinuo.safetywatcher.xls
data class SimpleCellValue(val content: String) : ICellValue {
override fun getValue(): String {
return content
}
}

@ -0,0 +1,138 @@
package com.yinuo.safetywatcher.xls.utils
import android.content.Context
import android.util.Log
import com.yinuo.safetywatcher.xls.ICellValue
import com.yinuo.safetywatcher.R
import jxl.Workbook
import jxl.WorkbookSettings
import jxl.write.Label
import jxl.write.WritableCellFormat
import jxl.write.WritableFont
import jxl.write.WritableWorkbook
import jxl.write.WriteException
import java.io.File
import java.io.FileInputStream
import java.io.InputStream
/**
* excel表格工具
*/
object ExcelUtils {
private const val TAG: String = "ExcelUtils"
private const val UTF8_ENCODING = "UTF-8"
private var arial12format: WritableCellFormat? = null
private fun format() {
try {
val writableFont = WritableFont(WritableFont.ARIAL, 12)
arial12format = WritableCellFormat(writableFont)
arial12format?.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN)
} catch (exception: WriteException) {
Log.e(TAG, "format() e==" + exception.message)
}
}
private fun makeDir(filePath: File) {
if (!filePath.parentFile.exists()) {
makeDir(filePath.parentFile)
}
filePath.mkdir()
}
private fun deleteByPath(filePath: String) {
val file = File(filePath)
if (file.exists()) {
if (file.isFile || file.isDirectory) {
file.delete()
}
}
}
private fun deleteExistFile(excelFilePath: String) {
val file = File(excelFilePath)
if (file.exists()) {
val files: Array<File> = file.listFiles()
for (i in files.indices) {
deleteByPath(files[i].absolutePath)
}
}
}
private fun initExcel(
allColumNames: Array<String>,
filePath: String,
sheetName: String
) {
format()
var workbook: WritableWorkbook? = null
try {
val file = File(filePath)
if (!file.exists()) {
file.createNewFile()
}
workbook = Workbook.createWorkbook(file)
val sheet = workbook.createSheet(sheetName, 0)
allColumNames.forEachIndexed { index, s ->
sheet.addCell(Label(index, 0, s, arial12format))
}
workbook.write()
} catch (e: Exception) {
Log.e(TAG, "initExcel e==" + e.message)
} finally {
try {
workbook?.close()
} catch (e: Exception) {
Log.e(TAG, "initExcel e==" + e.message)
}
}
}
fun writeStringListToExcel(
allRowsData: List<List<ICellValue>>,
context: Context
): Boolean {
val fileName = PathUtils.getNowTimeFormat(PathUtils.DATE_TO_STRING_LONG_PATTERN) + ".xls"
PathUtils.EXCEL_EXPORT_NAME = fileName
val filePath =
PathUtils.getUDiskPath() + PathUtils.EXCEL_EXPORT_PATH + PathUtils.EXCEL_EXPORT_NAME
deleteExistFile(PathUtils.getUDiskPath() + PathUtils.EXCEL_EXPORT_PATH)
makeDir(File(PathUtils.getUDiskPath() + PathUtils.EXCEL_EXPORT_PATH))
val columns = context.resources.getStringArray(R.array.excel_column)
initExcel(columns, filePath, PathUtils.SHEET_NAME) //需要写入权限
if (PathUtils.isListEmpty(allRowsData) || context == null)
return false
var writebook: WritableWorkbook? = null
var inputStream: InputStream? = null
try {
val setEncode = WorkbookSettings()
setEncode.encoding = UTF8_ENCODING
inputStream = FileInputStream(File(filePath))
val workbook = Workbook.getWorkbook(inputStream)
val file = File(filePath)
writebook = Workbook.createWorkbook(file, workbook)
val sheet = writebook.getSheet(0)
for ((row, item) in allRowsData.withIndex()) {
item.forEachIndexed { index, cellValue ->
sheet.addCell(Label(index, row + 1, cellValue.getValue(), arial12format))
}
}
writebook.write()
Log.i(TAG, "Excelel 写入成功")
return true
} catch (e: Exception) {
Log.e(TAG, "writeStringListToExcel() e==" + e.message)
} finally {
writebook?.close()
inputStream?.close()
}
return false
}
}

@ -0,0 +1,52 @@
package com.yinuo.safetywatcher.xls.utils
import android.content.Context
import android.os.Environment
import com.yinuo.safetywatcher.utils.LztekUtil
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
/**
* 基础工具类
*/
object PathUtils {
private const val TAG: String = "BaseUtils"
const val SHEET_NAME = "表格1"
const val EXCEL_EXPORT_PATH = "/ExportExcel/"
lateinit var EXCEL_EXPORT_NAME: String
const val DATE_TO_STRING_LONG_PATTERN: String = "yyyy_MM_dd_HH_mm_ss"
fun <T> isListEmpty(list: List<T>?): Boolean {
return list == null || list.isEmpty()
}
/**
* 获取应用中文件存储
*/
fun getExternalStoragePath(context: Context): String? {
return context.getExternalFilesDir(null)?.path
}
/**
* 获取U盘存储
*/
fun getUDiskPath(): String {
return LztekUtil.getLztek().usbStoragePath
}
fun getExternalStorageDirectory(context: Context): String? {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).path
}
/**
* 外部存储目录
*/
fun getExternalStorageDirectory(): String? {
return Environment.getExternalStorageDirectory().absolutePath
}
fun getNowTimeFormat(dateToStringLongPattern: String): String {
return SimpleDateFormat(dateToStringLongPattern, Locale.ROOT).format(Date());
}
}

@ -1,3 +1,11 @@
<resources>
<string name="app_name">app-compose</string>
<string-array name="excel_column">
<item>时间</item>
<item>属性</item>
<item></item>
<item>单位</item>
</string-array>
</resources>
Loading…
Cancel
Save