|
|
|
@ -1,409 +0,0 @@
|
|
|
|
|
package com.yinuo.safetywatcher.utils
|
|
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.text.TextUtils
|
|
|
|
|
import android.util.Log
|
|
|
|
|
import com.yinuo.safetywatcher.xls.Constants
|
|
|
|
|
import com.yinuo.safetywatcher.xls.Constants.COLUMN_NAME_MAP
|
|
|
|
|
import com.yinuo.safetywatcher.xls.Demo
|
|
|
|
|
import com.yinuo.safetywatcher.xls.ExcelLabelBean
|
|
|
|
|
import jxl.Workbook
|
|
|
|
|
import jxl.WorkbookSettings
|
|
|
|
|
import jxl.write.Label
|
|
|
|
|
import jxl.write.WritableCellFormat
|
|
|
|
|
import jxl.write.WritableFont
|
|
|
|
|
import jxl.write.WritableImage
|
|
|
|
|
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 var ImageSize: Int = 0
|
|
|
|
|
private val ImageSizeList = arrayListOf<Int>()
|
|
|
|
|
|
|
|
|
|
private fun initExcel(
|
|
|
|
|
allRowsData: List<List<ExcelLabelBean>>,
|
|
|
|
|
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)
|
|
|
|
|
sheet.addCell(Label(0, 0, filePath, arial12format))
|
|
|
|
|
ImageSize = 0
|
|
|
|
|
ImageSizeList.clear()
|
|
|
|
|
for ((index, value) in allRowsData.withIndex()) {
|
|
|
|
|
ImageSizeList.add(
|
|
|
|
|
(allRowsData.get(index).get(8).excelLableContent.length - allRowsData.get(index)
|
|
|
|
|
.get(8).excelLableContent.replace("|", "").length) + 1
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
for (i in 0 until ImageSizeList.size) {
|
|
|
|
|
if (ImageSize < ImageSizeList.get(i)) {
|
|
|
|
|
ImageSize = ImageSizeList.get(i)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
val columnNameMap = COLUMN_NAME_MAP
|
|
|
|
|
for (number in 0..ImageSize + 10) {
|
|
|
|
|
if (number <= 8) {
|
|
|
|
|
for ((index, value) in columnNameMap.withIndex()) {
|
|
|
|
|
if (index <= 8) {
|
|
|
|
|
sheet.addCell(Label(index, 0, value.excelName, arial12format))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (number > 8 && number <= ImageSize + 7) {
|
|
|
|
|
sheet.addCell(Label(number, 0, "", arial12format))
|
|
|
|
|
} else if (number == ImageSize + 8) {
|
|
|
|
|
for ((index, value) in columnNameMap.withIndex()) {
|
|
|
|
|
if (index > 8) {
|
|
|
|
|
sheet.addCell(
|
|
|
|
|
Label(
|
|
|
|
|
index + ImageSize - 1,
|
|
|
|
|
0,
|
|
|
|
|
value.excelName,
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fun importSheet(context: Context): ArrayList<CheckListTemplateBean> {
|
|
|
|
|
// val inputStream: InputStream =
|
|
|
|
|
// context.resources.assets.open(Constants.ASSETS_PRESET_EXCEL_NAME)
|
|
|
|
|
// val book: Workbook = Workbook.getWorkbook(inputStream)
|
|
|
|
|
// try {
|
|
|
|
|
//
|
|
|
|
|
// val sheet = book.getSheet(0)
|
|
|
|
|
// val checkListTemplateBeans = ArrayList<CheckListTemplateBean>()
|
|
|
|
|
// for (i in 1 until sheet.rows) {
|
|
|
|
|
// val checkListTopic = CheckListTopic()
|
|
|
|
|
// //导入的Excel中第1列要是。检查主题‘ 这一项
|
|
|
|
|
// checkListTopic.topicName = sheet.getCell(0, i).contents
|
|
|
|
|
//
|
|
|
|
|
// val checkObj = CheckObjType()
|
|
|
|
|
// //导入的Excel中第2列要是’检查对象‘ 这一项
|
|
|
|
|
// checkObj.checkObj = sheet.getCell(1, i).contents
|
|
|
|
|
//
|
|
|
|
|
// val checkListTemplate = CheckListTemplate()
|
|
|
|
|
// //导入的Excel中第3列要是’检查项目‘ 这一项
|
|
|
|
|
// checkListTemplate.inspectionItems = sheet.getCell(2, i).contents
|
|
|
|
|
//
|
|
|
|
|
// val checkContent = CheckContent()
|
|
|
|
|
// //导入的Excel中第4列要是’检查内容‘ 这一项
|
|
|
|
|
// checkContent.inspectionContent = sheet.getCell(3, i).contents
|
|
|
|
|
// //导入的Excel中第5列要是’检查标准‘ 这一项
|
|
|
|
|
// checkContent.inspectionRule = sheet.getCell(4, i).contents
|
|
|
|
|
// //导入的Excel中第6列要是’检查方法‘ 这一项
|
|
|
|
|
// checkContent.inspectionFun = sheet.getCell(5, i).contents
|
|
|
|
|
// //导入的Excel中第7列要是’检查依据‘ 这一项
|
|
|
|
|
// checkContent.inspectionBasis = sheet.getCell(6, i).contents
|
|
|
|
|
//
|
|
|
|
|
// val checkListTemplateBean =
|
|
|
|
|
// CheckListTemplateBean(checkListTopic, checkObj, checkListTemplate, checkContent)
|
|
|
|
|
// checkListTemplateBeans.add(checkListTemplateBean)
|
|
|
|
|
// }
|
|
|
|
|
// book.close();
|
|
|
|
|
// return checkListTemplateBeans
|
|
|
|
|
// } catch (e: Exception) {
|
|
|
|
|
// Log.i(TAG, "importSheet e=" + e.message)
|
|
|
|
|
// } finally {
|
|
|
|
|
// book.close()
|
|
|
|
|
// inputStream.close()
|
|
|
|
|
// }
|
|
|
|
|
// return ArrayList()
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取一个对象的所有属性和值,包括其二级对象的属性和值
|
|
|
|
|
*/
|
|
|
|
|
private fun readAllAttrs(any: Any): HashMap<String, Any> {
|
|
|
|
|
val hashMap: HashMap<String, Any> = HashMap()
|
|
|
|
|
try {
|
|
|
|
|
val javaClass = any.javaClass
|
|
|
|
|
val declaredFields = javaClass.declaredFields
|
|
|
|
|
for ((i, field) in declaredFields.iterator().withIndex()) {
|
|
|
|
|
field.isAccessible = true
|
|
|
|
|
val name = field.name
|
|
|
|
|
val valueAny = field[any]
|
|
|
|
|
hashMap[name] = valueAny
|
|
|
|
|
|
|
|
|
|
//获取子对象的属性和值
|
|
|
|
|
if (valueAny != null) {
|
|
|
|
|
val valueClass = valueAny.javaClass
|
|
|
|
|
val valueClassFields = valueClass.declaredFields
|
|
|
|
|
for (valueClassField in valueClassFields.iterator()) {
|
|
|
|
|
valueClassField.isAccessible = true
|
|
|
|
|
val valueClassFieldName = valueClassField.name
|
|
|
|
|
val valueClassFieldValue = valueClassField[valueAny]
|
|
|
|
|
hashMap[valueClassFieldName] = valueClassFieldValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
Log.e(TAG, "readAllAttrs() e==" + e.message)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return hashMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun writeStringListToExcel(
|
|
|
|
|
allRowsData: List<List<ExcelLabelBean>>,
|
|
|
|
|
context: Context
|
|
|
|
|
): Boolean {
|
|
|
|
|
val fileName = PathUtils.getNowTimeFormat(PathUtils.DATE_TO_STRING_LONG_PATTERN) + ".xls"
|
|
|
|
|
Constants.EXCEL_EXPORT_NAME = fileName
|
|
|
|
|
val filePath =
|
|
|
|
|
PathUtils.getExternalStoragePath(context) + Constants.EXCEL_EXPORT_PATH + Constants.EXCEL_EXPORT_NAME
|
|
|
|
|
deleteExistFile(PathUtils.getExternalStoragePath(context) + Constants.EXCEL_EXPORT_PATH)
|
|
|
|
|
makeDir(File(PathUtils.getExternalStoragePath(context) + "/" + Constants.SAVE_EXPORT_EXCEL_PATH))
|
|
|
|
|
|
|
|
|
|
initExcel(allRowsData, filePath, Constants.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 (number in 0..ImageSize+10) {
|
|
|
|
|
for ((row, item) in allRowsData.withIndex()) {
|
|
|
|
|
for ((colum, value) in item.withIndex()) {
|
|
|
|
|
if (value.isImage && !TextUtils.isEmpty(value.excelLableContent)) {
|
|
|
|
|
//这里value.excelLableContent 是图片在手机上的绝对地址,多个图片用|隔开
|
|
|
|
|
if (colum == 8) {
|
|
|
|
|
val split = value.excelLableContent.split("|")
|
|
|
|
|
var tempColum = colum
|
|
|
|
|
for (imagePath in split) {
|
|
|
|
|
val imgFile = File(imagePath)
|
|
|
|
|
if (imgFile.exists()) {
|
|
|
|
|
val image = WritableImage(
|
|
|
|
|
tempColum.toDouble(),
|
|
|
|
|
(row + 1).toDouble(),
|
|
|
|
|
1.0,
|
|
|
|
|
1.0,
|
|
|
|
|
imgFile
|
|
|
|
|
)
|
|
|
|
|
//设置行高
|
|
|
|
|
sheet.setRowView(row + 1, 1700, false)
|
|
|
|
|
sheet.addImage(image)
|
|
|
|
|
tempColum++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (colum == 11) {
|
|
|
|
|
val split = value.excelLableContent.split("|")
|
|
|
|
|
var tempColum = colum + ImageSize - 1
|
|
|
|
|
for (imagePath in split) {
|
|
|
|
|
val imgFile = File(imagePath)
|
|
|
|
|
if (imgFile.exists()) {
|
|
|
|
|
val image = WritableImage(
|
|
|
|
|
tempColum.toDouble(),
|
|
|
|
|
(row + 1).toDouble(),
|
|
|
|
|
1.0,
|
|
|
|
|
1.0,
|
|
|
|
|
imgFile
|
|
|
|
|
)
|
|
|
|
|
//设置行高
|
|
|
|
|
sheet.setRowView(row + 1, 1700, false)
|
|
|
|
|
sheet.addImage(image)
|
|
|
|
|
tempColum++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (colum <= 8) {
|
|
|
|
|
sheet.addCell(Label(colum, row + 1, value.excelLableContent, arial12format))
|
|
|
|
|
} else if (colum > 8) {
|
|
|
|
|
sheet.addCell(
|
|
|
|
|
Label(
|
|
|
|
|
colum + ImageSize - 1,
|
|
|
|
|
row + 1,
|
|
|
|
|
value.excelLableContent,
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fun transformExportData(records: MutableList<CheckListDetailItemBean>): MutableList<List<ExcelLabelBean>> {
|
|
|
|
|
// val allRowsData: MutableList<List<ExcelLabelBean>> = java.util.ArrayList()
|
|
|
|
|
//
|
|
|
|
|
// for (checkListDetailItemBean in records) {
|
|
|
|
|
// val rowData: MutableList<ExcelLabelBean> = ArrayList()
|
|
|
|
|
// val allAttrs = readAllAttrs(checkListDetailItemBean)
|
|
|
|
|
// for ((i, item) in COLUMN_NAME_MAP.withIndex()) {
|
|
|
|
|
// val obj = allAttrs[item.sqTabName]
|
|
|
|
|
// rowData.add(ExcelLabelBean(obj as String, item.isImage))
|
|
|
|
|
// }
|
|
|
|
|
// allRowsData.add(rowData)
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// return allRowsData
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
private fun initExcelDemo(
|
|
|
|
|
allColumNames: List<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 writeStringListToExcelDemo(
|
|
|
|
|
allRowsData: List<List<Demo>>,
|
|
|
|
|
context: Context
|
|
|
|
|
): Boolean {
|
|
|
|
|
val fileName = PathUtils.getNowTimeFormat(PathUtils.DATE_TO_STRING_LONG_PATTERN) + ".xls"
|
|
|
|
|
Constants.EXCEL_EXPORT_NAME = fileName
|
|
|
|
|
val filePath =
|
|
|
|
|
PathUtils.getExternalStoragePath(context) + Constants.EXCEL_EXPORT_PATH + Constants.EXCEL_EXPORT_NAME
|
|
|
|
|
deleteExistFile(PathUtils.getExternalStoragePath(context) + Constants.EXCEL_EXPORT_PATH)
|
|
|
|
|
makeDir(File(PathUtils.getExternalStoragePath(context) + "/" + Constants.SAVE_EXPORT_EXCEL_PATH))
|
|
|
|
|
|
|
|
|
|
val columNames = mutableListOf<String>()
|
|
|
|
|
columNames.add("时间")
|
|
|
|
|
columNames.add("columNames1")
|
|
|
|
|
columNames.add("columNames2")
|
|
|
|
|
columNames.add("columNames3")
|
|
|
|
|
initExcelDemo(columNames, filePath, Constants.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, demo ->
|
|
|
|
|
sheet.addCell(Label(index, row + 1, demo.content, 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
|
|
|
|
|
}
|
|
|
|
|
}
|