When create Excel file using c# cannot open it-VBForums
Results 1 to 2 of 2

Thread: When create Excel file using c# cannot open it

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    37

    When create Excel file using c# cannot open it

    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:

    Code:
    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 ?

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,309

    Re: When create Excel file using c# cannot open it

    Rather than create a new file each time, create a folder below the app folder, place a blank Excel file there. When needed copy it to the app folder and use it. I've done this in the follow code sample.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.