Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 found file in path
But cannot open it

my code as bellow:
C#
public void CreateSheetIfNotExists()  
       {  
           using (System.Data.OleDb.OleDbConnection databaseConnection = new System.Data.OleDb.OleDbConnection())  
           {  
  
               DataTable schemaTable = default(DataTable);  
  
               databaseConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=D:\\Book310.xlsx;Extended Properties=Excel 12.0;";  
  
               databaseConnection.Open();  
  
               schemaTable = databaseConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] {  
           null,  
           null,  
           "Sheet1$"  
       });  
  
               if (schemaTable.Rows.Count == 0)  
               {  
                   string SQLDDLCommand = "CREATE TABLE [Sheet1] (UserID INTEGER, UserName CHAR(255))";  
                   System.Data.OleDb.OleDbCommand excelCommand = new System.Data.OleDb.OleDbCommand(SQLDDLCommand, databaseConnection);  
                   excelCommand.ExecuteNonQuery();  
                     
               }  
              
               databaseConnection.Close();  
           }  
       }

after that file 310 created but not open
when open file i get this error

excel cannot open the file because the file format or file extension is not valid. verify that the file has not been corrupted and that the file extention matches the format of the file."
How to solve this problem if possible

What I have tried:

When create Excel file using c# cannot open it
Posted
Updated 22-Jan-17 2:03am
v2

It sounds like you need specify Xml:

Extended Properties=\"Excel 12.0 Xml;HDR=YES\"
 
Share this answer
 
v3
 
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