62,074
社区成员
发帖
与我相关
我的任务
分享
public static DataSet xlsread(string filepath, string sheetname)
{
DataSet ds = new DataSet();
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
DataTable oleDbSchemaTable = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (oleDbSchemaTable.Rows.Count == 0)
{
throw new Exception(sheetname + "不不存在!!");
}
OleDbDataAdapter adapter = new OleDbDataAdapter("select * from [" + sheetname + "$]", connectionString);
adapter.Fill(ds);
connection.Close();
if (ds.Tables[0].Rows.Count == 0)
{
throw new Exception(sheetname + "无数据!");
}
return ds;
}