欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

golang實(shí)現(xiàn)瀏覽器導(dǎo)出excel文件功能

 更新時(shí)間:2022年03月25日 11:22:03   作者:峰啊瘋了  
這篇文章主要介紹了golang實(shí)現(xiàn)瀏覽器導(dǎo)出excel文件功能,文章通過golang導(dǎo)出excel文件返回給web,實(shí)現(xiàn)瀏覽器導(dǎo)出excel文件功能,具有一定的參考價(jià)值,需要的小伙伴可以參考一下

1.依賴包

import (
"github.com/tealeg/xlsx"
)

2.示例

func (o *orderController) Export(request *restful.Request, response *restful.Response) {
username := request.Attribute(filters.UserName).(string)

orderService := service.NewOrderService(o.Db)

orders, _ := orderService.ListUserOrders(username)

file := xlsx.NewFile()
sheet, _ := file.AddSheet("訂單信息")

titles := []string{"服務(wù)類型", "訂單號(hào)", "創(chuàng)建時(shí)間", "訂單類型", "訂單金額(元)", "訂單狀態(tài)", "原因"}
row := sheet.AddRow()

var cell *xlsx.Cell
for _, title := range titles {
cell = row.AddCell()
cell.Value = title
}

for _, order := range *orders {
values := []string{
getServiceTypeStr(*order.ServiceType),
order.Id,
order.CreateTime.Format("2006-01-02 15:04:05"),
getOrderTypeStr(*order.OrderType),
"1",
getOrderStatusStr(*order.Status),
order.Reason,
}

row = sheet.AddRow()
for _, value := range values {
cell = row.AddCell()
cell.Value = value
}
}

filename := "訂單信息" + ".xlsx"

response.AddHeader("Content-Type", "application/octet-stream")
response.AddHeader("Content-Disposition", "attachment; filename="+filename)
response.AddHeader("Content-Transfer-Encoding", "binary")

//回寫到web 流媒體 形成下載
_ = file.Write(response.ResponseWriter)
}

3.分析

3.1先根據(jù)需求查詢需要的list對象

#yyds干貨盤點(diǎn)#golang導(dǎo)出excel文件返回給web,實(shí)現(xiàn)瀏覽器導(dǎo)出excel文件功能_github

3.2新建文件,設(shè)置文件名,跟列名

#yyds干貨盤點(diǎn)#golang導(dǎo)出excel文件返回給web,實(shí)現(xiàn)瀏覽器導(dǎo)出excel文件功能_github_02

3.3設(shè)置標(biāo)題單元格

#yyds干貨盤點(diǎn)#golang導(dǎo)出excel文件返回給web,實(shí)現(xiàn)瀏覽器導(dǎo)出excel文件功能_封裝_03

3.4設(shè)置內(nèi)容單元格

#yyds干貨盤點(diǎn)#golang導(dǎo)出excel文件返回給web,實(shí)現(xiàn)瀏覽器導(dǎo)出excel文件功能_流媒體_04

3.5流媒體返回web

#yyds干貨盤點(diǎn)#golang導(dǎo)出excel文件返回給web,實(shí)現(xiàn)瀏覽器導(dǎo)出excel文件功能_流媒體_05

這個(gè)示例是沒有封裝過的,如果想要封裝,可以參考我的另一篇文章,下面是鏈接,喜歡小編的點(diǎn)點(diǎn)關(guān)注

#yyds干貨盤點(diǎn)#golang導(dǎo)出excel文件返回給web,實(shí)現(xiàn)瀏覽器導(dǎo)出excel文件功能_流媒體_06

到此這篇關(guān)于golang實(shí)現(xiàn)瀏覽器導(dǎo)出excel文件功能的文章就介紹到這了,更多相關(guān)golang excel文件導(dǎo)出內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論