xlsx/export/title.go

43 lines
1.0 KiB
Go

package export
import "github.com/xuri/excelize/v2"
type Title struct {
// 标题名称
Name string
// 标题位置(列)
Location Location
// 标题合并列数
Colspan int
// 标题合并行数
Rowspan int
Style *excelize.Style
}
type Location struct {
X int
Y int
}
func (e *Exporter) SetGlobalTitleStyle(style *excelize.Style) {
e.GlobalTitleStyle = style
}
func (e *Exporter) SetTitle(sheet string) {
for _, title := range e.Titles {
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)
}
}
}