简体   繁体   中英

C#:selecting multiple data from excel sheet to datagridview

I am trying to import selected rows from excel sheet to datagridview. In my search, I learned the OLEDB method is there to import the data. The range of cell I am able to import but if I have a huge excel sheet and want only limited columns to be imported for example c6:c10 and d6:d10. How can I achieve it? my code is as follows:

public void demoread (string FileName, string SheetName, string StartCell, string EndCell)
{
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    System.Data.DataTable dt = new System.Data.DataTable();
    OleDbConnection cn = new OleDbConnection { ConnectionString = ConnectionString(FileName, "No") };

    try
    {
        List<string> jop = new List<string>();
        cn.Open();
         string SelectStatement = string.Format(@"SELECT * FROM [{0}${1}:{2}]", SheetName, StartCell, EndCell);
        //OleDbDataAdapter mydataadapter = new OleDbDataAdapter("Select * from [" + "Salary Sheet" + "$B4:H10 +$c7:c10]", cn);
        //mydataadapter.Fill(dt);
        //dataGridView1.DataSource = dt;
        //string[] data = dt.;

        OleDbCommand cmd = new OleDbCommand { CommandText = SelectStatement,Connection = cn };
        OleDbDataReader dr = cmd.ExecuteReader();

          if (dr.HasRows)
          {
              while (dr.Read())
              {

                for (int i = 0; i < 100; i++)
                {

                    MessageBox.Show(dr.GetString(0));
                    jop[i] = dr.GetString(0).ToString();
                    // MessageBox.Show(dr.GetString(0));

                    // dataGridView1.DataSource = dr;
                }
              }
          }
          else
          {
              //Console.WriteLine("No rows!!!");
          }

    }
    catch(Exception er)
    {
        MessageBox.Show(er.ToString());
    }
}       

in comments i tried with oledbdataadapter command also. i even tried to store the results in particular list so that later own club it as one data table in datagridview.please help me out and even is my approach is correct.

The following will get you what you want, if I understood your problem correctly:

string excelConnectString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\...\\Book2.xlsx;Extended Properties=\"Excel 12.0 Xml;HDR=NO\";";

OleDbConnection objConn = new OleDbConnection(excelConnectString);
OleDbCommand objCmd1 = new OleDbCommand("Select * From [Sheet1$C6:C10]", objConn);
OleDbCommand objCmd2 = new OleDbCommand("Select * From [Sheet1$D6:D10]", objConn);

OleDbDataAdapter objDatAdap = new OleDbDataAdapter();
objDatAdap.SelectCommand = objCmd1;
DataTable dt = new DataTable();
objDatAdap.Fill(dt);

objDatAdap.SelectCommand = objCmd2;
DataTable dt2 = new DataTable();
objDatAdap.Fill(dt2);

dt.Merge(dt2);

If the approach is correct or not really depends on your application and requirements. If it is just reading data then using OleDb is fine. But, if you want to know about the styles used and more, then OleDb falls short and you should probably look into OpenXML.

You can do operations on excel file similar as you do on any database, so you can do query like SELECT TOP 5 * FROM..

I am not sure about reading rows in particular range, But if you want to read initial rows, you can do something like

 string SelectStatement = string.Format(@"SELECT TOP {0} * FROM [{1}${2}:{3}]", numOfTopRowsToRead, SheetName, StartCell, EndCell);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM