Home Ask Login Register

Developers Planet

Your answer is one click away!

Ook February 2016

Error when opening XLSX file made by tealeg xlsx in Go language on Google App Engine

I'm using https://github.com/tealeg/xlsx to generate xlsx file in Go language. The application is running on Google App Engine.

var file *xlsx.File
var sheet *xlsx.Sheet
var row *xlsx.Row
var cell *xlsx.Cell
var err error

file = xlsx.NewFile()
sheet, err = file.AddSheet("TEST")
if err != nil {
row = sheet.AddRow()
cell = row.AddCell()
cell.Value = "I am a cell!"

w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
w.Header().Add("Content-Disposition", "attachment; filename=Test.xlsx")
fmt.Fprint(w, nil)

The variable w is http.ResponseWriter.

I have tested this code and the browser downloaded the xlsx file successfully and I can open it with LibreOffice on Linux 64 bit. However when I tried to open the file with Microsoft Excel 2010 on Windows 7 32 bit, it gave me the following error message:-

Excel found unreadable content in 'Test.xlsx'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes.

Once I clicked yes, it showed the content "I am a cell!" correctly then I clicked on Enable Editing button and it gave me a message:-

Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.

How to fix this problem by making Microsoft Excel opens xlsx files generated by tealeg/xlsx without any error message?


icza February 2016

Your last line is completely unnecessary, and it corrupts the Excel file (XLSX is a zip archive):

fmt.Fprint(w, nil)

This will print the bytes of the "<nil>" string to the web response after you already wrote the content of the Excel file, which I believe you just left there as an accident. Remove that.

Also File.Write() returns an error, also check that, for example:

if err = file.Write(w); err != nil {
    // Handle error, e.g. log it and send back an error response

If the error still persists, it's not AppEngine related. I suggest first try the Excel generation locally, saving it to a file using File.Save(), e.g.:

if err = file.Save("MyXLSXFile.xlsx"); err != nil {
    fmt.Println("Failed to save file:", err)
// If no error, try opening "MyXLSXFile.xlsx" on you computer.

Also note the comment from the home page of the library:

The support for writing XLSX files is currently extremely minimal. It will expand slowly, but in the meantime patches are welcome!

Post Status

Asked in February 2016
Viewed 1,833 times
Voted 10
Answered 1 times


Leave an answer

Quote of the day: live life