2 Replies - 825 Views - Last Post: 21 January 2017 - 04:04 PM Rate Topic: -----

#1 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

When create excel file using C# cannot open it

Posted 21 January 2017 - 02:35 PM

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:


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 ?

Is This A Good Question/Topic? 0
  • +

Replies To: When create excel file using C# cannot open it

#2 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: When create excel file using C# cannot open it

Posted 21 January 2017 - 03:11 PM

It should be "Extended Properties=Excel 12.0 Xml".

I tested this using a folder on my C drive.
Was This Post Helpful? 0
  • +
  • -

#3 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

Re: When create excel file using C# cannot open it

Posted 21 January 2017 - 04:04 PM

Thank you very much it solved
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1