excel.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package main
  2. import (
  3. "github.com/shopspring/decimal"
  4. "github.com/gogf/gf/v2/util/gconv"
  5. "github.com/xuri/excelize/v2"
  6. "app.yhyue.com/moapp/jybase/mongodb"
  7. )
  8. func Excel() {
  9. file := excelize.NewFile()
  10. //设置表名
  11. file.SetSheetName("Sheet1", "表1")
  12. //创建流式写入
  13. writer, err := file.NewStreamWriter("表1")
  14. //修改列宽
  15. writer.SetColWidth(1, 15, 12)
  16. //设置表头
  17. writer.SetRow("A1", []interface{}{"id", "文件交付时间", "层级", "省份", "城市", "项目名称", "项目情况", "采购内容/项目目标", "项目单位", "项目联系人", "联系电话", " 预算金额(万元)", "匹配关键词"})
  18. if err != nil {
  19. return
  20. }
  21. data, _ := db.Find("project_kedaxunfei_zxl", nil, nil, nil, false, -1, -1)
  22. i := 1
  23. m := map[string]bool{}
  24. for _, v := range *data {
  25. projectname := gconv.String(v["projectname"])
  26. if m[projectname] {
  27. continue
  28. }
  29. id := mongodb.BsonIdToSId(v["_id"])
  30. area := gconv.String(v["area"])
  31. city := gconv.String(v["city"])
  32. procure_content := gconv.String(v["procure_content"])
  33. kpi := gconv.String(v["kpi"])
  34. institution := gconv.String(v["institution"])
  35. phone := gconv.String(v["phone"])
  36. budget_f := gconv.Float64(v["budget_f"])
  37. matchkey := gconv.String(v["matchkey"])
  38. unit := gconv.String(v["unit"])
  39. if unit == "元" {
  40. budget_f = convertToWan(budget_f)
  41. }
  42. m[projectname] = true
  43. i++
  44. //索引转单元格坐标
  45. cell, _ := excelize.CoordinatesToCellName(1, i+1)
  46. //添加的数据
  47. writer.SetRow(cell, []interface{}{id, "", "", area, city, projectname, procure_content, kpi, institution, "", phone, budget_f, matchkey})
  48. }
  49. //结束流式写入
  50. writer.Flush()
  51. file.SaveAs("kedaxunfei.xlsx")
  52. }
  53. func convertToWan(amount float64) float64 {
  54. amountDecimal := decimal.NewFromFloat(amount)
  55. convertFactor := decimal.NewFromInt(10000)
  56. convertedAmount := amountDecimal.Div(convertFactor)
  57. convertedFloat, ok := convertedAmount.Float64()
  58. if !ok {
  59. return 0
  60. }
  61. return convertedFloat
  62. }