/// <summary>
/// 从Excel读取数据
/// </summary>
/// <param name="filePath">路径</param>
/// <returns>DataSet</returns>
public DataSet ImportFromExcel(string filePath)
{
DataSet ds = new DataSet();
string connString = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + filePath + ";Extended Properties=/"Excel 8.0;HDR=Yes;IMEX=1/"";
DataTable table = OleDbHelper.GetExcelTables(connString);
if(table == null || table.Rows.Count <= 0)
{
return null;
}
foreach(DataRow dr in table.Rows)
{
string cmdText = "select * from [" + dr["TABLE_NAME"].ToString() + "]";
DataTable dt = OleDbHelper.FillDataTable(connString, cmdText);
dt.TableName = dr["TABLE_NAME"].ToString();
ds.Tables.Add(dt);
}
return ds;
}
C#将Execl数据读取到DataSet中或DataTable中
最新推荐文章于 2025-12-03 15:01:35 发布
本文介绍了一种使用C#从Excel文件中读取数据并转换为DataSet的方法。通过连接字符串配置,利用OleDbHelper获取Excel表格内容,并遍历每一行数据执行SQL查询,最终将结果填充到DataSet中。
1702

被折叠的 条评论
为什么被折叠?



