public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
DataTable dt = new DataTable();
dt = GetData(@"D:\Files\20150424151220工作簿1.xls", "Sheet1$").Tables[0];
return View();
}
public DataSet GetData(string filename, string workTableName)
{
DataSet ds;
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Extended Properties=Excel 8.0;" + "data source=" + filename;
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT * FROM [" + workTableName + "]";
try
{
Open(myConn);//打开数据库
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
ds = new DataSet();
myCommand.Fill(ds);
myCommand.Dispose();
return ds;
}
catch (Exception ex)
{
return null;
}
finally
{
Close(myConn);
}
}
private void Open(OleDbConnection con)
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
}
private void Close(OleDbConnection con)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}