Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Storing data in Clipboard to past into Microsoft Excel

Storing data in Clipboard to past into Microsoft Excel

Scheduled Pinned Locked Moved C#
comquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Darryl Borden
    wrote on last edited by
    #1

    I am trying to find a way to take records from a ListView control and move them to the system clipboard so they can be pasted directly into Microsoft Excel. I have found the DataFormat class and see it supports a CommaSeparatedValue format, but I cannot get it to work correctly. I have tried this, just as a test... string s = "1,2,3,4,5,6,7,8,9"; DataObject d = new DataObject(); d.SetData(DataFormats.CommaSeparatedValue,s); Clipboard.SetDataObject(d,false); But when I paste it into Excel I get –§žý;pC¦yVk²ˆû Have any of you ever successfully formatted data, stored it in the Clipboard so it could be successfully pasted into Excel? Darryl Borden Principal IT Analyst darryl.borden@elpaso.com

    J 1 Reply Last reply
    0
    • D Darryl Borden

      I am trying to find a way to take records from a ListView control and move them to the system clipboard so they can be pasted directly into Microsoft Excel. I have found the DataFormat class and see it supports a CommaSeparatedValue format, but I cannot get it to work correctly. I have tried this, just as a test... string s = "1,2,3,4,5,6,7,8,9"; DataObject d = new DataObject(); d.SetData(DataFormats.CommaSeparatedValue,s); Clipboard.SetDataObject(d,false); But when I paste it into Excel I get –§žý;pC¦yVk²ˆû Have any of you ever successfully formatted data, stored it in the Clipboard so it could be successfully pasted into Excel? Darryl Borden Principal IT Analyst darryl.borden@elpaso.com

      J Offline
      J Offline
      John Arlen
      wrote on last edited by
      #2

      The Comma-Delimited Value isn't a string - which is the confusing part. The following code will do what you want. // Place comma-delimited on clipboard string commaText = "1,2,3,4,5,6,7,8,9"; byte[] bytes = System.Text.Encoding.UTF8.GetBytes(commaText); MemoryStream stream = new MemoryStream(bytes); DataObject dataObject = new DataObject(); dataObject.SetData(DataFormats.CommaSeparatedValue, stream); Clipboard.SetDataObject(dataObject, true); // Read comma-delimited from clipboard string values=string.Empty; IDataObject dataObject = Clipboard.GetDataObject(); if(dataObject.GetDataPresent(DataFormats.CommaSeparatedValue)) { StreamReader streamReader = new StreamReader((Stream) dataObject.GetData(DataFormats.CommaSeparatedValue)); values = streamReader.ReadToEnd(); streamReader.Close(); } Console.WriteLine( values );

      D 2 Replies Last reply
      0
      • J John Arlen

        The Comma-Delimited Value isn't a string - which is the confusing part. The following code will do what you want. // Place comma-delimited on clipboard string commaText = "1,2,3,4,5,6,7,8,9"; byte[] bytes = System.Text.Encoding.UTF8.GetBytes(commaText); MemoryStream stream = new MemoryStream(bytes); DataObject dataObject = new DataObject(); dataObject.SetData(DataFormats.CommaSeparatedValue, stream); Clipboard.SetDataObject(dataObject, true); // Read comma-delimited from clipboard string values=string.Empty; IDataObject dataObject = Clipboard.GetDataObject(); if(dataObject.GetDataPresent(DataFormats.CommaSeparatedValue)) { StreamReader streamReader = new StreamReader((Stream) dataObject.GetData(DataFormats.CommaSeparatedValue)); values = streamReader.ReadToEnd(); streamReader.Close(); } Console.WriteLine( values );

        D Offline
        D Offline
        Darryl Borden
        wrote on last edited by
        #3

        I tried out your code and it works great! I have just two questions... 1) where on earth did you dig this up - I have searched all over the place 2) This pastes a single row into Excel - how to I paste multiple rows? Thanks so much for your help! dpb Darryl Borden Principal IT Analyst darryl.borden@elpaso.com

        1 Reply Last reply
        0
        • J John Arlen

          The Comma-Delimited Value isn't a string - which is the confusing part. The following code will do what you want. // Place comma-delimited on clipboard string commaText = "1,2,3,4,5,6,7,8,9"; byte[] bytes = System.Text.Encoding.UTF8.GetBytes(commaText); MemoryStream stream = new MemoryStream(bytes); DataObject dataObject = new DataObject(); dataObject.SetData(DataFormats.CommaSeparatedValue, stream); Clipboard.SetDataObject(dataObject, true); // Read comma-delimited from clipboard string values=string.Empty; IDataObject dataObject = Clipboard.GetDataObject(); if(dataObject.GetDataPresent(DataFormats.CommaSeparatedValue)) { StreamReader streamReader = new StreamReader((Stream) dataObject.GetData(DataFormats.CommaSeparatedValue)); values = streamReader.ReadToEnd(); streamReader.Close(); } Console.WriteLine( values );

          D Offline
          D Offline
          Darryl Borden
          wrote on last edited by
          #4

          Never mind on the 2nd question. If I insert a "\n" character in the string then it wraps to the next line in Excel! "1,2,3,4,5\n6,7,8,9" Thanks again for your help. dpb Darryl Borden Principal IT Analyst darryl.borden@elpaso.com

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups