From a9078932aa7ce76bf5f2e326aabc0a480267adeb Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 20 May 2021 16:57:59 +0800 Subject: [PATCH] =?UTF-8?q?0.12.1=20=E8=B0=83=E6=95=B4excel=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=9B=E8=B0=83=E6=95=B4=E5=88=87=E9=9D=A2=E5=92=8C?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/synebula/gaea/app/component/Logger.kt | 40 +++--- .../gaea/app/component/aop/AppAspect.kt | 11 -- .../synebula/gaea/app/component/poi/Excel.kt | 118 ++++++++++++++---- 3 files changed, 116 insertions(+), 53 deletions(-) diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/Logger.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/Logger.kt index 24b042d..ec4150d 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/Logger.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/Logger.kt @@ -48,7 +48,7 @@ class Logger : ILogger { override fun trace(format: String, vararg args: Any) { if (this.logger.isTraceEnabled) { - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) this.logger.trace(message) } } @@ -56,7 +56,7 @@ class Logger : ILogger { override fun trace(t: Throwable, format: String, vararg args: Any) { if (this.logger.isTraceEnabled) { - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) this.logger.trace(message, t) } } @@ -64,7 +64,7 @@ class Logger : ILogger { override fun trace(obj: Any, format: String, vararg args: Any) { if (this.logger.isTraceEnabled) { val real = this.getLogger(obj) - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) real.trace(message) } } @@ -72,7 +72,7 @@ class Logger : ILogger { override fun trace(obj: Any, t: Throwable?, format: String, vararg args: Any) { if (this.logger.isTraceEnabled) { val real = this.getLogger(obj) - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) real.trace(message, t) } } @@ -95,7 +95,7 @@ class Logger : ILogger { override fun debug(format: String, vararg args: Any) { if (this.logger.isDebugEnabled) { - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) this.logger.debug(message) } } @@ -103,7 +103,7 @@ class Logger : ILogger { override fun debug(t: Throwable, format: String, vararg args: Any) { if (this.logger.isDebugEnabled) { - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) this.logger.debug(message, t) } } @@ -111,7 +111,7 @@ class Logger : ILogger { override fun debug(obj: Any, format: String, vararg args: Any) { if (this.logger.isDebugEnabled) { val real = this.getLogger(obj) - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) real.debug(message) } } @@ -119,7 +119,7 @@ class Logger : ILogger { override fun debug(obj: Any, t: Throwable?, format: String, vararg args: Any) { if (this.logger.isDebugEnabled) { val real = this.getLogger(obj) - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) real.debug(message, t) } } @@ -140,7 +140,7 @@ class Logger : ILogger { override fun info(format: String, vararg args: Any) { if (this.logger.isInfoEnabled) { - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) this.logger.info(message) } } @@ -148,7 +148,7 @@ class Logger : ILogger { override fun info(t: Throwable, format: String, vararg args: Any) { if (this.logger.isInfoEnabled) { - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) this.logger.info(message, t) } } @@ -156,7 +156,7 @@ class Logger : ILogger { override fun info(obj: Any, format: String, vararg args: Any) { if (this.logger.isInfoEnabled) { val real = this.getLogger(obj) - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) real.info(message) } } @@ -164,7 +164,7 @@ class Logger : ILogger { override fun info(obj: Any, t: Throwable?, format: String, vararg args: Any) { if (this.logger.isInfoEnabled) { val real = this.getLogger(obj) - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) real.info(message, t) } } @@ -185,7 +185,7 @@ class Logger : ILogger { override fun warn(format: String, vararg args: Any) { if (this.logger.isWarnEnabled) { - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) this.logger.warn(message) } } @@ -193,7 +193,7 @@ class Logger : ILogger { override fun warn(t: Throwable, format: String, vararg args: Any) { if (this.logger.isWarnEnabled) { - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) this.logger.warn(message, t) } } @@ -201,7 +201,7 @@ class Logger : ILogger { override fun warn(obj: Any, format: String, vararg args: Any) { if (this.logger.isWarnEnabled) { val real = this.getLogger(obj) - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) real.warn(message) } } @@ -209,7 +209,7 @@ class Logger : ILogger { override fun warn(obj: Any, t: Throwable?, format: String, vararg args: Any) { if (this.logger.isWarnEnabled) { val real = this.getLogger(obj) - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) real.warn(message, t) } } @@ -232,7 +232,7 @@ class Logger : ILogger { override fun error(format: String, vararg args: Any) { if (this.logger.isErrorEnabled) { - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) this.logger.error(message) } } @@ -240,7 +240,7 @@ class Logger : ILogger { override fun error(t: Throwable, format: String, vararg args: Any) { if (this.logger.isErrorEnabled) { - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) this.logger.error(message, t) } } @@ -248,7 +248,7 @@ class Logger : ILogger { override fun error(obj: Any, format: String, vararg args: Any) { if (this.logger.isErrorEnabled) { val real = this.getLogger(obj) - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) real.error(message) } } @@ -256,7 +256,7 @@ class Logger : ILogger { override fun error(obj: Any, t: Throwable?, format: String, vararg args: Any) { if (this.logger.isErrorEnabled) { val real = this.getLogger(obj) - val message = String.format(format, *args) + val message = if (args.isEmpty()) format else String.format(format, *args) real.error(message, t) } } diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/aop/AppAspect.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/aop/AppAspect.kt index bfc1329..b0272db 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/aop/AppAspect.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/aop/AppAspect.kt @@ -34,17 +34,6 @@ abstract class AppAspect { */ abstract fun func() - /** - * 后置异常通知 - */ - @AfterThrowing("func()", throwing = "ex") - fun throws(point: JoinPoint, ex: Throwable) { - val clazz = point.signature.declaringType - logger.error( - ex, - "${clazz.name}.${point.signature.name} exception:${ex.message}, args:${gson.toJson(point.args)}" - ) - } /** * 环绕通知,环绕增强,相当于MethodInterceptor diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/poi/Excel.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/poi/Excel.kt index 1ebf5a4..f22970d 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/poi/Excel.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/poi/Excel.kt @@ -4,15 +4,16 @@ import com.synebula.gaea.app.struct.ExcelData import org.apache.poi.hpsf.Decimal import org.apache.poi.hssf.usermodel.HSSFCell import org.apache.poi.hssf.usermodel.HSSFCellStyle +import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator import org.apache.poi.hssf.usermodel.HSSFWorkbook -import org.apache.poi.ss.usermodel.BorderStyle -import org.apache.poi.ss.usermodel.HorizontalAlignment -import org.apache.poi.ss.usermodel.Sheet -import org.apache.poi.ss.usermodel.VerticalAlignment +import org.apache.poi.ss.formula.BaseFormulaEvaluator +import org.apache.poi.ss.usermodel.* +import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator import org.apache.poi.xssf.usermodel.XSSFWorkbook import org.springframework.web.multipart.MultipartFile import java.util.* + /** * Excel操作对象 */ @@ -88,16 +89,16 @@ object Excel { * * @param file 上传文件流 * @param columns 文件列名称、类型定义 - * @param startRow 数据起始行,默认0 - * @param startColumn 数据起始列,默认0 + * @param rowStart 数据起始行,默认0 + * @param columnStart 数据起始列,默认0 * * @return ExcelData */ fun import( - file: MultipartFile, - columns: List>, - startRow: Int = 0, - startColumn: Int = 0 + file: MultipartFile, + columns: List>, + rowStart: Int = 0, + columnStart: Int = 0 ): List> { if (file.originalFilename?.endsWith(".xls") != true && file.originalFilename?.endsWith(".xlsx") != true) throw RuntimeException("无法识别的文件格式[${file.originalFilename}]") @@ -109,34 +110,107 @@ object Excel { val sheet = workbook.getSheetAt(0) val data = mutableListOf>() - for (i in startRow..sheet.lastRowNum) { + for (i in rowStart..sheet.lastRowNum) { val row = sheet.getRow(i) ?: continue val rowData = mutableMapOf() - for (c in startColumn until columns.size + startColumn) { + for (r in columnStart until columns.size + columnStart) { try { - val column = columns[c] + val column = columns[r] val value: Any = when (column.second) { Int::class.java.name, Double::class.java.name, Float::class.java.name, Decimal::class.java.name -> try { - row.getCell(c).numericCellValue + row.getCell(r).numericCellValue } catch (ignored: Exception) { - row.getCell(c).stringCellValue + row.getCell(r).stringCellValue } Boolean::class.java.name -> try { - row.getCell(c).booleanCellValue + row.getCell(r).booleanCellValue } catch (ignored: Exception) { - row.getCell(c).stringCellValue + row.getCell(r).stringCellValue } Date::class.java.name -> try { - row.getCell(c).dateCellValue + row.getCell(r).dateCellValue } catch (ignored: Exception) { - row.getCell(c).stringCellValue + row.getCell(r).stringCellValue } - else -> row.getCell(c).stringCellValue + else -> row.getCell(r).stringCellValue } - rowData.put(columns[c].first, value) + rowData.put(columns[r].first, value) } catch (ex: Exception) { - throw RuntimeException("解析EXCEL文件${file.originalFilename}第${i}行第${c}列出错", ex) + throw RuntimeException("解析EXCEL文件${file.originalFilename}第${r + 1}行第${r + 1}列出错", ex) + } + } + data.add(rowData) + } + workbook.close() + file.inputStream.close() + return data + } + + /** + * 导入文件 + * + * @param file 上传文件流 + * @param rowStartIndex 数据起始行,默认0 + * @param columnStartIndex 数据起始列,默认0 + * + * @return ExcelData + */ + fun import( + file: MultipartFile, + columnSize: Int = 0, + rowStartIndex: Int = 0, + columnStartIndex: Int = 0 + ): List> { + if (file.originalFilename?.endsWith(".xls") != true && file.originalFilename?.endsWith(".xlsx") != true) + throw RuntimeException("无法识别的文件格式[${file.originalFilename}]") + val eva: BaseFormulaEvaluator + val workbook = if (file.originalFilename?.endsWith(".xls") == true) { + val wb = HSSFWorkbook(file.inputStream) + eva = HSSFFormulaEvaluator(wb) + wb + } else { + val wb = XSSFWorkbook(file.inputStream) + eva = XSSFFormulaEvaluator(wb) + wb + } + val sheet = workbook.getSheetAt(0) + + val titles = mutableListOf() + val titleRow = sheet.getRow(rowStartIndex) + val size = if (columnSize != 0) columnSize else titleRow.physicalNumberOfCells //列数 + for (i in columnStartIndex until size) { + titles.add(titleRow.getCell(i).stringCellValue) + } + + val data = mutableListOf>() + for (r in (rowStartIndex + 1)..sheet.lastRowNum) { + val row = sheet.getRow(r) ?: continue + val rowData = mutableMapOf() + for (c in columnStartIndex until size + columnStartIndex) { + try { + val title = titles[c] + val cell = row.getCell(c) + val value = when (cell.cellType) { + CellType.BOOLEAN -> cell.booleanCellValue.toString() + CellType.ERROR -> cell.errorCellValue.toString() + CellType.NUMERIC -> { + val numericCellValue: Double = cell.numericCellValue + if (DateUtil.isCellDateFormatted(cell)) { + DateUtil.getLocalDateTime(numericCellValue).toString() + } else { + numericCellValue.toString() + } + } + CellType.STRING -> cell.richStringCellValue.string + CellType.BLANK -> "" + CellType.FORMULA -> eva.evaluate(cell).toString() + else -> throw Exception("匹配类型错误") + } + + rowData[title] = value + } catch (ex: Exception) { + throw RuntimeException("解析EXCEL文件${file.originalFilename}第${r + 1}行第${c + 1}列出错", ex) } } data.add(rowData)