excel.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package excel
  2. import (
  3. "fmt"
  4. "github.com/tealeg/xlsx"
  5. "monitor/db"
  6. )
  7. func Writing(userInfos []db.UserInfo) {
  8. file := xlsx.NewFile()
  9. sheet, err := file.AddSheet("Sheet1")
  10. if err != nil {
  11. fmt.Print(err.Error())
  12. }
  13. row := sheet.AddRow()
  14. row.SetHeightCM(0.5)
  15. cell := row.AddCell()
  16. cell.Value = "用户 ID"
  17. cell = row.AddCell()
  18. cell.Value = "用户名"
  19. style := xlsx.NewStyle()
  20. style.Border = xlsx.Border{
  21. Left: "thin",
  22. Right: "thin",
  23. Top: "thin",
  24. Bottom: "thin",
  25. }
  26. style.ApplyBorder = true;
  27. for _, info := range userInfos {
  28. row := sheet.AddRow()
  29. row.SetHeightCM(0.5)
  30. cell := row.AddCell()
  31. cell.Value = info.UserId
  32. cell.SetStyle(style)
  33. cell = row.AddCell()
  34. cell.Value = info.UserName
  35. cell.SetStyle(style)
  36. }
  37. // 保存
  38. err = file.Save("2019-_-_-2019-_-_Lag延时数据.xlsx")
  39. if err != nil {
  40. fmt.Printf(err.Error())
  41. }
  42. }
  43. func customStyle() *xlsx.Style {
  44. style := xlsx.NewStyle()
  45. font := *xlsx.NewFont(12, "Verdana")
  46. font.Bold = true
  47. font.Italic = true
  48. font.Underline = true
  49. style.Font = font
  50. //fill := *xlsx.NewFill("solid", "00FF0000", "FF000000")
  51. //style.Fill = fill
  52. border := *xlsx.NewBorder("thin", "thin", "thin", "thin")
  53. style.Border = border
  54. style.ApplyBorder = true
  55. style.ApplyFill = true
  56. style.ApplyFont = true
  57. return style
  58. }