title: Get CSV Data from Clipboard (pasted from Excel) that contains accented characters
body: <h2>SCENARIO</h2>
  
  <ul>
  <li>My users will copy cells from Excel (thus placing it into the clipboard)</li>
  <li>And my application will retrieve those cells from the clipboard</li>
  </ul>
  
  <h2>THE PROBLEM</h2>
  
  <ul>
  <li>My code retrieves the CSV format from the clipboard</li>
  <li>However, the if the original Excel content contains characters like ä (a with umlaut) then retrieved CSV string doesn't have the correct characters (ä ends up showing as a "square" for me)</li>
  <li>In comparison, if my code retrieves the Unicode text format from the clipboard everything works fine: the ä is preserved in the string retrieved from the clipboard</li>
  </ul>
  
  <h2>SOURCE CODE - ORIGINAL - WITH THE PROBLEM</h2>
  
  <pre><code>[STAThread]
  static void Main(string[] args)
  {
      var fmt_csv = System.Windows.Forms.DataFormats.CommaSeparatedValue;
  
      // read the CSV
      var dataobject = System.Windows.Forms.Clipboard.GetDataObject();
      var stream = (System.IO.Stream)dataobject.GetData(fmt_csv);
      var enc = new System.Text.UTF8Encoding();
      var reader = new System.IO.StreamReader(stream,enc);
      string data_csv = reader.ReadToEnd();
  
      // read the unicode string
      string data_string = System.Windows.Forms.Clipboard.GetText();
  
  
  
  }
  </code></pre>
  
  <h2>THE RESULTS WHEN RUNNING THE SAMPLE CODE</h2>
  
  <ul>
  <li>Repro steps: Enter some text in Excel (I used the word "doppelgänger" plus some numbers) and simply hit Ctrl-C to copy it to the clipboard and then run the code above.</li>
  <li>data_csv is set to "doppelg�nger,1\r\n2,3\r\n\0"</li>
  <li>data_string is set to "doppelgänger\t1\r\n2\t3\r\n"</li>
  </ul>
  
  <h2>QUESTION</h2>
  
  <ul>
  <li>What can I do to get the correct characters? </li>
  </ul>
  
  <h2>COMMENTS</h2>
  
  <ul>
  <li>Yes, I know I could workaround this problem by using the Unicode text. But I actually want to understand what is going on with the CSV</li>
  <li>using or not using the UTF-8 encoding when retrieving the stream makes no difference in the results</li>
  </ul>
  
  <h2>THE ANSWER</h2>
  
  <p>After looking at the comments, and paying close attention to what Excel was putting on the clipboard for CSV, it seemed reasonable that Excel might be placing the contents using an "legacy" encoding instead of UTF-8. So I tried the using the Windows 1252 codepage as the encoding and it worked. See the code below</p>
  
  <h2>SOURCE CODE - WITH THE ANSWER</h2>
  
  <pre><code>[STAThread]
  static void Main(string[] args)
  {
      var fmt_csv = System.Windows.Forms.DataFormats.CommaSeparatedValue;
  
      //read the CSV
      var dataobject = System.Windows.Forms.Clipboard.GetDataObject();
      var stream = (System.IO.Stream)dataobject.GetData(fmt_csv);
      var enc = System.Text.Encoding.GetEncoding(1252);
      var reader = new System.IO.StreamReader(stream,enc);
      string data_csv= reader.ReadToEnd();
  
      //read the Unicode String
      string data_string = System.Windows.Forms.Clipboard.GetText();
  }
  </code></pre>