0.12.0 增加excel的导入方法; 修复aop记录文件异常的问题
This commit is contained in:
@@ -21,7 +21,7 @@ allprojects {
|
|||||||
|
|
||||||
subprojects {
|
subprojects {
|
||||||
ext {
|
ext {
|
||||||
version '0.11.0'
|
version '0.12.0'
|
||||||
spring_version = "2.3.0.RELEASE"
|
spring_version = "2.3.0.RELEASE"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ dependencies {
|
|||||||
compile("org.springframework.boot:spring-boot-starter-mail:$spring_version")
|
compile("org.springframework.boot:spring-boot-starter-mail:$spring_version")
|
||||||
compile("org.springframework.boot:spring-boot-starter-security:$spring_version")
|
compile("org.springframework.boot:spring-boot-starter-security:$spring_version")
|
||||||
compile group: 'net.sf.dozer', name: 'dozer', version: '5.5.1'
|
compile group: 'net.sf.dozer', name: 'dozer', version: '5.5.1'
|
||||||
compile group: 'org.apache.poi', name: 'poi', version: '4.1.2'
|
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '5.0.0'
|
||||||
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
|
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
|
||||||
compile group: 'com.google.guava', name: 'guava', version: '30.1.1-jre'
|
compile group: 'com.google.guava', name: 'guava', version: '30.1.1-jre'
|
||||||
compile group: 'com.auth0', name: 'java-jwt', version: '3.14.0'
|
compile group: 'com.auth0', name: 'java-jwt', version: '3.14.0'
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.synebula.gaea.app.component.aop
|
package com.synebula.gaea.app.component.aop
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
|
import com.google.gson.Gson
|
||||||
import com.synebula.gaea.app.IApplication
|
import com.synebula.gaea.app.IApplication
|
||||||
import com.synebula.gaea.app.struct.HttpMessage
|
import com.synebula.gaea.app.struct.HttpMessage
|
||||||
import com.synebula.gaea.app.component.aop.annotation.MethodName
|
import com.synebula.gaea.app.component.aop.annotation.MethodName
|
||||||
@@ -15,11 +16,12 @@ import org.aspectj.lang.annotation.Around
|
|||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
import org.springframework.context.ApplicationContext
|
import org.springframework.context.ApplicationContext
|
||||||
import org.springframework.core.DefaultParameterNameDiscoverer
|
import org.springframework.core.DefaultParameterNameDiscoverer
|
||||||
|
import org.springframework.web.multipart.MultipartFile
|
||||||
|
|
||||||
abstract class AppAspect {
|
abstract class AppAspect {
|
||||||
private var paramDiscover = DefaultParameterNameDiscoverer()
|
private var paramDiscover = DefaultParameterNameDiscoverer()
|
||||||
|
|
||||||
private val mapper = ObjectMapper()
|
private val gson = Gson()
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
lateinit var logger: ILogger
|
lateinit var logger: ILogger
|
||||||
@@ -39,8 +41,8 @@ abstract class AppAspect {
|
|||||||
fun throws(point: JoinPoint, ex: Throwable) {
|
fun throws(point: JoinPoint, ex: Throwable) {
|
||||||
val clazz = point.signature.declaringType
|
val clazz = point.signature.declaringType
|
||||||
logger.error(
|
logger.error(
|
||||||
ex,
|
ex,
|
||||||
"${clazz.name}.${point.signature.name} exception:${ex.message}, args:${mapper.writeValueAsString(point.args)}"
|
"${clazz.name}.${point.signature.name} exception:${ex.message}, args:${gson.toJson(point.args)}"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,13 +86,10 @@ abstract class AppAspect {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
val message = "$moduleName - $funcName 异常"
|
val message = "$moduleName - $funcName 异常"
|
||||||
logger.error(
|
logger.error(ex,
|
||||||
ex,
|
"$message。Method args ${
|
||||||
"$message。Method args ${paramDiscover.getParameterNames(func)?.contentToString()} values is ${
|
paramDiscover.getParameterNames(func)?.contentToString()} values is ${
|
||||||
mapper.writeValueAsString(
|
gson.toJson(point.args)}"
|
||||||
point.args
|
|
||||||
)
|
|
||||||
}"
|
|
||||||
)
|
)
|
||||||
return HttpMessage(Status.Error, message)
|
return HttpMessage(Status.Error, message)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.synebula.gaea.app.component.poi
|
package com.synebula.gaea.app.component.poi
|
||||||
|
|
||||||
import com.synebula.gaea.app.struct.ExcelData
|
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.HSSFCell
|
||||||
import org.apache.poi.hssf.usermodel.HSSFCellStyle
|
import org.apache.poi.hssf.usermodel.HSSFCellStyle
|
||||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook
|
||||||
@@ -8,8 +9,9 @@ import org.apache.poi.ss.usermodel.BorderStyle
|
|||||||
import org.apache.poi.ss.usermodel.HorizontalAlignment
|
import org.apache.poi.ss.usermodel.HorizontalAlignment
|
||||||
import org.apache.poi.ss.usermodel.Sheet
|
import org.apache.poi.ss.usermodel.Sheet
|
||||||
import org.apache.poi.ss.usermodel.VerticalAlignment
|
import org.apache.poi.ss.usermodel.VerticalAlignment
|
||||||
import java.lang.Exception
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook
|
||||||
import java.lang.RuntimeException
|
import org.springframework.web.multipart.MultipartFile
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Excel操作对象
|
* Excel操作对象
|
||||||
@@ -81,6 +83,69 @@ object Excel {
|
|||||||
writeTo(wb)
|
writeTo(wb)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导入文件
|
||||||
|
*
|
||||||
|
* @param file 上传文件流
|
||||||
|
* @param columns 文件列名称、类型定义
|
||||||
|
* @param startRow 数据起始行,默认0
|
||||||
|
* @param startColumn 数据起始列,默认0
|
||||||
|
*
|
||||||
|
* @return ExcelData
|
||||||
|
*/
|
||||||
|
fun import(
|
||||||
|
file: MultipartFile,
|
||||||
|
columns: List<Pair<String, String>>,
|
||||||
|
startRow: Int = 0,
|
||||||
|
startColumn: Int = 0
|
||||||
|
): List<Map<String, Any>> {
|
||||||
|
if (file.originalFilename?.endsWith(".xls") != true && file.originalFilename?.endsWith(".xlsx") != true)
|
||||||
|
throw RuntimeException("无法识别的文件格式[${file.originalFilename}]")
|
||||||
|
|
||||||
|
val workbook = if (file.originalFilename?.endsWith(".xls") == true)
|
||||||
|
HSSFWorkbook(file.inputStream)
|
||||||
|
else
|
||||||
|
XSSFWorkbook(file.inputStream)
|
||||||
|
val sheet = workbook.getSheetAt(0)
|
||||||
|
|
||||||
|
val data = mutableListOf<Map<String, Any>>()
|
||||||
|
for (i in startRow..sheet.lastRowNum) {
|
||||||
|
val row = sheet.getRow(i) ?: continue
|
||||||
|
val rowData = mutableMapOf<String, Any>()
|
||||||
|
for (c in startColumn until columns.size + startColumn) {
|
||||||
|
try {
|
||||||
|
val column = columns[c]
|
||||||
|
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
|
||||||
|
} catch (ignored: Exception) {
|
||||||
|
row.getCell(c).stringCellValue
|
||||||
|
}
|
||||||
|
Boolean::class.java.name -> try {
|
||||||
|
row.getCell(c).booleanCellValue
|
||||||
|
} catch (ignored: Exception) {
|
||||||
|
row.getCell(c).stringCellValue
|
||||||
|
}
|
||||||
|
Date::class.java.name -> try {
|
||||||
|
row.getCell(c).dateCellValue
|
||||||
|
} catch (ignored: Exception) {
|
||||||
|
row.getCell(c).stringCellValue
|
||||||
|
}
|
||||||
|
else -> row.getCell(c).stringCellValue
|
||||||
|
}
|
||||||
|
rowData.put(columns[c].first, value)
|
||||||
|
} catch (ex: Exception) {
|
||||||
|
throw RuntimeException("解析EXCEL文件${file.originalFilename}第${i}行第${c}列出错", ex)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
data.add(rowData)
|
||||||
|
}
|
||||||
|
workbook.close()
|
||||||
|
file.inputStream.close()
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置列宽
|
* 设置列宽
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user