feiyu02
2024-01-26 01eae19a4870033e879a3faa6749eece92926cab
src/main/kotlin/com/flightfeather/uav/common/utils/ExcelUtil.kt
@@ -1,8 +1,16 @@
package com.flightfeather.uav.common.utils
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import com.flightfeather.uav.common.exception.ResponseErrorException
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.util.CellRangeAddress
import org.apache.poi.xssf.streaming.SXSSFWorkbook
import org.apache.poi.xssf.usermodel.XSSFRow
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import org.jetbrains.kotlin.incremental.isJavaFile
import java.io.ByteArrayInputStream
import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import java.time.LocalDate
import java.util.*
import kotlin.math.max
@@ -19,8 +27,36 @@
            var text: String,
            var rowSpan: Int = 1,
            var colSpan: Int = 1,
            var fontColor: Short? = null
        var fontColor: Short? = null,
    )
    /**
     * 读取文件
     * @param file 要读取的文件
     * @param headerCheck 文件首行检查回调
     * @param onRow 单行内容回调,从第二行开始
     */
    fun read(file: File, headerCheck: (header: XSSFRow) -> Boolean, onRow: (row: Row) -> Unit): Boolean {
        if (!file.exists() || !file.isFile) throw ResponseErrorException("it's not a normal file!")
        if (!file.extension.equals("xls", ignoreCase = true) || !file.extension.equals("xlsx", ignoreCase = true)) {
            throw ResponseErrorException("file's extension name should be xls or xlsx!")
        }
        return read(FileInputStream(file), headerCheck, onRow)
    }
    fun read(input: InputStream, headerCheck: (header: XSSFRow) -> Boolean, onRow: (row: Row) -> Unit): Boolean {
        val workbook = XSSFWorkbook(input)
        val sheet1 = workbook.getSheetAt(0)
        val header = sheet1.getRow(sheet1.topRow.toInt())
        return if (headerCheck(header)) {
            // 获取迭代器并去除第一行标题
            val iterator = sheet1.rowIterator().also { it.next() }
            iterator.forEach { onRow(it) }
            true
        } else {
            false
        }
    }
    /**
     * 自动处理行合并数据
@@ -153,7 +189,13 @@
    /**
     * 自动处理行合并数据
     */
    fun write(heads: List<Array<String>>, contents: List<Array<Any>>, workbook: SXSSFWorkbook, sheetName: String = "sheet1", row: Int = 0): Int {
    fun write(
        heads: List<Array<String>>,
        contents: List<Array<Any>>,
        workbook: SXSSFWorkbook,
        sheetName: String = "sheet1",
        row: Int = 0,
    ): Int {
        val sheet = workbook.getSheet(sheetName)?: workbook.createSheet(sheetName)
//        println("sheet: $sheetName")
@@ -195,7 +237,10 @@
                        }
                        if (rowspan > 1 || colSpan > 1) {
                            log("合并1-1:$rowIndex;${rowIndex + rowspan - 1};$col")
                            sheet.addMergedRegion(CellRangeAddress(rowIndex, rowIndex + rowspan - 1, col, col + colSpan - 1))
                            sheet.addMergedRegion(CellRangeAddress(rowIndex,
                                rowIndex + rowspan - 1,
                                col,
                                col + colSpan - 1))
                        }
                        if (cell.isEmpty()) {
                            ""
@@ -210,7 +255,10 @@
                        }
                        if (rowspan > 1 || colSpan > 1) {
                            log("合并1-2:$rowIndex;${rowIndex + rowspan - 1};$col")
                            sheet.addMergedRegion(CellRangeAddress(rowIndex, rowIndex + rowspan - 1, col, col + colSpan - 1))
                            sheet.addMergedRegion(CellRangeAddress(rowIndex,
                                rowIndex + rowspan - 1,
                                col,
                                col + colSpan - 1))
                        }
                        cell
                    }
@@ -272,7 +320,10 @@
                        }
                        if (rowspan > 1 || colSpan > 1) {
                            log("合并2:${index};${index + rowspan - 1};${map.key}")
                            sheet.addMergedRegion(CellRangeAddress(index, index + rowspan - 1, map.key, map.key + colSpan - 1))
                            sheet.addMergedRegion(CellRangeAddress(index,
                                index + rowspan - 1,
                                map.key,
                                map.key + colSpan - 1))
                        }
                        when (c) {