码迷,mamicode.com
首页 > 其他好文 > 详细

把Excel里面的数据变成datatable

时间:2015-04-24 18:07:47      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

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();
}
}

把Excel里面的数据变成datatable

标签:

原文地址:http://www.cnblogs.com/-maomao/p/4453638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!