CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2010
    Posts
    2

    export datatable to Excel

    Hello,

    I'm currently working on exporting a datatable to an Excel file and I'm having some formatting problems.

    The to export as a CSV would be absolutely fine, but when I download it and try and open it, Excel treats one of the ID columns as a number field (e.g. number is 016542, but excel opens it as 16542). I've tried putting each field in quotes but that hasn't made any difference. It works correctly if you view the CSV in notepad, but not by the time it opens in excel.

    Is there any way I can add formatting to the cell, so it opens as a string? Failing that, how can I export it as an XLS file and add formatting that way?

    Any ideas appreciated. Thanks

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: export datatable to Excel

    Fact 1)
    If you want to create a Excel file, you have to create a real Excel file with C# and the Office assemblies.
    There you can format all cells as you like. Search Google for "create excel file c#"

    Fact 2)
    If you create a CSV, you cannot open it directly in Excel in that way, that the data is inserted into the cells automatically.
    You need to extra import the data in Excel by the menu "Data | Import external data....".

  3. #3
    Join Date
    Jan 2010
    Posts
    2

    Re: export datatable to Excel

    Hi MNovy. Thanks for your reply. I thought that was with the case for Fact 2, but was worth asking.

    Found a great solution after searching Create Excel File. I'd been searching for Export to Excel which doesn't return nearly as good results.

    Anyway, for other who may encounter this, I was able to use this code almost entirely and it worked a treat.



    http://social.msdn.microsoft.com/For...c-c38493c0a664

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured