我有一个将数据从excel导出到数据集的功能,如下所示,
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;
}
}
我试图从我的一张有 900 行的 Excel 表中导出数据。该函数仅获得 253 行。但我想要所有的行。功能有什么问题?你能帮帮我吗?谢谢。