package main import ( "github.com/shopspring/decimal" "github.com/gogf/gf/v2/util/gconv" "github.com/xuri/excelize/v2" "app.yhyue.com/moapp/jybase/mongodb" ) func Excel() { file := excelize.NewFile() //设置表名 file.SetSheetName("Sheet1", "表1") //创建流式写入 writer, err := file.NewStreamWriter("表1") //修改列宽 writer.SetColWidth(1, 15, 12) //设置表头 writer.SetRow("A1", []interface{}{"id", "文件交付时间", "层级", "省份", "城市", "项目名称", "项目情况", "采购内容/项目目标", "项目单位", "项目联系人", "联系电话", " 预算金额(万元)", "匹配关键词"}) if err != nil { return } data, _ := db.Find("project_kedaxunfei_zxl", nil, nil, nil, false, -1, -1) i := 1 m := map[string]bool{} for _, v := range *data { projectname := gconv.String(v["projectname"]) if m[projectname] { continue } id := mongodb.BsonIdToSId(v["_id"]) area := gconv.String(v["area"]) city := gconv.String(v["city"]) procure_content := gconv.String(v["procure_content"]) kpi := gconv.String(v["kpi"]) institution := gconv.String(v["institution"]) phone := gconv.String(v["phone"]) budget_f := gconv.Float64(v["budget_f"]) matchkey := gconv.String(v["matchkey"]) unit := gconv.String(v["unit"]) if unit == "元" { budget_f = convertToWan(budget_f) } m[projectname] = true i++ //索引转单元格坐标 cell, _ := excelize.CoordinatesToCellName(1, i+1) //添加的数据 writer.SetRow(cell, []interface{}{id, "", "", area, city, projectname, procure_content, kpi, institution, "", phone, budget_f, matchkey}) } //结束流式写入 writer.Flush() file.SaveAs("kedaxunfei.xlsx") } func convertToWan(amount float64) float64 { amountDecimal := decimal.NewFromFloat(amount) convertFactor := decimal.NewFromInt(10000) convertedAmount := amountDecimal.Div(convertFactor) convertedFloat, ok := convertedAmount.Float64() if !ok { return 0 } return convertedFloat }