none
WPF datagrid to excel

    Question

  • He

    Hy I have this code: 

    dgSpelersPerPloeg.SelectAllCells();
                dgSpelersPerPloeg.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
                ApplicationCommands.Copy.Execute(null, dgSpelersPerPloeg);
                String resultaat = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);
                dgSpelersPerPloeg.UnselectAllCells();
                System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Username\test.csv");
                file.WriteLine(resultaat);
                file.Close();
    

    When I open the .csv file as an openoffice.org calc application I get the choice to set the cells separated by kommas.
    So i get this:

    id firstname lastname

    1 Bert    Cunnings 

    3 columns with headers and then 2 rows with content.

    first row are headers
    second row and all the other rows eventually should be data. 

    But when I want to do the same in excel 2010 everything gets pasted
    in the first two cells of the first column. How can I program this so that
    i get the same result in excel as in openoffice.org calc?

    thank you very much!  



    Sunday, January 15, 2012 8:55 PM

Answers

  • Eventually this worked: 

    file.WriteLine(String.Replace(',',';'));

    My list separation sign is ;
    and i had to place the string.replace method as a parameter in file.writeline() 

    Monday, January 16, 2012 5:59 PM

All replies

  • Have you perhaps tried separating the values with tab spaces instead of commas?

    So once you have your csv values call the String.Replace() method to replace the commas with tabs.

     

     

    Sunday, January 15, 2012 10:47 PM
  • I tried to do it like this:

    String.Replace(Convert.ToChar(44), Convert.ToChar(9));

    char integers stand for ASCII 44 = horizontal tab and 9 = ,

    But the output in excel is stil the same where , are used instead of tabs, probably because of this:
      (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);

     

    Monday, January 16, 2012 4:54 PM
  • String .Replace(",",";") isn't working either. 

    or String.Replace(",",@\t") isn't working either.

    Monday, January 16, 2012 5:42 PM
  • Eventually this worked: 

    file.WriteLine(String.Replace(',',';'));

    My list separation sign is ;
    and i had to place the string.replace method as a parameter in file.writeline() 

    Monday, January 16, 2012 5:59 PM