From 4c7e2d8f8d4a93f318ada0e728dbc370e7504e92 Mon Sep 17 00:00:00 2001 From: feiyu02 <risaku@163.com> Date: 星期四, 14 七月 2022 17:49:40 +0800 Subject: [PATCH] 1. 自动评分优化; 2. 巡查汇总报告功能编写中 --- src/main/kotlin/cn/flightfeather/supervision/common/utils/ExcelUtil.kt | 121 +++++++--------------------------------- 1 files changed, 21 insertions(+), 100 deletions(-) diff --git a/src/main/kotlin/cn/flightfeather/supervision/common/utils/ExcelUtil.kt b/src/main/kotlin/cn/flightfeather/supervision/common/utils/ExcelUtil.kt index e0cb1f2..8bb0aeb 100644 --- a/src/main/kotlin/cn/flightfeather/supervision/common/utils/ExcelUtil.kt +++ b/src/main/kotlin/cn/flightfeather/supervision/common/utils/ExcelUtil.kt @@ -24,19 +24,20 @@ /** * 鑷姩澶勭悊琛屽悎骞舵暟鎹� */ - fun write(heads: List<Array<String>>, contents: List<Array<Any>>, workbook: HSSFWorkbook, sheetName: String = "sheet1") { + fun write(heads: List<Array<Any>>, contents: MutableList<Array<Any>>, workbook: HSSFWorkbook, sheetName: String = "sheet1") { val sheet = workbook.createSheet(sheetName) var rowIndex = 0 - heads.forEach { - val rows = sheet.createRow(rowIndex) - for (i in it.indices) { - rows.createCell(i).setCellValue(it[i]) - } - rowIndex++ - } +// heads.forEach { +// val rows = sheet.createRow(rowIndex) +// for (i in it.indices) { +// rows.createCell(i).setCellValue(it[i]) +// } +// rowIndex++ +// } + contents.addAll(0, heads) contents.forEach { val maxRow = getMaxRows(it) @@ -100,9 +101,12 @@ println("write1-1: ${c.text};($rowIndex, ${col})") } is String -> { - rows.createCell(col).setCellValue(c) - println("write1-2: ${c};($rowIndex, ${col})") + if (c.isNotBlank()) { + rows.createCell(col).setCellValue(c) + println("write1-2: ${c};($rowIndex, ${col})") + } } + is Int -> rows.createCell(col).setCellValue(c.toDouble()) is Double -> rows.createCell(col).setCellValue(c) is Boolean -> rows.createCell(col).setCellValue(c) is Date -> rows.createCell(col).setCellValue(c) @@ -150,9 +154,12 @@ println("write2-1: ${c.text};($_rowIndex, ${map.key})") } is String -> { - rows.createCell(map.key).setCellValue(c) - println("write2-2: ${c};($_rowIndex, ${map.key})") + if (c.isNotBlank()) { + rows.createCell(map.key).setCellValue(c) + println("write2-2: ${c};($_rowIndex, ${map.key})") + } } + is Int -> rows.createCell(map.key).setCellValue(c.toDouble()) is Double -> rows.createCell(map.key).setCellValue(c) is Boolean -> rows.createCell(map.key).setCellValue(c) is Date -> rows.createCell(map.key).setCellValue(c) @@ -174,102 +181,16 @@ /** * 鑷姩澶勭悊琛屽悎骞舵暟鎹� */ - fun write2(out: OutputStream, heads: List<String>, contents: List<Array<Any>>): String { + fun write2(out: OutputStream, heads: List<Array<Any>>, contents: MutableList<Array<Any>>) { val workbook = HSSFWorkbook() - val sheet = workbook.createSheet("sheet1") + write(heads, contents, workbook) - var rowIndex = 0 - - if (heads.isNotEmpty()) { - val rows = sheet.createRow(rowIndex) - for (i in 0 until heads.size) { - rows.createCell(i).setCellValue(heads[i]) - } - rowIndex++ - } - - contents.forEach { - val maxRow = getMaxRows(it) - - var rows = sheet.createRow(rowIndex) - - val arrayMap = mutableMapOf<Int, Array<*>>() - - for (i in it.indices) { - val cell = it[i] - - var rowspan = maxRow//鍚堝苟鐨勮鐨勮法搴� - - val c = - if (cell is Array<*>) { - //褰撴暟鎹负鏁扮粍鏃讹紝闇�瑕佹牴鎹渶澶ц鏁伴噸鏂拌绠楄鍗曞厓鏍肩殑琛岃法搴� - arrayMap[i] = cell - rowspan = maxRow / if(cell.size==0) 1 else cell.size - if (rowspan > 1) { - sheet.addMergedRegion(CellRangeAddress(rowIndex, rowIndex + rowspan - 1, i, i)) - } - if (cell.isEmpty()) { - "" - } else { - cell[0] - } - } else { - //褰撴暟鎹笉鏄暟缁勬椂锛岄渶瑕佹寜鏈�澶ц鏁板悎骞跺崟鍏冩牸 - if (rowspan > 1) { - sheet.addMergedRegion(CellRangeAddress(rowIndex, rowIndex + rowspan - 1, i, i)) - } - cell - } - when (c) { - is String -> rows.createCell(i).setCellValue(c) - is Double -> rows.createCell(i).setCellValue(c) - is Boolean -> rows.createCell(i).setCellValue(c) - is Date -> rows.createCell(i).setCellValue(c) - is Calendar -> rows.createCell(i).setCellValue(c) - is LocalDate -> rows.createCell(i).setCellValue(c) - } - } - - for (i in 1 until maxRow) { - rowIndex++ - rows = sheet.createRow(rowIndex) - arrayMap.forEach {map -> - val array = map.value - if (i < array.size) { - val rowspan = maxRow / array.size - if (rowspan > 1) { - sheet.addMergedRegion(CellRangeAddress(rowIndex, rowIndex + rowspan - 1, i, i)) - } - val c = array[i] - when (c) { - is String -> rows.createCell(map.key).setCellValue(c) - is Double -> rows.createCell(map.key).setCellValue(c) - is Boolean -> rows.createCell(map.key).setCellValue(c) - is Date -> rows.createCell(map.key).setCellValue(c) - is Calendar -> rows.createCell(map.key).setCellValue(c) - is LocalDate -> rows.createCell(map.key).setCellValue(c) - } - } - } - } - - rowIndex++ - } - - val fileName = "${DateUtil().DateToString(Date(), "yyyy-MM-dd hh:mm:ss")}.xls" - val filePath = "${Constant.DEFAULT_FILE_PATH}/files/$fileName" - val xlsFile = File(filePath) - if (!xlsFile.parentFile.exists()) { - xlsFile.parentFile.mkdirs() - } -// val xlsStream = FileOutputStream(xlsFile) workbook.write(out) workbook.close() out.flush() out.close() - return fileName } private fun getMaxRows(rowArray: Array<Any>): Int { -- Gitblit v1.9.3