C#讀取Excel文件,獲取Excel文件中的信息,將獲取的信息以DataSet的方式返回。ide
<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; } }