golang服务器端代码:
func httpHandleExportFunc(w http.ResponseWriter, req *http.Request) {requesturl := req.RequestURI
uid := ""
if index := strings.Index(requesturl, "?"); index > 0 {
query := requesturl[index+1:]
if index = strings.Index(query, "="); index > 0 {
uid = query[index+1:]
}
}
parr := make([]string, 0)
sess_uid := make([]string, 1)
sess_uid[0] = uid
userlistarrmap := DBCallProcs("user_list", parr, sess_uid)
var xlsxfile *xlsx.File
var sheet *xlsx.Sheet
var row *xlsx.Row
var cell *xlsx.Cell
xlsxfile = xlsx.NewFile()
sheet = xlsxfile.AddSheet("sheet1")
row = sheet.AddRow()
cell = row.AddCell()
cell.Value = "用户ID"
cell = row.AddCell()
cell.Value = "密码"
cell = row.AddCell()
cell.Value = "餐厅名称"
cell = row.AddCell()
cell.Value = "餐厅地址"
cell = row.AddCell()
cell.Value = "联系电话"
cell = row.AddCell()
cell.Value = "联系人"
var fileds []string = []string{"uid", "pwd_hash", "title", "address", "telphone", "contact"}
for _, v := range userlistarrmap {
row = sheet.AddRow()
for _, n := range fileds {
cell = row.AddCell()
cell.Value = v[n]
}
}
xlsxfile.Save("exportusers.xlsx")
file, err := os.Open("./exportusers.xlsx")
defer file.Close()
if err != nil {
fmt.Println(err)
}
b, _ := ioutil.ReadAll(file)
w.Header().Add("Content-Disposition", "attachment")
//w.Header().Add("Content-Type", "application/vnd.ms-excel")
w.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
w.Write(b)
}
golang操作xlsx文件的包在:https://github.com/tealeg/xlsx
content-type=application/vnd.ms-excel 这个是发送xls文件的,下面的那个才是用来发送xlsx文件的.