Not sure what to call those exactly, string-ified numbers? Anyways, the cells with the green triangles on them that you would write like so: '3309
Here is my working code...
Reading an excel file's schema, whose sheet is selected later on from a combobox (Here I utilize a backgroundworker thread):
try
{
OleDbcon =
new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + IO_Name +
";Extended Properties='Excel 12.0 Xml; HDR = Yes; IMEX = 1'");
OleDbcon.Open();
SD.DataTable dt = OleDbcon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
OleDbcon.Close();
MaxSchema = dt.Rows.Count;
for (int i = 0; i < dt.Rows.Count; i++)
{
String sheetName = dt.Rows[i]["TABLE_NAME"].ToString();
sheetName = sheetName.Substring(0, sheetName.Length - 1);
cmbSheets.Invoke((MethodInvoker)delegate {
cmbSheets.Items.Add(sheetName);
});
backWorker2.ReportProgress(i);
}
}
catch (Exception ex)
{
MessageBox.Show("Write Excel: " + ex.Message);
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
backWorker2.ReportProgress(100);
}
Lastly, how I bring my data to a datagridview:
private void cmbSheets_SelectedIndexChanged(object sender, EventArgs e)
{
OleDbDataAdapter oledbDa = new OleDbDataAdapter("Select * from [" + cmbSheets.Text + "$]", OleDbcon);
SD.DataTable dt = new SD.DataTable();
oledbDa.Fill(dt);
DGVExcel.DataSource = dt;
lblItem.Text = DGVExcel.RowCount.ToString();
}
However, I noticed that data prefixed with an apostrophe, to make numbers into text, aren't being read at all, and are treated as if they were hidden. How do I make it so that my method can read the string-ified cells?
I realize that this might have to do with the fact that I read the schema of the excel file directly, it might naturally be avoiding certain things like filters or excel specific stuff, including my string-ified cells. My current method of reading it is quite efficient as far as I am concerned, so it would be great if the suggestion merely added to the current method.
Edit my current connection string's extended properties:
";Extended Properties= 'Excel 12.0 Xml;HDR=NO; IMEX = 1;TypeGuessRows=0;ImportMixedTypes=Text';");