使用c#导入excel doc时,如何防止前导零被剥离?

The*_*tan 6 c# asp.net excel ado.net

我能够连接并读取excel文件没问题.但是在导入具有前导零的zipcodes等数据时,如何防止excel猜测数据类型并在此过程中剥离前导零?

pal*_*rse 6

我相信您必须在连接字符串中设置选项以强制进行文本导入而不是自动检测它.

Provider=Microsoft.ACE.OLEDB.12.0;
    Data Source=c:\path\to\myfile.xlsx;
    Extended Properties=\"Excel 12.0 Xml;IMEX=1\";
Run Code Online (Sandbox Code Playgroud)

您的milage可能会有所不同,具体取决于您安装的版本.IMEX = 1扩展属性告诉Excel将混合数据视为文本.