您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容介绍了“beego excel的导出和读取操作”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
/** 利用hrpose rpc 连接传输以组合好的数据生成文件 https://github.com/hprose php 项目中引用 在你的 composer 项目中的 composer.json 文件中,添加这部分: { "require": { "hprose/hprose": ">=2.0.0" } } 使用 $header = ['aaa','cccc','dddd']; $data = [['a1','c1','d1'],['a2','c2','d2'],['a2','c2','d2']]; $client = new \Hprose\Http\Client(env('excel_rpc', 'http://192.168.0.230:8881/rpc'), false);//同步执行 $b_str = json_encode(array_merge([$header], $data)); $path_str = "dc_" . $type . '_' . $this->lang . date('Ymd-His') . '.xlsx'; $return_data = json_decode($client->excel($b_str, $path_str), true); Array ( [code] => 0 [data] => dc_test20190428-124612.xlsx [msg] => 生成完毕共花费0秒public/excel/dc_test20190428-124612.xlsx ) https://github.com/hprose/hprose-php/wiki/05-Hprose-%E5%AE%A2%E6%88%B7%E7%AB%AF go 部分 go 1.11+ 使用 go.mod 版本 go 环境自己搭建 go mod download build go build -o rpc_excel rpc.go 跨平台编译一次编译世界通用 CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/rpc_excel_linux rpc_excel.go CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o bin/rpc_excel_win rpc_excel.go */ package main import ( "encoding/json" "fmt" "github.com/360EntSecGroup-Skylar/excelize" "github.com/astaxie/beego" "github.com/hprose/hprose-golang/rpc" "github.com/robfig/cron" "os" "strconv" "time" "log" "regexp" "io/ioutil" "runtime" "runtime/debug" ) func excel(name string, path string) string { var jsonArray [][]string //解析传输过来的二维数组 jsonArray = strjsonArray(name) file := "public/excel/" + path os.Remove(file) fmt.Println(file) f := excelize.NewFile() start1 := time.Now().Unix() // Create a new sheet. for k, v := range jsonArray { if v == nil { /*跳过此次循环*/ continue } for k1, v1 := range v { var si string //大A列 大B列 if k1/26 > 0 { si = string(k1/26 - 1 + 65) + string(k1%26 + 65) } else { si = string(k1%26 + 65) } ki := strconv.Itoa(k + 1) // Set value of a cell. f.SetCellValue("Sheet1", si+ki, typeof(v1)) } } // Save xlsx file by the given path. err := f.SaveAs("../"+file) start2 := time.Now().Unix() var pFile string pFile = fmt.Sprintf("生成完毕共花费%d秒%s", start2-start1, file) fmt.Println(pFile) result := make(map[string]interface{}) result["code"] = 0 result["data"] = path result["msg"] = pFile //内存释放 runtime.GC() debug.FreeOSMemory() if err != nil { fmt.Println(err) result["code"] = -1 result["data"] = "" result["msg"] = err } resA, _ := json.Marshal(result) return string(resA) } func strjsonArray(data string) [][]string { var jsonArray [][]string err := json.Unmarshal([]byte(data), &jsonArray) if err != nil { fmt.Println(err) } return jsonArray } func oneJsonArray(data string) interface{} { var _jsonArray interface{} err := json.Unmarshal([]byte(data), &_jsonArray) if err != nil { fmt.Println(err) } return _jsonArray } type ExcelDelJob struct { } func (this ExcelDelJob)Run() { files, _ := ioutil.ReadDir("../public/excel/") for _, f := range files { fmt.Println(f.Name()) reg := regexp.MustCompile(`(?i:^dc_)*.(\d)+?.xlsx`) if len(reg.FindAllString( f.Name(), -1)) > 0 { file := "../public/excel/" + f.Name() os.Remove(file) log.Println("ExcelDelJob delete ... %s",file) } } log.Println("ExcelDelJob list ...") } func typeof(v interface{}) string { switch t := v.(type) { case string: return t case int: return strconv.Itoa(t) default: _ = t return "" } } /* 传入值 $data = [['a1','c1','d1'],['a2','c2','d2'],['a2','c2','d2']]; 这样才能解析 {"data":[['a1','c1','d1'],['a2',0,'d2'],['a2','c2',100]]} */ func excelV2(data string, path string) string { //解析传输过来的二维数组 file := "public/excel/" + path os.Remove(file) fmt.Println(file) f := excelize.NewFile() start1 := time.Now().Unix() // Create a new sheet. b := []byte(fmt.Sprintf("{\"data\":%s}",data)) var bf interface{} err := json.Unmarshal(b, &bf) if err != nil { fmt.Println(err) } m := bf.(map[string]interface{}) switch vv := m["data"].(type) { case string: fmt.Println(vv, "is string", vv) case int: fmt.Println(vv, "is int", vv) case []interface{}: //fmt.Println(vv, "is an array:") //大行 for k, u := range vv { switch vvv := u.(type) { case string: fmt.Println(k, "is string", vvv) case int: fmt.Println(k, "is int", vvv) case []interface{}: //大列 for k1, u1 := range vvv { var si string //大A列 大B列 if k1/26 > 0 { si = string(k1/26 - 1 + 65) + string(k1%26 + 65) } else { si = string(k1%26 + 65) } ki := strconv.Itoa(k + 1) switch u11 := u1.(type) { case int: f.SetCellInt("Sheet1", si+ki, u11) break default: f.SetCellValue("Sheet1", si+ki, u11) } //f.SetCellValue("Sheet1", si+ki, u1) } default: fmt.Println("is of a type I don't know how to handle") } } default: fmt.Println( vv," is of a type I don't know how to handle") } // Save xlsx file by the given path. err = f.SaveAs("../"+file) start2 := time.Now().Unix() var pFile string pFile = fmt.Sprintf("生成完毕共花费%d秒%s", start2-start1, file) fmt.Println(pFile) result := make(map[string]interface{}) result["code"] = 0 result["data"] = path result["msg"] = pFile //内存释放 runtime.GC() debug.FreeOSMemory() if err != nil { fmt.Println(err) result["code"] = -1 result["data"] = "" result["msg"] = err } resA, _ := json.Marshal(result) return string(resA) } func excelRead(path string) string { result := struct { Code int `json:"code""` Msg string `json:"msg"` Date interface{} `json:"data"` }{} xls_os, error := excelize.OpenFile(path) if error != nil { result.Code = 0 result.Msg = error.Error() result.Date = []string{} err_str, _ := json.Marshal(result) return string(err_str) } xls_data := xls_os.GetRows(xls_os.GetSheetName(1)) result.Code = 1 result.Msg = "ok" result.Date = xls_data res_data, _ := json.Marshal(result) return string(res_data) } func main() { runtime.GOMAXPROCS(runtime.NumCPU()) //计划任务 c := cron.New() spec := "0 0 */1 * * ?" //spec20 := "0 */1 * * * ?" //i := 0 //c.AddFunc(spec20, func() { // i++ // log.Println("内存回收cron running:", i) //}) c.AddJob(spec,ExcelDelJob{}) //启动计划任务 c.Start() //关闭着计划任务, 但是不能关闭已经在执行中的任务. defer c.Stop() service := rpc.NewHTTPService() service.AddFunction("excel", excel) service.AddFunction("excelV2", excelV2) service.AddFunction("excelRead", excelRead) beego.Handler("/rpc", service) beego.BConfig.Listen.HTTPPort = 8881 beego.Run() }
“beego excel的导出和读取操作”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。