Home » excel » c# – Using excel import wizard through code?

c# – Using excel import wizard through code?

Posted by: admin April 23, 2020 Leave a comment


I am creating a Generate Report button for some data in my application.

The report will just generate a CSV file, which I then automatically launch in excel.

Problem is with one of the data fields, is text and has leading zeroes (which I need to keep). When the file is automatically opened in excel, excel automatically trims these. Is there anyway I can get it so that it opens with the data in the format I want it.

I know you achieve this normally by opening the CSV file from within excel and can do the import wizard. So one potential solution would be to auto open the import wizard when the app launches this file. I don’t know if this is possible.

Thanks for reading.

How to&Answers:

Did you try quoting? i.e. "firstfield","second field","00032","etc"


You might want to take a look into generating HTML instead. Using specific CSS information and additional attributes, you may be able to encode just enough information on the data that Excel does not mangle with it.

The basic idea is to tag your table data with a specific css class, and have that css class define, e.g., a number format:

   .myStyle { mso-number-format:"\@" }
    <td class="myStyle">000345</td>

Save such a file as .xls, and it opens in Excel. Excel will complain about html in a xls file, but open it nevertheless.

You can find a download for the documentation for this on MSDN:

Microsoft Office HTML and XML Reference