dn8

Hi All,

I am trying to use the following code (from another post) to import excel into my application and then dislplay the results in a dataGridview

private void button1_Click(object sender, EventArgs e)
{
DataTable test = getDataFromXLS("c:\temp.xls");
if (test != null)
dataGridView1.DataSource = test;
}


private DataTable getDataFromXLS(string strFilePath)
{
try
{
string strConnectionString = "";
strConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + strFilePath + "; Jet OLEDB:Engine Type=5;" +
"Extended Properties=Excel 8.0;";
OleDbConnection cnCSV = new OleDbConnection(strConnectionString);
cnCSV.Open();
OleDbCommand cmdSelect = new OleDbCommand(@"SELECT * FROM [Sheet1$]", cnCSV);
OleDbDataAdapter daCSV = new OleDbDataAdapter(); daCSV.SelectCommand = cmdSelect;
DataTable dtCSV = new DataTable();
daCSV.Fill(dtCSV);
cnCSV.Close();
daCSV =
null;
return dtCSV;
}
catch (Exception ex)
{
return null;
}
finally { }
}

My problem is, it is doing absolutely nothing .. no errors no nothing ...

Do I need to register/enable OLEDB on my local machine Or is this included/enabled in the .Net framework...

any suggestions would be greatly appreciated...

thanks,

Paul



Re: Windows Forms General Import Excel file into dataGridView

H. Tony

well, it probably throws an exception that's caught and return null. why not debug into the code and see which line causes the problem





Re: Windows Forms General Import Excel file into dataGridView

dn8

shouldn't the line

catch (Exception ex)

throw and exception

I am running in debug and it is not showing any exceptions... however the data table is coming back as null

I changed the code from

if (test != null)

to

if (test == null) {

MessageBox.Show("nada");

}

and I am prompted with "nada"

so that is why I was wondering if the driver needed to be enabled/installed

Paul





Re: Windows Forms General Import Excel file into dataGridView

dn8

Figured it out .. something simple of course.. My "Sheet1" isn't name sheet one at all .. it's named testes!

-P