Mehmet Fatih

Mehmet Fatih

  • 855
  • 941
  • 42.5k

Unwanted characters in Excel table names

Apr 6 2024 9:20 PM
private void button2_Click(object sender, EventArgs e)
{
    try
    {
        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;");
            OleDbcon.Open();
            DataTable dt = OleDbcon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            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);
                comboBox1.Items.Add(sheetName);
            }
        }
    }
    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.Text + "]", 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);
    }
}

I'm pulling Excel table names into the combobox. However, it adds characters like $ and # in table names. It also shoots hidden paintings. How can I get rid of hidden tables and these characters?
Note: Circled are hidden tables and unwanted characters.

 


Answers (4)