Public ActionResult Index ()
{
Viewbag.message = "Welcome to ASP. mvc!";
DataTable dt = new DataTable ();
DT = GetData (@ "D:\Files\20150424151220 workbook 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);//Opening the database
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 ();
}
}
Turn the data in Excel into a DataTable