| | |
| | | |
| | | import org.apache.poi.hssf.usermodel.HSSFWorkbook |
| | | import org.apache.poi.ss.util.CellRangeAddress |
| | | import java.io.File |
| | | import java.io.OutputStream |
| | | import java.time.LocalDate |
| | | import java.util.* |
| | | import kotlin.math.max |
| | | |
| | | |
| | | /** |
| | | * @author riku |
| | |
| | | var text: String, |
| | | var rowSpan: Int = 1, |
| | | var colSpan: Int = 1, |
| | | var fontColor: Short? = null |
| | | var fontColor: Short? = null, |
| | | var isPercent:Boolean = false |
| | | ) |
| | | |
| | | /** |
| | |
| | | when (c) { |
| | | is MyCell -> { |
| | | rows.createCell(col).apply { |
| | | c.fontColor?.let {fC -> |
| | | val font = workbook.createFont() |
| | | if (c.isPercent) { |
| | | val cellStyle = workbook.createCellStyle() |
| | | |
| | | font.color = fC |
| | | cellStyle.setFont(font) |
| | | val format = workbook.createDataFormat() |
| | | cellStyle.dataFormat = format.getFormat("0.0%") |
| | | setCellStyle(cellStyle) |
| | | val v = c.text.toDoubleOrNull() |
| | | if (v != null) { |
| | | setCellValue(v) |
| | | } |
| | | } else { |
| | | c.fontColor?.let {fC -> |
| | | val font = workbook.createFont() |
| | | val cellStyle = workbook.createCellStyle() |
| | | |
| | | font.color = fC |
| | | cellStyle.setFont(font) |
| | | setCellStyle(cellStyle) |
| | | } |
| | | setCellValue(c.text) |
| | | } |
| | | setCellValue(c.text) |
| | | } |
| | | println("write1-1: ${c.text};($rowIndex, ${col})") |
| | | } |
| | |
| | | |
| | | when (c) { |
| | | is MyCell -> { |
| | | rows.createCell(map.key).setCellValue(c.text) |
| | | rows.createCell(col).apply { |
| | | if (c.isPercent) { |
| | | val cellStyle = workbook.createCellStyle() |
| | | val format = workbook.createDataFormat() |
| | | cellStyle.dataFormat = format.getFormat("0.0%") |
| | | setCellStyle(cellStyle) |
| | | val v = c.text.toDoubleOrNull() |
| | | if (v != null) { |
| | | setCellValue(v) |
| | | } |
| | | } else { |
| | | c.fontColor?.let {fC -> |
| | | val font = workbook.createFont() |
| | | val cellStyle = workbook.createCellStyle() |
| | | |
| | | font.color = fC |
| | | cellStyle.setFont(font) |
| | | setCellStyle(cellStyle) |
| | | } |
| | | setCellValue(c.text) |
| | | } |
| | | } |
| | | println("write2-1: ${c.text};($_rowIndex, ${map.key})") |
| | | } |
| | | is String -> { |
| | |
| | | /** |
| | | * 自动处理行合并数据 |
| | | */ |
| | | fun write2(out: OutputStream, heads: List<Array<Any>>, contents: MutableList<Array<Any>>) { |
| | | fun write2(out: OutputStream, heads: List<Array<Any>>, contents: MutableList<Array<Any>>, sheetName: String = "sheet1") { |
| | | |
| | | val workbook = HSSFWorkbook() |
| | | |
| | | write(heads, contents, workbook) |
| | | write(heads, contents, workbook, sheetName) |
| | | |
| | | workbook.write(out) |
| | | workbook.close() |