导出到 excel 文件时,我想将网格视图中的所有超链接转换为纯文本

When exporting to excel file, I want to convert all Hyperlink from gridview into Plain Text

提问人:user1924249 提问时间:6/30/2023 最后编辑:user1924249 更新时间:6/30/2023 访问量:29

问:

我想要什么:导出到excel文件时,我想将gridview中的所有超链接转换为纯文本。

原因:我正在使用这就是为什么它在 excel 中将 gridview 显示为 HTML 的原因。HtmlTextWriter

以下代码将 gridview 导出到 excel 文件。我尝试使用函数,但它不能将超链接转换为文本。tostring()

Protected Sub ExportToExcel(sender As Object, e As EventArgs)
    Try
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=myfile.xls")
        Response.Charset = ""
        Response.ContentType = "application/vnd.ms-excel"

        Using sw As New StringWriter()
            Dim hw As New HtmlTextWriter(sw)

            'To Export all pages
            GridView1.AllowPaging = False
            BindGridData()

            ' header rows
            GridView1.HeaderRow.BackColor = Color.White
            For Each cell As TableCell In GridView1.HeaderRow.Cells
                cell.BackColor = GridView1.HeaderStyle.BackColor
            Next

            ' data rows
            For Each row As GridViewRow In GridView1.Rows
                row.BackColor = Color.White
                For Each cell As TableCell In row.Cells
                    If row.RowIndex Mod 2 = 0 Then
                        cell.BackColor = GridView1.AlternatingRowStyle.BackColor
                    Else
                        cell.BackColor = GridView1.RowStyle.BackColor
                    End If
                    cell.CssClass = "textmode"
                Next
            Next

            GridView1.RenderControl(hw)
            'style to format numbers to string
            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
        ErrorMessage.Text = "error"
        Return
    End Try
End Sub

带列的基本网格视图。 第一个列是超链接

<asp:Gridview id ="GridView1" .... >
   <asp:TemplateField ... >
        <ItemTemplate>
             <asp:hyperlink ... />
        </ItemTemplate>
    </asp:TemplateField>
    ....
</asp:Gridview>
vb.net gridview

评论

0赞 djv 6/30/2023
尝试在字符串前面加上前面,Excel 将强制将其为文本'

答: 暂无答案