1

我正在将 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

最后一列是打印 £ 但我只想打印 "£" ,请指导我做错了什么。

4

1 回答 1

0

Excel 不喜欢 UTF-8 编码。尝试将您的编码更改为 Windows-1252。

于 2014-02-28T04:29:08.830 回答