Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker download: 使用docker容器启动go程序,程序中包含导出excel的接口。访问接口导出空文件。 #1656

Closed
poscergx opened this issue Sep 11, 2023 · 6 comments
Labels
duplicate This issue or pull request already exists

Comments

@poscergx
Copy link

	r.GET("/json", func(c *gin.Context) {
		f := excelize.NewFile()
		sheet1 := "eat food"
		f.SetSheetName("Sheet1", sheet1)
		f.SetCellValue(sheet1, "C3", "are you eat")
		f.SetCellValue(sheet1, "D4", "are you hungry")
		buffer, _ := f.WriteToBuffer()
		c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, "eat.xlsx"))
		c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
		a, _ := c.Writer.Write(buffer.Bytes())
        }

代码如上,访问json接口下载名为eat.xlsx的文档,下载xlsx的文档文件名和sheet1名称符合预期,但是内容为空。请教一下原因为何?
以下是dockerfile内容

FROM golang:latest
ENV GOPROXY https://goproxy.cn,direct
WORKDIR $GOPATH/home/test
COPY . $GOPATH/home/test
RUN go build .
EXPOSE 9090
ENTRYPOINT ["./m"]

以下是docker的构建和启动命令:
docker build -t gin-test .
docker run --privileged -d --name gin_test -p 9090:9090 gin-test

@xuri
Copy link
Member

xuri commented Sep 11, 2023

Thanks for your issue. Which version of the Go language and Excelize library are you using? Could you follow the issue template and provides more details about this?

@xuri xuri added the needs more info This issue can't reproduce, need more info label Sep 11, 2023
@poscergx
Copy link
Author

补充:
go version go1.19.4 linux/amd64
github.com/gin-gonic/gin v1.9.1
github.com/xuri/excelize/v2 v2.8.0

@xuri
Copy link
Member

xuri commented Sep 11, 2023

Thanks for your feedback, could you show us a complete, standalone example program or reproducible demo?

@poscergx
Copy link
Author

poscergx commented Sep 11, 2023

这是一个demo,所以项目下只有main.go一个文件
main.go完整代码如下

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/xuri/excelize/v2"
)

func main() {
	r := gin.Default()

	r.GET("/json", func(c *gin.Context) {
		f := excelize.NewFile()

		sheet1 := "eat food"
		f.SetSheetName("Sheet1", sheet1)
		f.SetCellValue(sheet1, "C3", "are you eat")
		f.SetCellValue(sheet1, "D4", "are you hungry")

		buffer, _ := f.WriteToBuffer()
		c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, "eat.xlsx"))
		c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
		a, _ := c.Writer.Write(buffer.Bytes())

	})

	r.Run(":9090")

}

@xuri
Copy link
Member

xuri commented Sep 11, 2023

I've tested with your code and it works well. Please make sure you're not using Go 1.21.0. Also reference issues #1465, #1595, #1603, #1608, #1614, #1619, #1620, #1621, #1623, #1633, #1637, #1641, #1642, #1648, #1651 and #1652.

@poscergx
Copy link
Author

Your suggestion was helpful, thank you👍

@xuri xuri added duplicate This issue or pull request already exists and removed needs more info This issue can't reproduce, need more info labels Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants