package main import ( "fmt" "strconv" //"pkg/mod/github.com/360!ent!sec!group-!skylar/excelize@v1.4.1" "github.com/360EntSecGroup-Skylar/excelize" _ "github.com/go-sql-driver/mysql" "github.com/jinzhu/gorm" "log" ) var DB1 *gorm.DB type User struct { Rechargedate string `json:"rechargedate"` Rechargetime string `json:"rechargetime"` Rechargezone string `json:"rechargezone"` Rechargechannel string `json:"rechargechannel"` Rechargemoney string `json:"rechargemoney"` Rechargepeople string `json:"rechargepeople"` } //初始化並保持鏈接 func init() { var err error //DB, err = gorm.Open("mysql", "backupinfo:backupinfo123123@tcp(127.0.0.1:xxxxx)/test?charset=utf8&parseTime=True&loc=Local") //DB, err = gorm.Open("mysql", "backupinfo:backupinfo123123@tcp(xxxxx:xxxx)/test?charset=utf8&parseTime=True&loc=Local") DB1, err = gorm.Open("mysql", "xx:xxx@tcp(xxx63994)/xxx?charset=utf8&parseTime=True&loc=Local") // DB.LogMode(true)//打印sql語句 if err != nil { log.Fatalf("database11 connect is err:%s", err.Error()) } else { log.Print("connect database11 is success") } err = DB1.DB().Ping() if err != nil { DB1.DB().Close() log.Fatalf("Error on opening database11 connection: %s", err.Error()) } } func GetRoleList1() (userlist []User, err error) { //err = DB1.Find(&rolelist,"id < ?",10000).Error //err = DB.Find(&rolelist,20).Error //fmt.Println(rolelist) //var userlist []User //DB1.Raw("select id,publish_id from users limit 10").Find(&userlist) //DB1.Raw("select id,publish_id from users limit 10").Find(&userlist) DB1.Raw("SELECT DATE(updated_at) as rechargedate ,DATE_FORMAT(updated_at,'%H') as rechargetime ,server_id as rechargezone,channel_id as rechargechannel ,SUM(amount)/100 as rechargemoney,COUNT(DISTINCT(open_id)) as rechargepeople FROM orders fmt.Println("sql執行結果!!!!") fmt.Println(userlist) return userlist, err } func main() { //var user User //var userlists []User userlist,_ := GetRoleList1() fmt.Println("打印數據:") var userlists []map[string]interface{} for i := 0; i < len(userlist); i++ { userdic := make(map[string]interface{}) userdic["rechargedate"] = userlist[i].Rechargedate userdic["rechargetime"] = userlist[i].Rechargetime userdic["rechargezone"] = userlist[i].Rechargezone userdic["rechargechannel"] = userlist[i].Rechargechannel userdic["rechargemoney"] = userlist[i].Rechargemoney userdic["rechargepeople"] = userlist[i].Rechargepeople userlists = append(userlists, userdic) } fmt.Println(userlist) fmt.Println(userlists) // 列標題 //titles := []string{ // "姓名","年齡","sex", //} //新的列標題 titles := []string{ "充值日期","充值時間","充值區服","充值渠道","實付金額","充值人數", } //數據源 //data := []map[string]interface{}{ // map[string]interface{}{"name":"jack","age":18,"sex":1}, // map[string]interface{}{"name":"mary","age":28,"sex":2}, //} //fmt.Println(data) f := excelize.NewFile() // Create a new sheet. index := f.NewSheet("Sheet1") for clumnNum,v := range titles { sheetPosition := Div(clumnNum+1)+"1" fmt.Print(sheetPosition) f.SetCellValue("Sheet1", sheetPosition,v) } for lineNum,dic := range userlists { clumnNum := 0 fmt.Println(lineNum,dic) for k,_ := range dic{ clumnNum++; Anewposition := "A" + strconv.Itoa(lineNum+2) Bnewposition := "B" + strconv.Itoa(lineNum+2) Cnewposition := "C" + strconv.Itoa(lineNum+2) Dnewposition := "D" + strconv.Itoa(lineNum+2) Enewposition := "E" + strconv.Itoa(lineNum+2) Fnewposition := "F" + strconv.Itoa(lineNum+2) switch k { case "rechargedate": fmt.Println("name的值是:",dic["name"]) f.SetCellValue("Sheet1", Anewposition,dic["rechargedate"]) break case "rechargetime": f.SetCellValue("Sheet1", Bnewposition,dic["rechargetime"]) break case "rechargezone": f.SetCellValue("Sheet1", Cnewposition,dic["rechargezone"]) break case "rechargechannel": f.SetCellValue("Sheet1", Dnewposition,dic["rechargechannel"]) break case "rechargemoney": f.SetCellValue("Sheet1", Enewposition,dic["rechargemoney"]) break case "rechargepeople": f.SetCellValue("Sheet1", Fnewposition,dic["rechargepeople"]) break } } } // Set active sheet of the workbook. f.SetActiveSheet(index) //生成圖形 if err := f.AddChart("Sheet1", "M1", `{"type":"col3DClustered","series":[{"name":"Sheet1!$C$2","categories":"Sheet1!$B$2:$B$61","values":"Sheet1!$E$2:$E$61"},{"name":"Sheet1!$C$13","categories":"Sheet1!$B$2:$B$61","values":"Sheet1!$E$2:$E$61"},{"name":"Sheet1!$C$32","categories":"Sheet1!$B$2:$B$61","values":"Sheet1!$E$2:$E$61"}],"title":{"name":"Fruit 3D Clustered Column Chart"}}`); err != nil { fmt.Println(err) return } // Save xlsx file by the given path. if err := f.SaveAs("Book2.xlsx"); err != nil { println(err.Error()) } } // Div 數字轉字母 func Div(Num int) string{ var( Str string = "" k int temp []int //保存轉化後每一位數據的值,而後經過索引的方式匹配A-Z ) //用來匹配的字符A-Z Slice := []string{"","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O", "P","Q","R","S","T","U","V","W","X","Y","Z"} if Num >26 { //數據大於26須要進行拆分 for { k = Num % 26 //從個位開始拆分,若是求餘爲0,說明末尾爲26,也就是Z,若是是轉化爲26進制數,則末尾是能夠爲0的,這裏必須爲A-Z中的一個 if k == 0 { temp = append(temp, 26) k = 26 } else { temp = append(temp, k) } Num = (Num - k) / 26 //減去Num最後一位數的值,由於已經記錄在temp中 if Num <= 26{ //小於等於26直接進行匹配,不須要進行數據拆分 temp = append(temp, Num) break } } }else{ return Slice[Num] } for _,value := range temp{ Str = Slice[value] + Str //由於數據切分後存儲順序是反的,因此Str要放在後面 } return Str }