C# 后台程序,用ADO.net 如何读取EXCEL中数据到DataSet
DataSource中路径格式是D:/try01/123.xls,D:\try01\123.xls,还是D:\\try01\\123.xls?...
Data Source 中路径格式是D:/try01/123.xls , D:\try01\123.xls, 还是D:\\try01\\123.xls?
展开
2个回答
展开全部
publicDataSet ImportExcel(string strFileName) //参数为文件路径+文件名。就是DataSource
{
if (strFileName != "")
{
string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFileName + ";Extended Properties=Excel 8.0";
string sql = "select * from [Sheet1$]";
OleDbDataAdapter da = newOleDbDataAdapter(sql, conn);
DataSet ds = newDataSet();
try
{
da.Fill(ds, "datatable");
}
catch
{
}
return ds;
}
else
{
return null;
}
}
{
if (strFileName != "")
{
string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFileName + ";Extended Properties=Excel 8.0";
string sql = "select * from [Sheet1$]";
OleDbDataAdapter da = newOleDbDataAdapter(sql, conn);
DataSet ds = newDataSet();
try
{
da.Fill(ds, "datatable");
}
catch
{
}
return ds;
}
else
{
return null;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询