【发布时间】:2014-09-02 07:17:01
【问题描述】:
我正在尝试将数据从 gridview 导出到 excel。我的电脑上安装了office 2010。当我尝试打开 excel 文件时,它给了我错误,即“您尝试打开的文件的格式与文件扩展名 c# 指定的格式不同”。
我的网格视图导出代码:
Protected Sub btnexptoexcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnexptoexcel.Click
Try
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=Complaint_Details.xls")
Response.Charset = ""
Response.ContentType = "application/ms-excel"
Using sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
grd_ComplaintDetails.AllowPaging = False
grd_ComplaintDetails.HeaderRow.BackColor = Color.White
For Each cell As TableCell In grd_ComplaintDetails.HeaderRow.Cells
cell.BackColor = grd_ComplaintDetails.HeaderStyle.BackColor
Next
For Each row As GridViewRow In grd_ComplaintDetails.Rows
row.BackColor = Color.White
For Each cell As TableCell In row.Cells
If row.RowIndex Mod 2 = 0 Then
cell.BackColor = grd_ComplaintDetails.AlternatingRowStyle.BackColor
Else
cell.BackColor = grd_ComplaintDetails.RowStyle.BackColor
End If
cell.CssClass = "textmode"
Next
Next
grd_ComplaintDetails.RenderControl(hw)
Dim style As String = "<style> .textmode { } </style>"
Response.Write(style)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.[End]()
End Using
Catch ex As Exception
div_Msg.InnerText = "Can not generate Excel File"
End Try
End Sub
我的问题是当我打开文件时(在 MSOffice 2003、2007 或 2010 中)它不应该给我文件扩展名错误... 你能告诉我我应该对代码进行哪些更改吗???
【问题讨论】:
标签: asp.net vb.net excel gridview