C#读取Excel文件,获取Excel文件中的信息,将获取的信息以DataSet的方式返回。
<span style="white-space:pre"> </span>/// <summary> /// 读取Excel文件,获得Excel文件信息 /// </summary> /// <param name="excelpath">Excel文件路径</param> /// <returns>网站信息集合</returns> private DataSet LoadExcelToDataset(string excelpath) { try { ///Provider=Microsoft.Jet.OLEDB.4.0;对应Offic2003及其前版本 ///Provider=Microsoft.ACE.OLEDB.12.0;对应Office2007及其后版本,向后兼容 string strConn = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + excelpath + "; Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); string commandStr = "Select * from [sheet1$]"; OleDbDataAdapter da = new OleDbDataAdapter(commandStr, conn); DataSet ds = new DataSet(); da.Fill(ds); conn.Close(); return ds; } catch (Exception e) { MessageBox.Show("Excel表格读取失败,额外信息:" + e.Message); throw; } }