///
/// 读取Excel文档
///
/// 文件名称
/// 返回一个数据集
public DataSet ExcelToDS(string Path)
{
string strConn = string.Empty;
//= "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
if (Path.ToLower().IndexOf(".xlsx") > 0)
{
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + Path + "';Extended Properties='Excel 12.0;HDR=YES'";
}
if (Path.ToLower().IndexOf(".xls") > 0 && Path.EndsWith("xls"))
{
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + Path + "';Extended Properties='Excel 8.0;HDR=YES;'";
}
/// 读取Excel文档
///
/// 文件名称
/// 返回一个数据集
public DataSet ExcelToDS(string Path)
{
string strConn = string.Empty;
//= "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
if (Path.ToLower().IndexOf(".xlsx") > 0)
{
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + Path + "';Extended Properties='Excel 12.0;HDR=YES'";
}
if (Path.ToLower().IndexOf(".xls") > 0 && Path.EndsWith("xls"))
{
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + Path + "';Extended Properties='Excel 8.0;HDR=YES;'";
}
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds, "table1");
conn.Close();
return ds;
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22392018/viewspace-751570/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/22392018/viewspace-751570/