Excel Reader

Posted on 2011-08-31 15:45  Leon0812  阅读(256)  评论(0编辑  收藏  举报
       string excelFile = Server.MapPath("~/service/data.xlsx");

//string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + excelFile + ";Extended Properties=Excel 8.0;";
string strConn = @"Provider=Microsoft.Ace.OleDb.12.0;Data Source= " + excelFile + ";Extended Properties='Excel 12.0;HDR=No;IMEX=1'";
OleDbDataReader myDataReader
= null;
OleDbConnection myOleDbConnection
= new OleDbConnection(strConn);
OleDbCommand myOleDbCommand
= new OleDbCommand("SELECT * FROM [table1$]", myOleDbConnection);
myOleDbConnection.Open();
myDataReader
= myOleDbCommand.ExecuteReader();
try
{

ModeBiz biz
= new ModeBiz();
Mode model
= new Mode();
string name = string.Empty;
while (myDataReader.Read())
{
model
= new Mode();
name
= myDataReader.GetValue(4).ToString();

biz.Add(model);
}
}
#region Catch
catch (System.Threading.ThreadAbortException ex)
{
ViewBag.ErrMsg
= ex.Message;
}
catch (Exception ex)
{
ViewBag.ErrMsg
= ex.Message;
}
finally
{
myDataReader.Close();
myOleDbConnection.Close();
}
#endregion

Copyright © 2024 Leon0812
Powered by .NET 9.0 on Kubernetes