设置单元格样式

  1. func (f *File) SetCellStyle(sheet, hCell, vCell string, styleID int) error

根据给定的工作表名、单元格坐标区域和样式索引设置单元格的值。样式索引可以通过 NewStyle 函数获取。注意,在同一个坐标区域内的 diagonalDowndiagonalUp 需要保持颜色一致。SetCellStyle 将覆盖单元格的已有样式,而不会将样式与已有样式叠加或合并。

  • 例1,为名为 Sheet1 的工作表 D7 单元格设置边框样式:
  1. style, err := f.NewStyle(&excelize.Style{
  2. Border: []excelize.Border{
  3. {Type: "left", Color: "0000FF", Style: 3},
  4. {Type: "top", Color: "00FF00", Style: 4},
  5. {Type: "bottom", Color: "FFFF00", Style: 5},
  6. {Type: "right", Color: "FF0000", Style: 6},
  7. {Type: "diagonalDown", Color: "A020F0", Style: 7},
  8. {Type: "diagonalUp", Color: "A020F0", Style: 8},
  9. },
  10. })
  11. if err != nil {
  12. fmt.Println(err)
  13. }
  14. err = f.SetCellStyle("Sheet1", "D7", "D7", style)

为单元格设置边框样式

单元格 D7 的四个边框被设置了不同的样式和颜色,这与调用 NewStyle 函数时的参数有关,需要设置不同的样式可参考该章节的文档。

  • 例2,为名为 Sheet1 的工作表 D7 单元格设置渐变样式:
  1. style, err := f.NewStyle(&excelize.Style{
  2. Fill: excelize.Fill{Type: "gradient", Color: []string{"#FFFFFF", "#E0EBF5"}, Shading: 1},
  3. })
  4. if err != nil {
  5. fmt.Println(err)
  6. }
  7. err = f.SetCellStyle("Sheet1", "D7", "D7", style)

为单元格设置渐变样式

单元格 D7 被设置了渐变效果的颜色填充,渐变填充效果与调用 NewStyle 函数时的参数有关,需要设置不同的样式可参考该章节的文档。

  • 例3,为名为 Sheet1 的工作表 D7 单元格设置纯色填充:
  1. style, err := f.NewStyle(&excelize.Style{
  2. Fill: excelize.Fill{Type: "pattern", Color: []string{"#E0EBF5"}, Pattern: 1},
  3. })
  4. if err != nil {
  5. fmt.Println(err)
  6. }
  7. err = f.SetCellStyle("Sheet1", "D7", "D7", style)

为单元格设置纯色填充

单元格 D7 被设置了纯色填充。

  • 例4,为名为 Sheet1 的工作表 D7 单元格设置字符间距与旋转角度:
  1. f.SetCellValue("Sheet1", "D7", "样式")
  2. style, err := f.NewStyle(&excelize.Style{
  3. Alignment: &excelize.Alignment{
  4. Horizontal: "center",
  5. Indent: 1,
  6. JustifyLastLine: true,
  7. ReadingOrder: 0,
  8. RelativeIndent: 1,
  9. ShrinkToFit: true,
  10. TextRotation: 45,
  11. Vertical: "",
  12. WrapText: true,
  13. },
  14. })
  15. if err != nil {
  16. fmt.Println(err)
  17. }
  18. err = f.SetCellStyle("Sheet1", "D7", "D7", style)

设置字符间距与旋转角度

  • 例5,Excel 中的日期和时间用实数表示,例如 2017/7/4 12:00:00 PM 可以用数字 42920.5 来表示。为名为 Sheet1 的工作表 D7 单元格设置时间格式:
  1. f.SetCellValue("Sheet1", "D7", 42920.5)
  2. f.SetColWidth("Sheet1", "D", "D", 13)
  3. style, err := f.NewStyle(&excelize.Style{NumFmt: 22})
  4. if err != nil {
  5. fmt.Println(err)
  6. }
  7. err = f.SetCellStyle("Sheet1", "D7", "D7", style)

为单元格设置时间格式

单元格 D7 被设置了时间格式。注意,当应用了时间格式的单元格宽度过窄无法完整展示时会显示为 ####,可以拖拽调整列宽或者通过调用 SetColWidth 函数设置列宽到合适的大小使其正常显示。

  • 例6,为名为 Sheet1 的工作表 D7 单元格设置字体、字号、颜色和倾斜样式:
  1. f.SetCellValue("Sheet1", "D7", "Excel")
  2. style, err := f.NewStyle(&excelize.Style{
  3. Font: &excelize.Font{
  4. Bold: true,
  5. Italic: true,
  6. Family: "Times New Roman",
  7. Size: 36,
  8. Color: "#777777",
  9. },
  10. })
  11. if err != nil {
  12. fmt.Println(err)
  13. }
  14. err = f.SetCellStyle("Sheet1", "D7", "D7", style)

为单元格设置字体、字号、颜色和倾斜样式

  • 例7,锁定并隐藏名为 Sheet1 的工作表 D7 单元格:
  1. style, err := f.NewStyle(&excelize.Style{
  2. Protection: &excelize.Protection{
  3. Hidden: true,
  4. Locked: true,
  5. },
  6. })
  7. if err != nil {
  8. fmt.Println(err)
  9. }
  10. err = f.SetCellStyle("Sheet1", "D7", "D7", style)

要锁定单元格或隐藏公式,请保护工作表。在“审阅”选项卡上,单击“保护工作表”。