xlsx/export/cell.go

37 lines
992 B
Go

package export
import (
"fmt"
"github.com/xuri/excelize/v2"
)
type Cell struct {
Value any
// 标题位置(列)
Location Location
// 标题合并列数(除开本身)
Colspan int
// 标题合并行数(除开本身)
Rowspan int
Style *excelize.Style
}
func (e *Exporter) SetCell(sheet string, cell Cell) {
start_col, end_col := e.CaculateCell(cell.Location, cell.Rowspan, cell.Colspan)
e.xlsx.MergeCell(sheet, start_col, end_col)
e.xlsx.SetCellValue(sheet, start_col, cell.Value)
if cell.Style != nil {
style, _ := e.xlsx.NewStyle(cell.Style)
e.xlsx.SetCellStyle(sheet, start_col, end_col, style)
}
}
func (e *Exporter) CaculateCell(location Location, rowSpan, colSpan int) (string, string) {
col_name, _ := excelize.ColumnNumberToName(location.X)
start_col := fmt.Sprintf("%s%d", col_name, location.Y)
h_pos, _ := excelize.ColumnNumberToName(location.X + rowSpan)
end_col := fmt.Sprintf("%s%d", h_pos, location.Y+colSpan)
return start_col, end_col
}