Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I work in windows form c# visual studio 2015

I have excel file xlsx and name is 3105.xlsx and i need to create it in application

path .

how to create excel file in application path

my code

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:\\Book3105.xlsx;Mode=ReadWrite;Extended Properties=Excel 12.0 Xml;";  
  
               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();  
           }  
       }


What I have tried:

How to create Excel file 31055.xlsx in application path
Posted
Updated 23-Jan-17 18:34pm
Comments
[no name] 23-Jan-17 19:08pm    
Show the code where you have tried to create the file and tell us how the code did not do what you expect.
Dave Kreskowiak 23-Jan-17 22:10pm    
You do realize that the Program Files folder (and everything under it) is ReadOnly for normal users, correct? Normal users will not be able to run this code and expect it to work because you're trying to create and write to a file in a location that is ReadOnly for those users.

1 solution

This question was asked a few days ago, is it you ?
Quote:
How to create Excel file 31055.xlsx in application path

Windows actively prevent this from happening for security reasons,it is to prevent malware from altering apps. No app store its own configuration in the app path, there is a reason.
Only supervisors can do this, normal users can't.

Why do you absolutely need to store this Excel file in app directory ?
 
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