Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to paste CSV data to Windows Clipboard with C#
    text
    copied!<h2>What I'm trying to accomplish</h2> <ul> <li>My app generates some tabular data</li> <li>I want the user to be able to launch Excel and click "paste" to place the data as cells in Excel</li> <li>Windows accepts a format called "CommaSeparatedValue" that is used with it's APIs so this seems possible</li> <li>Putting raw text on the clipboard works, but trying to use this format does not</li> <li>NOTE: I can correctly retrieve CSV data from the clipboard, my problem is about pasting CSV data to the clipboard.</li> </ul> <h2>What I have tried that isn't working</h2> <p>Clipboard.SetText()</p> <pre><code>System.Windows.Forms.Clipboard.SetText( "1,2,3,4\n5,6,7,8", System.Windows.Forms.TextDataFormat.CommaSeparatedValue ); </code></pre> <p>Clipboard.SetData()</p> <pre><code>System.Windows.Forms.Clipboard.SetData( System.Windows.Forms.DataFormats.CommaSeparatedValue, "1,2,3,4\n5,6,7,8", ); </code></pre> <p>In both cases something is placed on the clipboard, but when pasted into Excel it shows up as one cell of garbarge text: "–§žý;pC¦yVk²ˆû"</p> <h2>Update 1: Workaround using SetText()</h2> <p>As BFree's answer shows <strong>SetText</strong> with <strong>TextDataFormat</strong> serves as a workaround</p> <pre><code>System.Windows.Forms.Clipboard.SetText( "1\t2\t3\t4\n5\t6\t7\t8", System.Windows.Forms.TextDataFormat.Text ); </code></pre> <p>I have tried this and confirm that now pasting into Excel and Word works correctly. In each case it pastes as a table with cells instead of plaintext.</p> <p>Still curious why CommaSeparatedValue is <em>not</em> working.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload