Mehmet Fatih

Mehmet Fatih

  • 851
  • 941
  • 42.4k

Heading reading problem in Excel Sheet

Apr 7 2024 12:46 PM

I'm going crazy please help. I am pulling the data from Excel into datagrdiview. However, when the data under the SCORE heading is empty, the title appears. I couldn't understand why it didn't appear when it was full.

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                comboBox1.SelectedIndex = -1;
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "Excel Files|*.xls;*.xlsx";
                openFileDialog.ShowDialog();

                if (!string.IsNullOrEmpty(openFileDialog.FileName))
                {

                    OleDbcon = new OleDbConnection(@"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=" + openFileDialog.FileName + ";Extended Properties='Excel 12.0;IMEX=1;'");
                    OleDbcon.Open();
                    DataTable dt = OleDbcon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[] { null, null, null, "TABLE" });
                    OleDbcon.Close();

                    comboBox1.Items.Clear();

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {                      
                        string sheetName = dt.Rows[i]["TABLE_NAME"].ToString();
                        sheetName = sheetName.Substring(0, sheetName.Length + i++);
                        i++;

                        var str = sheetName;
                        str = new string((from c in str
                                          where char.IsWhiteSpace(c) || char.IsLetterOrDigit(c)
                                          select c
                            ).ToArray());

                        comboBox1.DisplayMember = sheetName;
                        comboBox1.Items.Add(str);

                    }

                }
            }
            catch (Exception ex)
            {
                // Hata alirsak ekrana bastiriyoruz.
                MessageBox.Show("Dosya Seçilmedi!Hata :" + ex.Message);
            }
        }
 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
     try
     {
         OleDbDataAdapter oledbDa = new OleDbDataAdapter("Select * from [" + comboBox1.DisplayMember + "]", OleDbcon);
         DataTable dt = new DataTable();
         oledbDa.Fill(dt);
         dataGridView1.DataSource = dt;
     }
     catch (Exception err)
     {
         MessageBox.Show("Hata! " + err.Message, "Hata Olustu.Dosya Seçilmedi!", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }

 

/forums/uploadfile/56c12d/04072024123540PM/Excel-Mark.xlsx

 


Answers (4)