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.

Mehmet FatihPosted Apr 7, 2024, 10:08 AM
I have solved the problem with the following codes:
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);
}
Jaimin ShethiyaPosted Apr 7, 2024, 7:56 AM
Try this one
string removableChars = Regex.Escape(@"$#");
string pattern = "[" + removableChars + "]";
string username = Regex.Replace(username, pattern, "");
Mehmet FatihPosted Apr 7, 2024, 6:17 AM
I solved the unwanted character problems with the code below. But I still see the hidden Excel sheets. How can I prevent this?
for (int i = 0; i < dt.Rows.Count; i++)
{
string sheetName = dt.Rows[i]["TABLE_NAME"].ToString();
sheetName = sheetName.Substring(0, sheetName.Length);
var str = sheetName;
str = new string((from c in str
where char.IsWhiteSpace(c) || char.IsLetterOrDigit(c)
select c
).ToArray());
comboBox1.Items.Add(str);
comboBox1.ValueMember = sheetName;
}
Jaimin ShethiyaPosted Apr 7, 2024, 3:46 AM
Hello,
Please refer below link for remove the hidden characters.
https://stackoverflow.com/questions/15259275/removing-hidden-characters-from-within-strings
Thanks