Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to show an excel file in a datagridview when a button is pressed. Here is the code for the button click event:
C#
private void button1_Click(object sender, EventArgs e)
{
    string fileName = "C:\test.xlsx";
    string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="   + fileName + ";Extended Properties=\"Excel 8.0;HDR=YES;\"";
    OleDbConnection con = new System.Data.OleDb.OleDbConnection(connectionString);
    con.Open();
    OleDbDataAdapter cmd = new System.Data.OleDb.OleDbDataAdapter("select * from [SHEET1$]", con);
    DataSet excelDataSet = new DataSet();
    cmd.Fill(excelDataSet);
    xl.DataSource = excelDataSet.Tables[0];
    con.Close();
}

But always I get an OleDbexception which says "Failure Creating File" pointing a yellow arrow to the 'con.Open();' line
Please help me what can I do.
Posted
Updated 8-May-17 11:11am
v2

1 solution

On the first look of eye, the connection string is wrong.
Read about it at: http://connectionstrings.com/excel-2007[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900