The below code explains how you can fetch data from EXcel to a data adapter . Here I have used a OLEDB Connection
public void GetExcelData(string strFilename) { try { dataset dsExcelData; dsExcelData.Clear(); string sCurrentSheet = "[Sheet1$]"; string strConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sFilename + ";Extended Properties='Excel 8.0;HDR=Yes'"; OleDbConnection oConnection = new OleDbConnection(strConnectionString); oConnection.Open(); OleDbCommand oCommand = new OleDbCommand("Select [MAKE BOX], [L],[W],[D],[ID],[SEQ],[PART],[PARTDESCRIPTION] from " + sCurrentSheet, oConnection); OleDbDataAdapter oDataAdapter = new OleDbDataAdapter(oCommand); oDataAdapter.Fill(dsExcelData);// dataset oConnection.Close(); } catch (Exception) {
} }
// binding to a datagridview control
BindingSource bsDataBind = new BindingSource(); bsDataBind.DataSource = dsExcelData; datagridview1.DataSource = bsDataBind;
or you can directly bind
datagridview1.datasourse= dsExcelData;
|
| Author: Filip 20 Aug 2009 | Member Level: Silver Points : 1 |
Nice code but OLEDB is slow. Try using GemBox spreadsheet component. It's much faster then OLEDB and easier to use. Here's are some examples how to read, write, export or convert spreadsheet files.
|