1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
 
/**
 * 各模板合并输出为整体文档
 * 单个数据源
 */
abstract class BaseExcel(val dataSource: DataSource) {
 
    abstract val templates: List<BaseTemplate>
 
    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()
            }
            objectResults.add(it.toObject())
        }
    }
 
    fun toFile(path: String) {
        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 {
            if (!it.isExecuted) {
                it.execute()
            }
            it.toWorkBook(workbook)
        }
        workbook.write(out)
        workbook.close()
        out.flush()
        out.close()
    }
}