2024-05-15 15:09:48 +08:00
|
|
|
package export
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/xuri/excelize/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Cell struct {
|
|
|
|
Value any
|
|
|
|
// 标题位置(列)
|
|
|
|
Location Location
|
2024-05-16 16:53:29 +08:00
|
|
|
// 标题合并列数
|
2024-05-15 15:09:48 +08:00
|
|
|
Colspan int
|
2024-05-16 16:53:29 +08:00
|
|
|
// 标题合并行数
|
2024-05-15 15:09:48 +08:00
|
|
|
Rowspan int
|
|
|
|
Style *excelize.Style
|
|
|
|
}
|
|
|
|
|
2024-05-16 16:53:29 +08:00
|
|
|
type CellLocation struct {
|
|
|
|
LeftX int
|
|
|
|
RightX int
|
|
|
|
TopY int
|
|
|
|
BottomY int
|
|
|
|
IsMerge bool
|
|
|
|
}
|
|
|
|
|
2024-05-15 15:09:48 +08:00
|
|
|
func (e *Exporter) SetCell(sheet string, cell Cell) {
|
|
|
|
start_col, end_col := e.CaculateCell(cell.Location, cell.Rowspan, cell.Colspan)
|
2024-05-16 16:53:29 +08:00
|
|
|
fmt.Println(sheet, start_col, end_col, cell.Value)
|
|
|
|
if start_col != end_col {
|
|
|
|
e.xlsx.MergeCell(sheet, start_col, end_col)
|
|
|
|
}
|
|
|
|
leftx, topy, _ := excelize.CellNameToCoordinates(start_col)
|
|
|
|
rightx, bottomy, _ := excelize.CellNameToCoordinates(end_col)
|
2024-05-15 15:09:48 +08:00
|
|
|
e.xlsx.SetCellValue(sheet, start_col, cell.Value)
|
2024-05-16 16:53:29 +08:00
|
|
|
CellLocation := CellLocation{
|
|
|
|
LeftX: leftx,
|
|
|
|
RightX: rightx,
|
|
|
|
TopY: topy,
|
|
|
|
BottomY: bottomy,
|
|
|
|
IsMerge: start_col != end_col,
|
|
|
|
}
|
|
|
|
e.preLocation = &CellLocation
|
2024-05-15 15:09:48 +08:00
|
|
|
if cell.Style != nil {
|
|
|
|
style, _ := e.xlsx.NewStyle(cell.Style)
|
|
|
|
e.xlsx.SetCellStyle(sheet, start_col, end_col, style)
|
2024-05-16 16:53:29 +08:00
|
|
|
} else if e.GlobalCellStyle != nil {
|
|
|
|
style, _ := e.xlsx.NewStyle(e.GlobalCellStyle)
|
|
|
|
e.xlsx.SetCellStyle(sheet, start_col, end_col, style)
|
2024-05-15 15:09:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exporter) CaculateCell(location Location, rowSpan, colSpan int) (string, string) {
|
2024-05-16 16:53:29 +08:00
|
|
|
if rowSpan >= 1 {
|
|
|
|
rowSpan -= 1
|
|
|
|
} else {
|
|
|
|
rowSpan = 0
|
|
|
|
}
|
|
|
|
if colSpan >= 1 {
|
|
|
|
colSpan -= 1
|
|
|
|
} else {
|
|
|
|
colSpan = 0
|
|
|
|
}
|
2024-05-15 15:09:48 +08:00
|
|
|
col_name, _ := excelize.ColumnNumberToName(location.X)
|
|
|
|
start_col := fmt.Sprintf("%s%d", col_name, location.Y)
|
2024-05-16 16:53:29 +08:00
|
|
|
h_pos, _ := excelize.ColumnNumberToName(location.X + colSpan)
|
|
|
|
end_col := fmt.Sprintf("%s%d", h_pos, location.Y+rowSpan)
|
2024-05-15 15:09:48 +08:00
|
|
|
return start_col, end_col
|
|
|
|
}
|