我将gridview数据导出到Excel文件。它输出正常,但有一个小错误。在gridview中,一列包含不能正常打印的特殊字符。
要导出的代码:
Private Sub exportToExcel(ByVal exportFull As Boolean)
'------------------------------------------------------------------------------------------------------------
Response.Clear()
Response.Buffer = True
Dim fileName As String = "Replacement_" + Convert.ToString(DateTime.Now.Day) + "_" + Convert.ToString(DateTime.Now.Month) + "_" + Convert.ToString(DateTime.Now.Year) + ".xls"
Response.AddHeader("content-disposition", "attachment;filename=" + fileName)
Response.Charset = "UTF-8"
Response.ContentType = "application/vnd.ms-excel"
Dim strWrite As New StringWriter()
Dim htmlWrite As New HtmlTextWriter(strWrite)
Dim htmlfrm As New HtmlForm()
'------------------------------------------------------------------------------------------------------------
'Hide button columns from the grid to avoid them to export to excel file.
grdReplacementList.HeaderRow.Style.Add("background-color", "#FFFFFF")
grdReplacementList.HeaderRow.Cells(1).Style.Add("background-color", "#A4A4A4")
grdReplacementList.HeaderRow.Cells(2).Style.Add("background-color", "#A4A4A4")
grdReplacementList.HeaderRow.Cells(3).Style.Add("background-color", "#A4A4A4")
grdReplacementList.HeaderRow.Cells(4).Style.Add("background-color", "#A4A4A4")
grdReplacementList.HeaderRow.Cells(5).Style.Add("background-color", "#A4A4A4")
grdReplacementList.HeaderRow.Cells(6).Style.Add("background-color", "#A4A4A4")
grdReplacementList.HeaderRow.Cells(7).Style.Add("background-color", "#A4A4A4")
grdReplacementList.HeaderRow.Cells(8).Style.Add("background-color", "#A4A4A4")
grdReplacementList.HeaderRow.Cells(9).Style.Add("background-color", "#A4A4A4")
grdReplacementList.Columns(grdReplacementList.Columns.Count - 1).Visible = False
grdReplacementList.Columns(0).Visible = False
If (Not exportFull) Then
For i As Integer = 0 To grdReplacementList.Rows.Count - 1
Dim row As GridViewRow = grdReplacementList.Rows(i)
If row.RowType = DataControlRowType.DataRow Then
Dim chkProperty As CheckBox = DirectCast(row.Cells(0).FindControl("chkReplacementItem"), CheckBox)
If (Not chkProperty.Checked) Then
row.Visible = False
Else
row.Visible = True
row.BackColor = System.Drawing.Color.White
End If
End If
Next
End If
'------------------------------------------------------------------------------------------------------------
grdReplacementList.Parent.Controls.Add(htmlfrm)
htmlfrm.Attributes("runat") = "server"
htmlfrm.Controls.Add(grdReplacementList)
htmlfrm.RenderControl(htmlWrite)
Response.Write(strWrite.ToString())
Response.Flush()
Response.[End]()
End Sub
#End Region
输出:
Address: Component: Replaced/Installed: Replacement Due: Last Stock Survey: Appointment Status: Appointment Date: Total:
41 St Katherines Court Dodman's Close Bathroom 1950 2034 To be Arranged - £7,000
41 St Katherines Court Dodman's Close Bathroom 1984 2034 To be Arranged - £7,000
41 St Katherines Court Dodman's Close Bathroom 1984 2034 To be Arranged - £7,000
最后一栏是打印£但是我只打印“£”,请指导我做错了。
答案 0 :(得分:0)
Excel不喜欢UTF-8编码。尝试将编码更改为Windows-1252。