Commit 6527de59 by yemin

no message

parent f9ca8458
...@@ -6,11 +6,12 @@ import ( ...@@ -6,11 +6,12 @@ import (
"github.com/tealeg/xlsx" "github.com/tealeg/xlsx"
"io" "io"
"net/http" "net/http"
"reflect"
"time" "time"
) )
// ToExcel 生成io.ReadSeeker 参数 titleList 为Excel表头,dataList 为数据 // ToExcel 生成io.ReadSeeker 参数 titleList 为Excel表头,dataList 为数据
func ToExcel(titleList []string, dataList [][]interface{}) (content io.ReadSeeker) { func ToExcel(titleList []string, dataList []interface{}) (content io.ReadSeeker) {
// 生成一个新的文件 // 生成一个新的文件
file := xlsx.NewFile() file := xlsx.NewFile()
// 添加sheet页 // 添加sheet页
...@@ -22,10 +23,36 @@ func ToExcel(titleList []string, dataList [][]interface{}) (content io.ReadSeeke ...@@ -22,10 +23,36 @@ func ToExcel(titleList []string, dataList [][]interface{}) (content io.ReadSeeke
cell.Value = v cell.Value = v
} }
// 插入内容 // 插入内容
var types string = ""
var innerTypes string = ""
for _, v := range dataList { for _, v := range dataList {
row := sheet.AddRow() row := sheet.AddRow()
//row.WriteStruct(v, -1) vv := reflect.ValueOf(v)
row.WriteSlice(&v, -1) if innerTypes == "" {
if vv.Kind() == reflect.Ptr {
types = "Ptr"
vv = vv.Elem()
}
if vv.Kind() == reflect.Struct {
innerTypes = "Struct"
} else if vv.Kind() == reflect.Slice {
innerTypes = "Slice"
}
}
if innerTypes == "Struct" {
if types == "" {
row.WriteStruct(&v, -1)
} else {
row.WriteStruct(v, -1)
}
} else if innerTypes == "Slice" {
if types == "" {
row.WriteSlice(&v, -1)
} else {
row.WriteSlice(v, -1)
}
}
} }
var buffer bytes.Buffer var buffer bytes.Buffer
...@@ -43,8 +70,8 @@ func ResponseXls(w http.ResponseWriter, req *http.Request, content io.ReadSeeker ...@@ -43,8 +70,8 @@ func ResponseXls(w http.ResponseWriter, req *http.Request, content io.ReadSeeker
} }
// ExportExcel 生成excel并导出 // ExportExcel 生成excel并导出
// 参数 fileName:文件名;titleList:表头;dataList:数据 // 参数 fileName:文件名;titleList:表头;dataList:数据(数组指针)
func ExportExcel(fileName string, titleList []string, dataList [][]interface{}, w http.ResponseWriter, req *http.Request) { func ExportExcel(fileName string, titleList []string, dataList []interface{}, w http.ResponseWriter, req *http.Request) {
content := ToExcel(titleList, dataList) content := ToExcel(titleList, dataList)
ResponseXls(w, req, content, fileName) ResponseXls(w, req, content, fileName)
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment