Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you prevent leading zeros from being stripped when importing an excel doc using c#

I'm able to connect to and read an excel file no problem. But when importing data such as zipcodes that have leading zeros, how do you prevent excel from guessing the datatype and in the process stripping out leading zeros?

like image 406
TheEmirOfGroofunkistan Avatar asked Aug 22 '08 17:08

TheEmirOfGroofunkistan


People also ask

How do I stop excel removing leading zeros?

You can type an apostrophe (') in front of the number, and Excel will treat it as text.


2 Answers

I believe you have to set the option in your connect string to force textual import rather than auto-detecting it.

Provider=Microsoft.ACE.OLEDB.12.0;
    Data Source=c:\path\to\myfile.xlsx;
    Extended Properties=\"Excel 12.0 Xml;IMEX=1\";

Your milage may vary depending on the version you have installed. The IMEX=1 extended property tells Excel to treat intermixed data as text.

like image 63
palehorse Avatar answered Oct 04 '22 04:10

palehorse


Prefix with '

like image 43
Stu Avatar answered Oct 04 '22 02:10

Stu