hcong
2024-12-17 09c8120288ea7df454c10d67911ab8643f2f4235
src/main/kotlin/cn/flightfeather/supervision/business/report/BaseExcel.kt
@@ -1,12 +1,14 @@
package cn.flightfeather.supervision.business.report
import cn.flightfeather.supervision.business.report.bean.BaseTemplateResult
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream
import java.util.*
/**
 * 各模板合并输出为整体文档
 * 单个数据源
 */
abstract class BaseExcel(val dataSource: DataSource) {
@@ -14,18 +16,40 @@
    abstract val fileName: String
    // 中间结果对象 by hc 2024.12.06
    private val objectResults: MutableList<MutableList<BaseTemplateResult>> = mutableListOf()
    // excel文档
    private var workbook = HSSFWorkbook()
    fun getReportName(): String = "${dataSource.areaName()}-${fileName}.xlsx"
    // 输出到对象
    fun toObject() {
        templates.forEach {
            if (!it.isExecuted) {
                it.execute()
            }
            it.toObject()
            objectResults.add(it.resultObjects)
        }
    }
    fun toFile(path: String) {
        val fileName = "${dataSource.areaName()}-${fileName}-${Date().time}.xls"
        val out = FileOutputStream(path + fileName)
        val fileName = getReportName()
        val file = File(path + fileName)
        if (!file.parentFile.exists()) {
            file.parentFile.mkdirs()
        }
        val out = FileOutputStream(file)
        toOutputStream(out)
    }
    fun toOutputStream(out: OutputStream) {
        templates.forEach {
            it.execute()
            if (!it.isExecuted) {
                it.execute()
            }
            it.toWorkBook(workbook)
        }
        workbook.write(out)