查询上传数据时出错!Microsoft Jet 数据库引擎找不到对象'a$'。请确定对象是否存在,并正确地写出它的名称和路径。
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Data.SqlClient;public class BaseFunction
{ /// <summary> /// 取得上传文件的数据集 /// </summary> /// <param name="strUpFileExtName">上传文件扩展名</param> /// <param name="strUpFileName">上传文件名</param> /// <param name="strUpPath">上传的路径</param> /// <param name="strSplitChar">拆分字符或Excel表名</param> /// <param name="aryField">要导入的数据字段数组</param> /// <returns></returns> public static DataSet GetOleDbData(string strUpFileExtName, string strUpFileName, string strUpPath, string strSplitChar, string[] aryField, bool preview)
{ string strDBDatabaseName; string DBSQL; if (preview)
{ DBSQL = "select top 10 * from {0}"; } else
{ DBSQL = "select * from {0}"; } string oleConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties={1};"; switch (strUpFileExtName)
{ case ".xls":
{ strDBDatabaseName = strUpPath + strUpFileName;//excel数据库完整文件名作为数据源 DBSQL = String.Format(DBSQL, "[" + strUpFileName + "$]");//"SELECT * FROM ["+tbExcel.Text+"$]"; //硬性规定只能在Sheet1工作表中存放数据 oleConnStr = String.Format(oleConnStr, strDBDatabaseName, "Excel 8.0"); break; } } //通过ole数据提供者获得数据 OleDbConnection oleConnection = new OleDbConnection(oleConnStr); OleDbDataAdapter dbdat = new OleDbDataAdapter(DBSQL, oleConnection); DataSet upDs = new DataSet();
try
{ dbdat.Fill(upDs, strUpFileName);
if (upDs.Tables[strUpFileName].Columns.Count > aryField.Length)
{ //上传的字段不合法 throw new Exception("上传的文件字段不符合规则!列数为" + upDs.Tables[strUpFileName].Columns.Count.ToString()); }
} catch (Exception ex)
{ throw new Exception("查询上传数据时出错!" + ex.Message); } finally
{ if (dbdat != null)
{ dbdat.Dispose(); dbdat = null; } if (oleConnection != null)
{ oleConnection.Dispose(); oleConnection = null; } } return upDs; }}我把新建的Excel另存为4.0格式的,导入没问题!但是新建的Excel不转换就抛异常!即使里面的内容一样也不行!
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Data.SqlClient;public class BaseFunction
{ /// <summary> /// 取得上传文件的数据集 /// </summary> /// <param name="strUpFileExtName">上传文件扩展名</param> /// <param name="strUpFileName">上传文件名</param> /// <param name="strUpPath">上传的路径</param> /// <param name="strSplitChar">拆分字符或Excel表名</param> /// <param name="aryField">要导入的数据字段数组</param> /// <returns></returns> public static DataSet GetOleDbData(string strUpFileExtName, string strUpFileName, string strUpPath, string strSplitChar, string[] aryField, bool preview)
{ string strDBDatabaseName; string DBSQL; if (preview)
{ DBSQL = "select top 10 * from {0}"; } else
{ DBSQL = "select * from {0}"; } string oleConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties={1};"; switch (strUpFileExtName)
{ case ".xls":
{ strDBDatabaseName = strUpPath + strUpFileName;//excel数据库完整文件名作为数据源 DBSQL = String.Format(DBSQL, "[" + strUpFileName + "$]");//"SELECT * FROM ["+tbExcel.Text+"$]"; //硬性规定只能在Sheet1工作表中存放数据 oleConnStr = String.Format(oleConnStr, strDBDatabaseName, "Excel 8.0"); break; } } //通过ole数据提供者获得数据 OleDbConnection oleConnection = new OleDbConnection(oleConnStr); OleDbDataAdapter dbdat = new OleDbDataAdapter(DBSQL, oleConnection); DataSet upDs = new DataSet();
try
{ dbdat.Fill(upDs, strUpFileName);
if (upDs.Tables[strUpFileName].Columns.Count > aryField.Length)
{ //上传的字段不合法 throw new Exception("上传的文件字段不符合规则!列数为" + upDs.Tables[strUpFileName].Columns.Count.ToString()); }
} catch (Exception ex)
{ throw new Exception("查询上传数据时出错!" + ex.Message); } finally
{ if (dbdat != null)
{ dbdat.Dispose(); dbdat = null; } if (oleConnection != null)
{ oleConnection.Dispose(); oleConnection = null; } } return upDs; }}我把新建的Excel另存为4.0格式的,导入没问题!但是新建的Excel不转换就抛异常!即使里面的内容一样也不行!
?