desc:compose excel相关
parent
e0feb94d2a
commit
e28d3afb95
Binary file not shown.
@ -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))
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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…
Reference in New Issue