Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I have an excel sheet. After I load the excel sheet on a listbox, I used check box to select some column from the sheet and store it to the arraylist?

C#
public void LoadExcelFields(string strConn)
       {
           try
           {
               parameters.currSheet = cboSelectWorksheet.Text;
               //sheetName = cboSelectWorksheet.Text;
               fileName = txtFileName.Text;

               if (parameters.currSheet != "")
               {
                   OleDbConnection newConn = new OleDbConnection(strConn);
                   newConn.Open();

                   OleDbCommand cmd = newConn.CreateCommand();
                   cmd.CommandText = "SELECT * FROM [" + sheetName + "]";


                   dtReader = cmd.ExecuteReader();
                   if (dtReader.Read())
                   {
                       for (int x = 0; x < dtReader.FieldCount; x++)
                       {
                           lstboxExcelFields.Items.Add(dtReader.GetName(x));
                       }
                   }
                   dtReader.Close();
                   cmd.Dispose();
                   newConn.Close();
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }
Posted
Updated 21-May-12 11:18am
v2
Comments
Sergey Alexandrovich Kryukov 21-May-12 17:35pm    
I fail to see any sense here. Perhaps you think that Excel is a database, don't you? :-)
--SA
Clifford Nelson 21-May-12 17:42pm    
It appears to me that you are reading only the first row of the excel sheet. You will probably be better off using the excel interopt.
SASS_Shooter 23-May-12 11:24am    
Okay you listed what you are doing and what you coded. Now ask the question as to what your problem is and what you've tried. Did you google using oledb to read an excel spreadsheet to see if articles can point out your problem? If not then try that first.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900