2024-05-15 15:09:48 +08:00
|
|
|
package export
|
|
|
|
|
|
|
|
import "github.com/xuri/excelize/v2"
|
|
|
|
|
|
|
|
type Title struct {
|
|
|
|
// 标题名称
|
|
|
|
Name string
|
|
|
|
// 标题位置(列)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type Location struct {
|
|
|
|
X int
|
|
|
|
Y int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exporter) SetGlobalTitleStyle(style *excelize.Style) {
|
|
|
|
e.GlobalTitleStyle = style
|
|
|
|
}
|
|
|
|
|
2024-06-14 11:12:59 +08:00
|
|
|
func (e *Exporter) WriteTitle(sheet string, title Title) {
|
|
|
|
start_col, end_col := e.CaculateCell(title.Location, title.Rowspan, title.Colspan)
|
|
|
|
e.xlsx.MergeCell(sheet, start_col, end_col)
|
|
|
|
e.xlsx.SetCellValue(sheet, start_col, title.Name)
|
|
|
|
start_col_number, _, _ := excelize.SplitCellName(start_col)
|
|
|
|
e.xlsx.SetColWidth(sheet, start_col_number, start_col_number, 20)
|
|
|
|
if title.Style != nil {
|
|
|
|
style, _ := e.xlsx.NewStyle(title.Style)
|
|
|
|
e.xlsx.SetCellStyle(sheet, start_col, end_col, style)
|
|
|
|
} else if e.GlobalTitleStyle != nil {
|
|
|
|
style, _ := e.xlsx.NewStyle(e.GlobalTitleStyle)
|
|
|
|
e.xlsx.SetCellStyle(sheet, start_col, end_col, style)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-15 15:09:48 +08:00
|
|
|
func (e *Exporter) SetTitle(sheet string) {
|
|
|
|
for _, title := range e.Titles {
|
2024-06-14 11:12:59 +08:00
|
|
|
e.WriteTitle(sheet, title)
|
2024-05-15 15:09:48 +08:00
|
|
|
}
|
|
|
|
}
|