c# 读取Excel到datable

直接代码:
using System.Data.OleDb;

string fileName = myFile.FileName;
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(fileName) + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";
                OleDbConnection conn = new OleDbConnection(connStr);
                OleDbCommand comm = new OleDbCommand();
                comm.Connection = conn;
                comm.CommandText = "select * from [" + excelName + "$]";
                OleDbDataAdapter ada = new OleDbDataAdapter();
                DataSet ds = new DataSet();
                ada.SelectCommand = comm;
                ada.Fill(ds, excelName);
                DataTable dt = ds.Tables[excelName];


HDR属性决定读不读列名。
 “IMEX=1;”通知驱动程序始终将“互混”数据列作为文本读取。

你可能感兴趣的:(Excel)