复制粘贴gridview asp.net

发布于 2024-10-09 13:30:58 字数 123 浏览 0 评论 0原文

如何从 gridview (asp.net) 复制(右键单击)某些列并将其粘贴到 Excel 中?当我尝试时,我得到一行包含粘贴在 Excel 中的所有数据,并且它应该与 gridview 位于相同的列和行中。

谢谢

How do you copy (rightclick) some columns from a gridview (asp.net) and paste it in excel? When I try I get one line with all the data pasted in excel, and it should be in the same columns and rows as the gridview.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

↘人皮目录ツ 2024-10-16 13:30:58

好的,谢谢。我制作了一个按钮并将其导出到 csv 文件。下面的代码完成了这项工作。

  Private Sub ExportGrid(ByVal gridname As String)
    Dim kolonner As Integer
    If gridname.ToString = GridView1.ToString Then
        kolonner = 5
    Else
        kolonner = 10
    End If

    'Lav DT 
    Dim dtUse As DataTable = New DataTable
    If gridname = "GridView1" Then
        dtUse = dt2.Copy()
    Else
        '  dtUse = dt2.Copy
    End If

    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv")
    ' Response.Charset = "System.Text.Encoding.Default"
    Response.Charset = ""
    Response.ContentEncoding = System.Text.Encoding.Default
    'text/html
    'Response.ContentType = "System.Text.Encoding.Default"
    Response.ContentType = "text/html"

    Dim sb As New StringBuilder()
    For Each clm As DataColumn In dtUse.Columns
        sb.Append(clm.ColumnName.ToString + ";"c)
    Next
    'append new line
    sb.Append(vbCr & vbLf)

    Dim tt As String = sb.ToString

    For Each row As DataRow In dtUse.Rows
        For Each CLM2 As DataColumn In dtUse.Columns
            'add separator
            sb.Append(row(CLM2).ToString + ";"c)
        Next
        'append new line
        sb.Append(vbCr & vbLf)
    Next
    Response.Output.Write(sb.ToString())
    Response.Flush()
    Response.End()

End Sub

Ok, thanks. I made a button and exported it to a csv file. The code below does the job.

  Private Sub ExportGrid(ByVal gridname As String)
    Dim kolonner As Integer
    If gridname.ToString = GridView1.ToString Then
        kolonner = 5
    Else
        kolonner = 10
    End If

    'Lav DT 
    Dim dtUse As DataTable = New DataTable
    If gridname = "GridView1" Then
        dtUse = dt2.Copy()
    Else
        '  dtUse = dt2.Copy
    End If

    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv")
    ' Response.Charset = "System.Text.Encoding.Default"
    Response.Charset = ""
    Response.ContentEncoding = System.Text.Encoding.Default
    'text/html
    'Response.ContentType = "System.Text.Encoding.Default"
    Response.ContentType = "text/html"

    Dim sb As New StringBuilder()
    For Each clm As DataColumn In dtUse.Columns
        sb.Append(clm.ColumnName.ToString + ";"c)
    Next
    'append new line
    sb.Append(vbCr & vbLf)

    Dim tt As String = sb.ToString

    For Each row As DataRow In dtUse.Rows
        For Each CLM2 As DataColumn In dtUse.Columns
            'add separator
            sb.Append(row(CLM2).ToString + ";"c)
        Next
        'append new line
        sb.Append(vbCr & vbLf)
    Next
    Response.Output.Write(sb.ToString())
    Response.Flush()
    Response.End()

End Sub
倚栏听风 2024-10-16 13:30:58

为什么不尝试使用自定义上下文菜单(例如“导出到 Excel”)并为其编写代码。
在您的代码中,获取一行中的所有值并用逗号分隔它们(制作 csv 格式的行),然后在每行后面附加一个 "\r\n"

数据将变成以下格式。

col1 , col2 , col3 \r\n
data1, data2 data3 \r\n
data1, data2 data3 \r\n
data1, data2 data3 \r\n
data1, data2 data3 \r\n

这样,它就会被正确格式化以导出到 Excel 中。只需将最终字符串添加到剪贴板即可。

Why don't you try Using a custom context Menu like "Export to excel", and write code for it.
In your your code take all the Values in a row and separate them with a comma (Making a csv formatted row) and then append each Row with a "\r\n".

The data will become in following format.

col1 , col2 , col3 \r\n
data1, data2 data3 \r\n
data1, data2 data3 \r\n
data1, data2 data3 \r\n
data1, data2 data3 \r\n

That way it will be properly formatted to be exported in Excel. Just add the Final string to Clipboard.

娇柔作态 2024-10-16 13:30:58

人们不会复制粘贴整个 GridView。
它既乏味又容易出错。
作为程序员,你有责任为他们做到这一点。
放置一个漂亮的导出到 GridView 顶部的 Excel 按钮

People dont copy paste a whole GridView.
It tedious and error prone.
Its your duty as a programmer to do that for them.
Place a nice Export to Excel Button on top of the GridView for that

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文