从vb.net中的响应代码下载时,Excel文件已损坏

时间:2017-06-09 11:15:49

标签: asp.net excel vb.net

我遇到一个奇怪的错误,我在vb.net中有一个代码,基本上只是根据我创建的数据集下载一个excel。一切似乎工作正常,文件下载,但当我尝试打开文件时,文件已损坏。

我认为代码中存在问题或者我错过了一些东西,这是我正在使用的代码:

   dgGrid.DataSource = DS2.DefaultView   ' This is where i do the Databinding
        dgGrid.HeaderStyle.Font.Bold = True
        dgGrid.DataBind()

        ExcelFile = name & ".xls"

        Response.Clear()
        Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", ExcelFile))
        'Response.ContentType = "application/ms-excel"
        Response.Charset = "utf-8"
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        'Response.ContentType = "application/octet-stream"  


        dgGrid.RenderControl(htwWriter)
        Response.ContentEncoding = System.Text.Encoding.Unicode
        Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble())

        Dim style As String = "<style>.textmode{mso-number-format:\@;}</style>"
        Response.Write(style)
        Response.Output.Write(stwWriter.ToString())
        Response.End()
        'Response.ContentType = "application/octet-stream"
        Response.Close()
        Response.Flush()
        'Response.Write()
        'Response.End()

谢谢,任何帮助将不胜感激:(

0 个答案:

没有答案
相关问题