标签:导出 位置 head pen ace list val param 文件的
package test import ( "github.com/360EntSecGroup-Skylar/excelize" ) /** * 导出Excel表格 * @param name {string} 导出的表名 * @param header {[]string} 表头key,导出后显示的顺序 * @param headerKV {map[string]string} 表头、数据kv对照 * @param data {[]map[string]interface{}} 数据集合 * @return err {error} 异常 */ func ExportExcelSkylar(name string, header []string, headerKV map[string]string, data []map[string]interface{}) (fileName string, err error) { f := excelize.NewFile() // Create a new sheet index := f.NewSheet("Sheet1") headerValue := make([]string, 0) for _, v := range header { headerValue = append(headerValue, headerKV[v]) } f.SetSheetRow("Sheet1", "A1", &headerValue) var rowValue []interface{} //行数据对象 for i, v := range data { rowValue = make([]interface{}, 0) rowNum := strconv.Itoa(i + 2) //表中的行顺序是从1开始的; A1是表头,A2才是数据的开始行 for _, key := range header { rowValue = append(rowValue, v[key]) } f.SetSheetRow("Sheet1", "A"+rowNum, &rowValue) } f.SetActiveSheet(index) // Save xlsx file by the given path. fileName = name + ".xlsx" //文件名称 fileNamePath := "/" + fileName //保存文件的位置 err = f.SaveAs(fileNamePath) return } //返回文件名称 func DownExcel() string{ header := []string{"name", "age"} headerKV := map[string]string{ "name": "姓名", "age": "年龄", } name := "学生列表" list := make([]map[string]interface{}, 0) for i:=0;i<10;i++{ item := map[string]interface{}{ "name": "name-"+i, "age": 10+i, } list = append(list, item) } //假设10条数据
fileName, err := ExportExcelSkylar(name, header, headerKV, list) if err != nil { return ""
}
return fileName
}
标签:导出 位置 head pen ace list val param 文件的
原文地址:https://www.cnblogs.com/ashion89/p/13796991.html