日历
使用 Go 语言在 Excel 文档中创建日历:
package main
import (
"fmt"
"strconv"
"github.com/360EntSecGroup-skylar/excelize"
)
func main() {
// 创建工作簿
f := excelize.NewFile()
var (
monthStyle, titleStyle, dataStyle, blankStyle,
grayBlankStyle, grayDataStyle, noteStyle, noteLineStyle int
err error
addr string
sheet = "Sheet1"
// 定义单元格的值
data = map[int][]interface{}{
1: {"五月 2020"},
3: {"日", "一", "二", "三",
"四", "五", "六"},
4: {26, 27, 28, 29, 30, 1, 2},
6: {3, 4, 5, 6, 7, 8, 9},
8: {10, 11, 12, 13, 14, 15, 16},
10: {17, 18, 19, 20, 21, 22, 23},
12: {24, 25, 26, 27, 28, 29, 30},
14: {31, 1, 2, 3, 4, 5, 6},
18: {"笔记"},
}
// 自定义行高
height = map[int]float64{
1: 45, 3: 22, 5: 44, 7: 44, 9: 44, 11: 44, 13: 44, 15: 44,
18: 24, 19: 24, 20: 24, 21: 24, 22: 24, 23: 24, 24: 24,
}
top = excelize.Border{Type: "top", Style: 1, Color: "DADEE0"}
left = excelize.Border{Type: "left", Style: 1, Color: "DADEE0"}
right = excelize.Border{Type: "right", Style: 1, Color: "DADEE0"}
bottom = excelize.Border{Type: "bottom", Style: 1, Color: "DADEE0"}
fill = excelize.Fill{Type: "pattern", Color: []string{"EFEFEF"}, Pattern: 1}
)
// 按行赋值
for r, row := range data {
if addr, err = excelize.JoinCellName("B", r); err != nil {
fmt.Println(err)
return
}
if err = f.SetSheetRow(sheet, addr, &row); err != nil {
fmt.Println(err)
return
}
}
// 设置自定义行高
for r, ht := range height {
if err = f.SetRowHeight(sheet, r, ht); err != nil {
fmt.Println(err)
return
}
}
// 设置列宽
if err = f.SetColWidth(sheet, "B", "H", 16.5); err != nil {
fmt.Println(err)
return
}
// 合并月份单元格
if err = f.MergeCell(sheet, "B1", "D1"); err != nil {
fmt.Println(err)
return
}
// 设置月份单元格样式
if monthStyle, err = f.NewStyle(&excelize.Style{
Font: &excelize.Font{Color: "1f7f3b", Bold: true, Size: 22, Family: "Arial"},
}); err != nil {
fmt.Println(err)
return
}
// 设置月份单元格字体
if err = f.SetCellStyle(sheet, "B1", "D1", monthStyle); err != nil {
fmt.Println(err)
return
}
// 创建周一至周日标题行样式
if titleStyle, err = f.NewStyle(&excelize.Style{
Font: &excelize.Font{Color: "1f7f3b", Bold: true, Family: "Arial"},
Fill: excelize.Fill{Type: "pattern", Color: []string{"E6F4EA"}, Pattern: 1},
Alignment: &excelize.Alignment{Vertical: "center", Horizontal: "center"},
Border: []excelize.Border{{Type: "top", Style: 2, Color: "1f7f3b"}},
}); err != nil {
fmt.Println(err)
return
}
// 设置周一至周日标题行样式
if err = f.SetCellStyle(sheet, "B3", "H3", titleStyle); err != nil {
fmt.Println(err)
return
}
// 创建日期单元格样式
if dataStyle, err = f.NewStyle(&excelize.Style{
Border: []excelize.Border{top, left, right},
}); err != nil {
fmt.Println(err)
return
}
// 设置日期单元格样式
for _, r := range []int{4, 6, 8, 10, 12, 14} {
if err = f.SetCellStyle(sheet, "B"+strconv.Itoa(r),
"H"+strconv.Itoa(r), dataStyle); err != nil {
fmt.Println(err)
return
}
}
// 创建空白单元格样式
if blankStyle, err = f.NewStyle(&excelize.Style{
Border: []excelize.Border{left, right, bottom},
}); err != nil {
fmt.Println(err)
return
}
// 设置空白单元格样式
for _, r := range []int{5, 7, 9, 11, 13, 15} {
if err = f.SetCellStyle(sheet, "B"+strconv.Itoa(r),
"H"+strconv.Itoa(r), blankStyle); err != nil {
fmt.Println(err)
return
}
}
// 创建上个月和下个月日期中的空白单元格样式
if grayBlankStyle, err = f.NewStyle(&excelize.Style{
Border: []excelize.Border{left, right, bottom},
Fill: fill}); err != nil {
fmt.Println(err)
return
}
// 设置上个月和下个月日期中的空白单元格样式
if err = f.SetCellStyle(sheet, "B5", "F5", grayBlankStyle); err != nil {
fmt.Println(err)
return
}
if err = f.SetCellStyle(sheet, "C15", "H15", grayBlankStyle); err != nil {
fmt.Println(err)
return
}
// 创建上个月和下个月日期的单元格样式
if grayDataStyle, err = f.NewStyle(&excelize.Style{
Border: []excelize.Border{left, right, top},
Font: &excelize.Font{Color: "777777"}, Fill: fill}); err != nil {
fmt.Println(err)
return
}
// 设置上个月和下个月日期的单元格样式
if err = f.SetCellStyle(sheet, "B4", "F4", grayDataStyle); err != nil {
fmt.Println(err)
return
}
if err = f.SetCellStyle(sheet, "C14", "H14", grayDataStyle); err != nil {
fmt.Println(err)
return
}
// 创建笔记单元格样式
if noteStyle, err = f.NewStyle(&excelize.Style{
Font: &excelize.Font{Color: "1f7f3b", Bold: true, Size: 14, Family: "Arial"},
}); err != nil {
fmt.Println(err)
return
}
// 设置笔记单元格样式
if err = f.SetCellStyle(sheet, "B18", "B18", noteStyle); err != nil {
fmt.Println(err)
return
}
// 创建笔记区域横线样式
if noteLineStyle, err = f.NewStyle(&excelize.Style{
Border: []excelize.Border{{Type: "bottom", Style: 4, Color: "DDDDDD"}},
}); err != nil {
fmt.Println(err)
return
}
// 设置笔记区域横线样式
for r := 19; r < 25; r++ {
if err = f.SetCellStyle(sheet, "B"+strconv.Itoa(r),
"H"+strconv.Itoa(r), noteLineStyle); err != nil {
fmt.Println(err)
return
}
}
// 隐藏工作表网格线
if err = f.SetSheetViewOptions(sheet, 0,
excelize.ShowGridLines(false)); err != nil {
fmt.Println(err)
return
}
// 重命名工作表
f.SetSheetName(sheet, "五月 20")
// 保存工作簿
if err = f.SaveAs("日历.xlsx"); err != nil {
fmt.Println(err)
}
}