2013-10-24 23 views
1

我正在將gridview數據導出到Excel文件。它出口很好,但有一個小錯誤。在gridview中,一列包含特殊字符,它不能正常打印。無效的字符導出gridview到excel文件

代碼出口:

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

Excel不喜歡UTF-8編碼。嘗試將您的編碼更改爲Windows-1252。