J John Sutor Nov 26, 2004 #1 Can anyone send me a code snippet on how to connect to an Excel spreadsheet and then read it in a row at a time? John S
Can anyone send me a code snippet on how to connect to an Excel spreadsheet and then read it in a row at a time? John S
S Shiva Nov 26, 2004 #2 These might help: http://www.codeproject.com/csharp/Excel_using_OLEDB.asp http://www.c-sharpcorner.com/winforms/ExcelReadMG.asp Can anyone send me a code snippet on how to connect to an Excel spreadsheet and then read it in a row at a time? John S
These might help: http://www.codeproject.com/csharp/Excel_using_OLEDB.asp http://www.c-sharpcorner.com/winforms/ExcelReadMG.asp Can anyone send me a code snippet on how to connect to an Excel spreadsheet and then read it in a row at a time? John S
D Darrin J. Olson Nov 26, 2004 #3 These two methods should get you started. They will fill a dataset with the excel data. Hope that helps. -Darrin private DataSet GetExcelData(System.IO.FileInfo importFile) { OleDbConnection _xlConn = new OleDbConnection(); try { DataSet _xlDataSet = new DataSet(); _xlConn = GetOleConnectionForExcel(importFile.FullName); OleDbDataAdapter _xlAdapt = new OleDbDataAdapter(); _xlConn.Open(); DataTable _schemaTable = _xlConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); foreach(DataRow _dr in _schemaTable.Rows) { _xlAdapt.SelectCommand = new OleDbCommand("Select * from [" + _dr["TABLE_NAME"].ToString() + "]", _xlConn); _xlAdapt.Fill(_xlDataSet, _dr["TABLE_NAME"].ToString()); } return _xlDataSet; } catch(SqlException _sqlException) { throw new Exception("Error getting Excel data.", _sqlException); } catch(Exception _exception) { throw new Exception("Error getting Excel data.", _exception); } finally { if(_xlConn.State != ConnectionState.Closed) _xlConn.Close(); } } private OleDbConnection GetOleConnectionForExcel(string filePath) { string _cnString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";" + "Extended Properties=Excel 8.0;IMEX=1"; return new OleDbConnection(_cnString); }
These two methods should get you started. They will fill a dataset with the excel data. Hope that helps. -Darrin private DataSet GetExcelData(System.IO.FileInfo importFile) { OleDbConnection _xlConn = new OleDbConnection(); try { DataSet _xlDataSet = new DataSet(); _xlConn = GetOleConnectionForExcel(importFile.FullName); OleDbDataAdapter _xlAdapt = new OleDbDataAdapter(); _xlConn.Open(); DataTable _schemaTable = _xlConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); foreach(DataRow _dr in _schemaTable.Rows) { _xlAdapt.SelectCommand = new OleDbCommand("Select * from [" + _dr["TABLE_NAME"].ToString() + "]", _xlConn); _xlAdapt.Fill(_xlDataSet, _dr["TABLE_NAME"].ToString()); } return _xlDataSet; } catch(SqlException _sqlException) { throw new Exception("Error getting Excel data.", _sqlException); } catch(Exception _exception) { throw new Exception("Error getting Excel data.", _exception); } finally { if(_xlConn.State != ConnectionState.Closed) _xlConn.Close(); } } private OleDbConnection GetOleConnectionForExcel(string filePath) { string _cnString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";" + "Extended Properties=Excel 8.0;IMEX=1"; return new OleDbConnection(_cnString); }