使用 VB.net 导出后 Excel 文件出现问题
Issue with Excel file after exporting using VB.net
我遇到了一个非常非常奇怪的问题,我不知道是什么问题。
我正在使用以下代码从 RadGrid 导出数据:
Public Sub Export()
Dim GridView1 As New GridView()
GridView1.AllowPaging = False
rgrvResourceTasks.DataBind()
GridView1.DataSource = SqlDataSource_Resources
GridView1.DataBind()
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.Buffer = True
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=ResourceTaskExport.xls")
Response.ContentEncoding = Encoding.UTF8
HttpContext.Current.Response.Charset = ""
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
For i As Integer = 0 To GridView1.Rows.Count - 1
'Apply text style to each Row
GridView1.Rows(i).Attributes.Add("class", "textmode")
Next
GridView1.RenderControl(hw)
'style to format numbers to string
Dim style As String = "<style> .textmode{mso-number-format:\@;}</style>"
HttpContext.Current.Response.Write(style)
HttpContext.Current.Response.Output.Write(sw.ToString())
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.End()
Response.Clear()
'Response.Write("<script language=""javascript"">self.close();</script>")
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "Close_Window", "self.close();", True)
End Sub
文件导出正常。但是当我打开它时,它好像没有打开任何东西(黑色背景,没有显示 headers 等)- 甚至没有空白 sheet。但是,当我用记事本打开它时,有数据(html 格式)。当我复制它并将其粘贴到新的 excel 作品 sheet 中时,它会按应有的方式显示数据。当我将其另存为 html 文件时,它会按应有的方式显示数据。但是导出后excel就是打不开。
可能是什么问题?我正在使用 MS Excel 2016.
这似乎是 Excel 的问题。请参阅这篇文章:HTML files with .XLS not opening outside protected view in Excel 2010, 2013, and 2016
我遇到了一个非常非常奇怪的问题,我不知道是什么问题。
我正在使用以下代码从 RadGrid 导出数据:
Public Sub Export()
Dim GridView1 As New GridView()
GridView1.AllowPaging = False
rgrvResourceTasks.DataBind()
GridView1.DataSource = SqlDataSource_Resources
GridView1.DataBind()
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.Buffer = True
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=ResourceTaskExport.xls")
Response.ContentEncoding = Encoding.UTF8
HttpContext.Current.Response.Charset = ""
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
For i As Integer = 0 To GridView1.Rows.Count - 1
'Apply text style to each Row
GridView1.Rows(i).Attributes.Add("class", "textmode")
Next
GridView1.RenderControl(hw)
'style to format numbers to string
Dim style As String = "<style> .textmode{mso-number-format:\@;}</style>"
HttpContext.Current.Response.Write(style)
HttpContext.Current.Response.Output.Write(sw.ToString())
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.End()
Response.Clear()
'Response.Write("<script language=""javascript"">self.close();</script>")
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "Close_Window", "self.close();", True)
End Sub
文件导出正常。但是当我打开它时,它好像没有打开任何东西(黑色背景,没有显示 headers 等)- 甚至没有空白 sheet。但是,当我用记事本打开它时,有数据(html 格式)。当我复制它并将其粘贴到新的 excel 作品 sheet 中时,它会按应有的方式显示数据。当我将其另存为 html 文件时,它会按应有的方式显示数据。但是导出后excel就是打不开。
可能是什么问题?我正在使用 MS Excel 2016.
这似乎是 Excel 的问题。请参阅这篇文章:HTML files with .XLS not opening outside protected view in Excel 2010, 2013, and 2016