feiyu02
2022-11-15 909fd8929d7906f1dca68acc05e36e29b0b9192c
src/main/kotlin/cn/flightfeather/supervision/business/report/BaseTemplate.kt
@@ -12,9 +12,10 @@
/**
 * excel报告模板基类
 * 单个数据源
 */
// FIXME: 2022/7/15 模板简化:只有表头会涉及单元格合并,默认表内容中没有合并
abstract class BaseTemplate(val dataSource: DataSource){
abstract class BaseTemplate(val dataSource: DataSource) : BaseOutputInterface {
    //列组合
    abstract val cols: List<BaseCols>
@@ -24,6 +25,7 @@
    //表头
    val head = mutableListOf<MutableList<ExcelUtil.MyCell>>()
    //内容
    val contents = mutableListOf<MutableList<Any>>()
@@ -33,11 +35,11 @@
        dataSource.reset()
        //合成表头
        cols.forEach {
            it.combineHead(head,dataSource)
            it.combineHead(head, dataSource)
        }
        //合成数据
        dataSource.loop { index, rowData ->
            cols.forEach {col ->
            cols.forEach { col ->
                val r = col.getOneRow(rowData)
                if (index >= contents.size) {
                    contents.add(mutableListOf())
@@ -47,12 +49,12 @@
        }
    }
    fun toWorkBook(wb: HSSFWorkbook) {
    override fun toWorkBook(wb: HSSFWorkbook) {
        val f = tableFormat()
        ExcelUtil.write(f.first, f.second, wb, templateName)
    }
    fun toOutputStream(out: OutputStream, sheetName: String? = null) {
    override fun toOutputStream(out: OutputStream, sheetName: String?) {
        val f = tableFormat()
        ExcelUtil.write2(out, f.first, f.second, sheetName ?: templateName)
    }
@@ -60,7 +62,7 @@
    /**
     * 输出为文档
     */
    fun toFile(path: String) {
    override fun toFile(path: String) {
        val fileName = "${dataSource.areaName()}-${templateName}-${Date().time}.xls"
        val out = FileOutputStream(path + fileName)
        toOutputStream(out)
@@ -69,12 +71,13 @@
    /**
     * 表头和表内容格式转化
     */
    private fun tableFormat(): Pair<MutableList<Array<Any>>, MutableList<Array<Any>>> {
    fun tableFormat(): Pair<MutableList<Array<Any>>, MutableList<Array<Any>>> {
        val h = mutableListOf<Array<Any>>()
        val c = mutableListOf<Array<Any>>()
        head.forEach { h.add(it.toTypedArray()) }
        contents.forEach { c.add(it.toTypedArray()) }
        val index = c[0][0]
        //按照第一列进行排序
        if (index is Int) {
            c.sortBy {
                if (it[0] is Int) {
@@ -91,11 +94,3 @@
        return Pair(h, c)
    }
}
//固定列组合
//基本信息
//问题扣分分布