if (Path.GetExtension(path).ToUpper() == ".XLS")
    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + ";" + "Extended Properties=Excel 8.0;HDR=Yes;IMEX=1";
else
    strConn = "Provider= Microsoft.Ace.OleDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";

path:Excel完整物理路径
HDR=Yes: 显示第一行包含columnnames
IMEX=1: tells the driver to always read "intermixed" data columns as text

TIP! SQL syntax: "SELECT * FROM [sheet1$]" - i.e. worksheet name followed by a "$" and wrapped in "[" "]" brackets.

如果第一行是数据而不是标题的话, 应该写: "HDR=No;"
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""" 

 

posted on 2016-07-07 12:24  清风暮雨  阅读(1528)  评论(0)    收藏  举报