Click here to Skip to main content
15,911,306 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am new to C # programming. I'm trying to pass multiple ranges of a file in Excel for a dataviewgrid. I've tried to use UNION ALL and multiples tables, but I'm not succeeding. I would like the output to be something like this:

Excel:
A B C D E F G H
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
2 2 2 2 2 2 2 2

Output:
A B C D E F G H
1 2 3 4 5 6 7 8
2 2 2 2 2 2 2 2

Thank U
My code is below:

C#
}
private void button2_Click(object sender, EventArgs e)
    {
        string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        path = Path.Combine(path, "MINERALOGIA.xlsm");

        string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "C:\\Users\\Daniel\\Desktop\\MINERALOGIA.xlsm" + ";Extended Properties=\"Excel 12.0 Xml;HDR=NO;ImpoertMixedTypes=Text;TypeGuessRows=0\";";
        
              
        OleDbConnection conn = new OleDbConnection(ConnectionString);

        DataSet ds = new DataSet();

        DataTable dt1 = ds.Tables.Add("set1");
        using (OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [Entrada$A1:Q1];", conn))
        {
            adapter.Fill(dt1);
        }

        DataTable dt2 = ds.Tables.Add("set2");
        using (OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [Entrada$A2:Q2];", conn))
        {
            adapter.Fill(dt2);
        }

               
        try
        {
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.ReadOnly = true;
            conn.Open();
            dataGridView1.DataSource = ds.Tables[0];
            dt2 = ds.Tables[0];
            dt2.Rows.Add(ds.Tables[1]);
            dt2.AcceptChanges();


   catch(Exception ex)
   {
       MessageBox.Show(ex.ToString());
   }
   finally
   {
       conn.Close();
   
}
}
Posted
Comments
DamithSL 5-Feb-15 13:12pm    
have you tried with sql like
SELECT * FROM [Entrada$A1:Q1] UNION ALL SELECT * FROM [Entrada$A2:Q2]
Member 11360962 5-Feb-15 17:22pm    
Yes, but the output is just Entrada$A1:Q1

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