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

Generate xlsx file under Ubuntu 20.04 and Go 1.21.0 but error occured and got nothing inside it when try to open it under windows #1660

Closed
syz9306 opened this issue Sep 14, 2023 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@syz9306
Copy link

syz9306 commented Sep 14, 2023

Hi,

I'm not good at English so I just try to explain this or it may be quite helpful if you can understand Chinese :)

I met a problem when I recently tried to generate a xlsx file. I generated it under Ubuntu 20.04 with Go 1.21.0, then I got this file through http request and save it on my Windows desktop. When I tried to open it, excel notified that an error occured, I followed the notification, tried to repair file by excel itself but got a blank file, with nothing inside it. The notification is below:

image

So I tried to downgrade my go version from 1.21.0 to 1.20.3, then generated again, it works.

  • System infomation is here:
    • Linux: Linux SGW2023 5.15.0-83-generic #92~20.04.1-Ubuntu SMP Mon Aug 21 14:00:49 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
    • Windows: Windowss Professional Workstation 22H2 19045.3208
    • Excel: Microsoft Office 365 Excel version 2307 (inside version16626.220134)
    • VMware: VMware® Workstation 16 Pro 16.2.5 build-20904516

Related file is here, including go.mod go.sum two xlsx files generated reletively, and sample code, I hope it's helpful:

issue.zip

I used excelize both version 2.7.1 and 2.8.0, met the same question. I also paste code here:

func exportPoints(w http.ResponseWriter) {
	var pointsFileName = "sample.xlsx"
	var filename string = strconv.FormatInt(time.Now().Unix(), 10) + "_" + pointsFileName
	var filepath string = "/dev/shm/" + filename

	f0 := excelize.NewFile()
	f0.SetSheetRow("Sheet1", "A1", &[]interface{}{"DeviceName", "DeviceID", "IP", "Port", "SlaveId", "Remark", "Name", "Type", "Endian", "FC", "Address", "Bit", "Cycle", "Formula"})

	pointsList, _ := RedisC.Keys("points/Modbus_TCP/*").Result()
	for index := 0; index < len(pointsList); index++ {

		rval, err := RedisC.HGetAll(pointsList[index]).Result()
		if err != nil {
			zap.L().Error(err.Error())
		} else {
			var data = []interface{}{
				rval["DeviceName"], rval["DeviceID"], rval["IP"], rval["Port"], rval["SlaveId"],
				rval["Remark"], rval["Name"], rval["Type"], rval["Endian"], rval["FC"], rval["Address"], rval["Bit"], rval["Cycle"], rval["Formula"]}
			f0.SetSheetRow("Sheet1", "A"+strconv.Itoa(index+2), &data)

		}
	}

	if err := f0.SaveAs(filepath); err != nil {
		zap.L().Error(err.Error())
	}

	f, err := os.Open(filepath)
	if err != nil {
		zap.L().Error(err.Error())
		return
	}
	defer f.Close()

	fileStat, _ := f.Stat()
	w.Header().Set("Content-Disposition", "attachment; filename="+filename)
	w.Header().Set("Content-Type", "application/octet-stream")
	w.Header().Set("Content-Length", strconv.FormatInt(fileStat.Size(), 10))
	io.Copy(w, f)
}

Thank you :)

Best regards,

@xuri
Copy link
Member

xuri commented Sep 14, 2023

If you are reporting a new issue, make sure that we do not have any duplicates already exist. If it does not work with Go 1.21.0, please reference the issues #1465, #1595, #1603, #1608, #1614, #1619, #1620, #1621, #1623, #1633, #1637, #1641, #1642, #1648, #1651, #1652, #1656 and #1657. I have added notice on the README and documentation website for this.

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