Commit a0778d5d by yemin

no message

parents
go get -u github.com/tealeg/xlsx
\ No newline at end of file
package excelv1
import (
"bytes"
"fmt"
"github.com/tealeg/xlsx"
"io"
"net/http"
"time"
)
// ToExcel 生成io.ReadSeeker 参数 titleList 为Excel表头,dataList 为数据
func ToExcel(titleList []string, dataList [][]interface{}) (content io.ReadSeeker) {
// 生成一个新的文件
file := xlsx.NewFile()
// 添加sheet页
sheet, _ := file.AddSheet("Sheet1")
// 插入表头
titleRow := sheet.AddRow()
for _, v := range titleList {
cell := titleRow.AddCell()
cell.Value = v
}
// 插入内容
for _, v := range dataList {
row := sheet.AddRow()
//row.WriteStruct(v, -1)
row.WriteSlice(v, -1)
}
var buffer bytes.Buffer
_ = file.Write(&buffer)
content = bytes.NewReader(buffer.Bytes())
return
}
// ResponseXls 向前端返回Excel文件
// 参数 content 为上面生成的io.ReadSeeker, fileName 为返回前端的文件名
func ResponseXls(w http.ResponseWriter, req *http.Request, content io.ReadSeeker, fileName string) {
w.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
w.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
http.ServeContent(w, req, fileName, time.Now(), content)
}
// ExportExcel 生成excel并导出
// 参数 fileName:文件名;titleList:表头;dataList:数据
func ExportExcel(fileName string, titleList []string, dataList [][]interface{}, w http.ResponseWriter, req *http.Request) {
content := ToExcel(titleList, dataList)
ResponseXls(w, req, content, fileName)
}
module git.168cad.top/excel-sdk
go 1.18
require github.com/tealeg/xlsx v1.0.5 // indirect
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE=
github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
package main
func main() {
}
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