public static DataTable GetExcelToDataTableBySheet(string FileFullPath, string SheetName)
{
//string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + FileFullPath(fup.postedfile.filename) + ";Extended Properties='Excel 8.0; HDR=NO; IMEX=1'"; //此连接只能操作Excel2007之前(.xls)文件
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + fup.postedfile.filename+ ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'"; //此连接可以操作.xls与.xlsx文件
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter odda = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}]", SheetName), conn); //("select * from [Sheet1$]", conn);
odda.Fill(ds, SheetName);
conn.Close();
return ds.Tables[0];
}
fup.postedfile.filename如果获取的是文件名,则
把工具~Internet选项~安全~Internet~自定义级别~其它~将文件上载到服务器时包含本地目录路径:设为启用