开发者

Copy paste gridview asp.net

开发者 https://www.devze.com 2023-02-01 18:21 出处:网络
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 co

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


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消