I try to create excel file using c# windows form visual studio 2015
i need to create excel file with excel 2007 with extension xlsx
after i click the button and got to path i need to create i not found file found in path
so that what is wrong in code bellow :
- public Boolean AddExcelRow2007(String strFilePath)
- {
- if (!File.Exists(strFilePath)) return false;
- string strExcelConn = "Provider = Microsoft.ACE.OLEDB.12.0;" +
- "Data Source =" +strFilePath + "; Excel 12.0; HDR = YES;";
- OleDbConnection connExcel = new OleDbConnection(strExcelConn);
- OleDbCommand cmdExcel = new OleDbCommand();
- try
- {
- cmdExcel.Connection = connExcel;
-
-
- connExcel.Open();
- DataTable dtExcelSchema;
- dtExcelSchema =
- connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
- connExcel.Close();
- DataRow[] dr =
- dtExcelSchema.Select("TABLE_NAME = 'tblData'");
-
-
- if (dr == null || dr.Length == 0)
- {
- cmdExcel.CommandText = "CREATE TABLE[tblData]" +
- " (ID varchar(10), Name varchar(50));";
- connExcel.Open();
- cmdExcel.ExecuteNonQuery();
- connExcel.Close();
- }
-
-
- cmdExcel.CommandText = "INSERT INTO[tblData]" + "(ID, Name) values('0', 'Start')";
-
-
- connExcel.Open();
- cmdExcel.ExecuteNonQuery();
- connExcel.Close();
- return true;
- }
- catch
- {
- return false;
- }
- finally
- {
- cmdExcel.Dispose();
- connExcel.Dispose();
- }
- }
and after that i call
- private void button6_Click_1(object sender, EventArgs e)
- {
-
- AddExcelRow2007("D:\\Book300.xlsx");
- }
But excel file not created
so that what is wrong in code above