在使用Go语言中的tealeg xlsx库在Google App Engine上打开XLSX文件时出现错误。

huangapple go评论91阅读模式
英文:

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

问题

我正在使用https://github.com/tealeg/xlsx在Go语言中生成xlsx文件。该应用程序正在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 {
  fmt.Printf(err.Error())
}
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")
file.Write(w)
fmt.Fprint(w, nil)

变量w是http.ResponseWriter。

我已经测试了这段代码,浏览器成功下载了xlsx文件,并且我可以在Linux 64位上使用LibreOffice打开它。然而,当我尝试在Windows 7 32位上使用Microsoft Excel 2010打开该文件时,它给了我以下错误信息:-

Excel在'Test.xlsx'中发现了无法读取的内容。是否要恢复此工作簿的内容?如果您信任此工作簿的来源,请单击“是”。

一旦我点击了是,它正确显示了内容"I am a cell!",然后我点击了"启用编辑"按钮,它给了我一个消息:-

Excel完成了文件级别的验证和修复。该工作簿的某些部分可能已被修复或丢弃。

如何通过使Microsoft Excel无错误地打开由tealeg/xlsx生成的xlsx文件来解决这个问题?

英文:

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 {
  fmt.Printf(err.Error())
}
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")
file.Write(w)
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?

答案1

得分: 4

你的最后一行是完全不必要的,它会破坏Excel文件(XLSX是一个zip归档文件):

fmt.Fprint(w, nil)

这将在你已经写入Excel文件内容后,将"<nil>"字符串的字节打印到Web响应中,我相信你只是不小心留下了这个。请删除它。

另外,[File.Write()][1]也会返回一个错误,你也需要检查它,例如:

if err = file.Write(w); err != nil {
    // 处理错误,例如记录错误并发送错误响应
}

如果错误仍然存在,那么这与AppEngine无关。我建议首先在本地尝试生成Excel文件,并使用[File.Save()][2]将其保存到文件中,例如:

if err = file.Save("MyXLSXFile.xlsx"); err != nil {
    fmt.Println("保存文件失败:", err)
}
// 如果没有错误,尝试在你的计算机上打开"MyXLSXFile.xlsx"文件。

还请注意库的主页上的注释

> 目前对于写入XLSX文件的支持非常有限它将慢慢扩展但同时欢迎贡献补丁

  [1]: https://godoc.org/github.com/tealeg/xlsx#File.Write
  [2]: https://godoc.org/github.com/tealeg/xlsx#File.Save

<details>
<summary>英文:</summary>

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 `&quot;&lt;nil&gt;&quot;` 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()`][1] 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&#39;s not AppEngine related. I suggest first try the Excel generation locally, saving it to a file using [`File.Save()`][2], e.g.:

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

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

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

  [1]: https://godoc.org/github.com/tealeg/xlsx#File.Write
  [2]: https://godoc.org/github.com/tealeg/xlsx#File.Save

</details>



huangapple
  • 本文由 发表于 2016年2月8日 15:45:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/35264596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定