שתף באמצעות


How to copy a datatable into a clipboard?

Question

Wednesday, December 8, 2010 6:19 PM

I want to copy a datatable into a clipboard so I can paste it into excel and other programs I use, how can I get that to work? If there is no way to get it to work, how can I get a datatable in the clipboard and have it tab delimited?

All replies (2)

Wednesday, December 8, 2010 7:37 PM ✅Answered

Because Excel will paste from a webpage I like to use XElements and LINQ to create an html table to put on the clipboard
    An Example Here

But if you want to use Tab delimitated you could do something like the below example. To use it create a new forms project and paste the below code over the new form1’s code.

Public Class Form1

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim ClipText As String = String.Empty
    Dim ClipRow As String = String.Empty

    'Header row
    For Each Col As DataColumn In MyTable.Columns
      If Not String.IsNullOrEmpty(ClipRow) Then
        ClipRow += ControlChars.Tab
      End If

      ClipRow += Col.ColumnName
    Next

    ClipText += ClipRow + ControlChars.NewLine

    'Data rows
    For Each row As DataRow In MyTable.Rows
      ClipRow = String.Empty

      For Each col As DataColumn In MyTable.Columns
        If Not String.IsNullOrEmpty(ClipRow) Then
          ClipRow += ControlChars.Tab
        End If

        ClipRow += row(col.ColumnName)
      Next

      ClipText += ClipRow + ControlChars.NewLine
    Next

    Clipboard.SetText(ClipText)
  End Sub

  'Below code creates datatable and 
  'populates it with test data for example
  Private _MyTable As DataTable
  Public Property MyTable() As DataTable
    Get
      If _MyTable Is Nothing Then
        Dim newRow As DataRow

        _MyTable = New DataTable("MyTable")

        With _MyTable
          .Columns.Add("PetName", GetType(String))
          .Columns.Add("PetType", GetType(String))

          newRow = .NewRow

          newRow("PetName") = "Tweety"
          newRow("PetType") = "Bird"

          .Rows.Add(newRow)

          newRow = .NewRow

          newRow("PetName") = "Samson"
          newRow("PetType") = "Dog"

          .Rows.Add(newRow)

          newRow = .NewRow

          newRow("PetName") = "Kidders"
          newRow("PetType") = "Cat"

          .Rows.Add(newRow)
        End With
      End If

      Return _MyTable
    End Get
    Set(ByVal value As DataTable)
      _MyTable = CType(value, DataTable)
    End Set
  End Property
End Class

Wednesday, December 8, 2010 7:07 PM

I want to copy a datatable into a clipboard so I can paste it into excel and other programs I use, how can I get that to work? If there is no way to get it to work, how can I get a datatable in the clipboard and have it tab delimited?

I don't know about adding to the clipboard, but you certainly can create a tab delimited .txt file:

 

Dim CSVFile As System.IO.StreamWriter
CSVFile = New System.IO.StreamWriter("C:\MyAdoNetDataTable.txt")

Dim sb As New System.Text.StringBuilder
        sb = New System.Text.StringBuilder
        CSVFile.WriteLine("Field1   Field2   Field3")

Using CSVFile
   For Each r As DataRow In MyDataTable.Rows
    sb = New System.Text.StringBuilder
    sb.Append((r("Field1")) & "   " _
          & (r("Field2")) & "   " _
          & (r("Field3")

    CSVFile.WriteLine(sb.ToString)
  Next
End Using

James Crandall ~ http://javitechnologies.com Spatial Database Solutions