我有一个将数据从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 = 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;
}
}
我尝试从我的一张excel表中导出数据,这些数据包含900行。该函数只获得253行。但我想要所有的行。这个功能有什么问题?你能救我吗?感谢。