package utils import ( "github.com/tealeg/xlsx/v3" ) type StyleCfg struct { IsBold bool IsBorderTop bool IsBorderRight bool IsBorderLeft bool IsBorderBottom bool FontSize float64 Horizontal string Vertical string Font string Fill []string } func GetCommonStyle(cfg StyleCfg) *xlsx.Style { styleCommon := xlsx.NewStyle() if cfg.Horizontal == "" { cfg.Horizontal = "center" } if cfg.Vertical == "" { cfg.Vertical = "center" } if cfg.Font == "" { cfg.Font = "微软雅黑" } styleCommon.Alignment.Horizontal = cfg.Horizontal styleCommon.Alignment.Vertical = cfg.Vertical styleCommon.Alignment.WrapText = true styleCommon.Font.Name = cfg.Font if cfg.FontSize == 0 { cfg.FontSize = 11 } styleCommon.Font.Size = cfg.FontSize if cfg.IsBold { styleCommon.Font.Bold = true } if len(cfg.Fill) > 0 { styleCommon.Fill = *xlsx.NewFill("solid", cfg.Fill[0], cfg.Fill[1]) } styleCommon.Border.Top = "thin" styleCommon.Border.TopColor = "00000000" styleCommon.Border.Right = "thin" styleCommon.Border.RightColor = "00000000" styleCommon.Border.Bottom = "thin" styleCommon.Border.BottomColor = "00000000" styleCommon.Border.Left = "thin" styleCommon.Border.LeftColor = "00000000" if cfg.IsBorderTop { styleCommon.Border.Top = "medium" } if cfg.IsBorderRight { styleCommon.Border.Right = "medium" } if cfg.IsBorderBottom { styleCommon.Border.Bottom = "medium" } if cfg.IsBorderLeft { styleCommon.Border.Left = "medium" } styleCommon.ApplyBorder = true styleCommon.ApplyFont = true return styleCommon } func MergeCell(row *xlsx.Row, style *xlsx.Style, num int) { for i := 0; i < num; i++ { cell := row.AddCell() cell.SetStyle(style) } } type Row struct { Height float64 } func AddRow(sh *xlsx.Sheet, row Row) *xlsx.Row { r := sh.AddRow() r.SetHeight(row.Height) return r } type Cell struct { Float float64 Value string Format string Style *xlsx.Style HMerge int VMerge int } func AddCell(row *xlsx.Row, cell Cell) { c := row.AddCell() switch cell.Format { case "float": c.SetFloat(cell.Float) default: c.Value = cell.Value } if cell.HMerge > 0 { c.HMerge = cell.HMerge MergeCell(row, cell.Style, cell.HMerge) } if cell.VMerge > 0 { c.VMerge = cell.VMerge } c.SetStyle(cell.Style) } func SetColWidth(sh *xlsx.Sheet, width []float64) *xlsx.Sheet { for k, v := range width { idx := k + 1 sh.SetColWidth(idx, idx, v) } return sh }