日期:2014-05-18  浏览次数:20651 次

ASP.NET如何操作EXCEL文件
C# code

 string Sql = "select * from Sheet1";
        OleDbConnection ExcelConn = new OleDbConnection(ConfigurationManager.AppSettings["ExcelConnString"]);
        ExcelConn.Open();
        OleDbCommand ExcelCmd = new OleDbCommand(Sql, ExcelConn);
        OleDbDataReader ExcelDr = ExcelCmd.ExecuteReader();
        while (ExcelDr.Read())
        {
            Response.Write(ExcelDr["title"].ToString());
            Response.End();
        }
        ExcelDr.Close();
        ExcelConn.Close();






我这样读XLS表有么有错?
ConfigurationManager.AppSettings["ExcelConnString"]=“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Bak.xls;Extended Properties=Excel 8.0”;

执行

Microsoft Jet 数据库引擎找不到对象'Sheet1'。请确定对象是否存在,并正确地写出它的名称和路径。


Microsoft Jet 数据库引擎找不到对象'Sheet1'。请确定对象是否存在,并正确地写出它的名称和路径。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.Data.OleDb.OleDbException: Microsoft Jet 数据库引擎找不到对象'Sheet1'。请确定对象是否存在,并正确地写出它的名称和路径。

源错误: 


行 31: ExcelConn.Open();
行 32: OleDbCommand ExcelCmd = new OleDbCommand(Sql, ExcelConn);
行 33: OleDbDataReader ExcelDr = ExcelCmd.ExecuteReader();
行 34: while (ExcelDr.Read())
行 35: {
 

源文件: e:\WebSite\Test\XlsToSqlServer.aspx.cs 行: 33 

堆栈跟踪: 


[OleDbException (0x80040e37): Microsoft Jet 数据库引擎找不到对象'Sheet1'。请确定对象是否存在,并正确地写出它的名称和路径。]
  System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) +267
  System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) +192
  System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) +48
  System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +106
  System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) +111
  System.Data.OleDb.OleDbCommand.ExecuteReader() +6
  XlsToSqlServer.Page_Load(Object sender, EventArgs e) in e:\WebSite\Test\XlsToSqlServer.aspx.cs:33
  System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
  System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
  System.Web.UI.Control.OnLoad(EventArgs e) +99
  System.Web.UI.Control.LoadRecursive() +47
  System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061

 


--------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.42; ASP.NET 版本:2.0.50727.42

------解决方案--------------------
string Sql = "select * from [Sheet1$]";

好像是这样的。
------解决方案--------------------
类似Word的这个
http://dotnet.aspx.cc/ShowDetail.aspx?id=13C874E4-7FC7-4FD1-8CF6-DE9EF4469A9C
------解决方案--------------------
探讨
string Sql = "select * from [Sheet1$]";

好像是这样的。

------解决方案--------------------
C# code
using System.Data.odbc 

OdbcConnection MyConn = new OdbcConnection();
MyConn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};DBQ=" + Server.MapPath("/Excel.xls");
OdbcComm