摘要:读取Excel文件里面的内容到DataSet
代码:
1 /// <summary> 2 /// 读取Excel文件里面的内容到DataSet 3 /// </summary> 4 /// <param name="Path"></param> 5 /// <param name="ds"></param> 6 private void ExcelToDS(string Path,ref DataSet ds) 7 { 8 string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\""; 9 OleDbConnection conn = new OleDbConnection(strConn); 10 conn.Open(); 11 12 //返回Excel的架构,包括各个sheet表的名称,类型,创建时间和修改时间等 13 DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); 14 15 //包含excel中表名的字符串数组 16 string[] strTableNames = new string[dtSheetName.Rows.Count]; 17 for (int k = 0; k < dtSheetName.Rows.Count; k++) 18 { 19 strTableNames[k] = dtSheetName.Rows[k]["TABLE_NAME"].ToString(); 20 } 21 22 string strExcel = ""; 23 OleDbDataAdapter myCommand = null; 24 strExcel = "select * from [" + strTableNames[0] + "]";//strTableNames[0]取得第一个Sheet中的数据 25 myCommand = new OleDbDataAdapter(strExcel, strConn); 26 ds = new DataSet(); 27 myCommand.Fill(ds); 28 conn.Close(); 29 conn.Dispose(); 30 }
如果您看了本篇博客,觉得对您有所收获,请点击右下角的 [推荐]
如果您想转载本博客,请注明出处
如果您对本文有意见或者建议,欢迎留言
感谢您的阅读,请关注我的后续博客