将数据从excel导出到数据集的函数中有什么错误?
我有一个将数据从excel导出到Dataset的功能,如下所示将数据从excel导出到数据集的函数中有什么错误?,excel,oledb,Excel,Oledb,我有一个将数据从excel导出到Dataset的功能,如下所示 public DataSet GetDataFromExcel(string filePath) { string strConn; strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";" + "Extended Properties=Excel 8.0;"; DataTable dt =
public DataSet GetDataFromExcel(string filePath)
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + filePath + ";" +
"Extended Properties=Excel 8.0;";
DataTable dt = new DataTable();
dt = null;
using (OleDbConnection oleDB = new OleDbConnection(strConn))
{
oleDB.Open();
dt = oleDB.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
return null;
ArrayList arr = new ArrayList();
//ListItemCollection items = new ListItemCollection();
int i = 0;
for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
{
string excelSheetName;
string lastCharacter = "";
excelSheetName = dt.Rows[rowIndex]["TABLE_NAME"].ToString();
excelSheetName = excelSheetName.Replace("'", "");
lastCharacter = excelSheetName.Substring(excelSheetName.Length - 1, 1);
if (lastCharacter == "$")
{
arr.Add(dt.Rows[rowIndex]["TABLE_NAME"].ToString());
//items.Add(dt.Rows[rowIndex]["TABLE_NAME"].ToString());
}
}
//if (items.Count > 1)
if (arr.Count > 1)
return null;
string sName;
string query;
//sName = items[0].ToString();
sName = arr[0].ToString();
sName = sName.Replace("'", "");
sName = sName.Replace("$", "");
query = "";
query = String.Format("select * from [{0}$]", sName);
OleDbDataAdapter da = new OleDbDataAdapter(query, strConn);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
公共数据集GetDataFromExcel(字符串文件路径)
{
字符串结构;
strConn=“Provider=Microsoft.Jet.OLEDB.4.0;”+
“数据源=“+filePath+”;”+
“扩展属性=Excel 8.0;”;
DataTable dt=新的DataTable();
dt=零;
使用(oleDB连接oleDB=新oleDB连接(strConn))
{
oleDB.Open();
dt=oleDB.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);
如果(dt==null)
返回null;
ArrayList arr=新的ArrayList();
//ListItemCollection items=新建ListItemCollection();
int i=0;
对于(int rowIndex=0;rowIndexMicrosoft.ACE.OLEDB.12.0
提供程序。Jet提供商有一个硬限制