ahmed salah

ahmed salah

  • 1.6k
  • 530
  • 122.9k

How to create Excel file 31055.xlsx in application path

Jan 23 2017 6:55 PM
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
  1. public void CreateSheetIfNotExists()  
  2.        {  
  3.            using (System.Data.OleDb.OleDbConnection databaseConnection = new System.Data.OleDb.OleDbConnection())  
  4.            {  
  5.   
  6.                DataTable schemaTable = default(DataTable);  
  7.                databaseConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=D:\\Book3105.xlsx;Mode=ReadWrite;Extended Properties=Excel 12.0 Xml;";  
  8.   
  9.                databaseConnection.Open();  
  10.   
  11.                schemaTable = databaseConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] {  
  12.            null,  
  13.            null,  
  14.            "Sheet1$"  
  15.        });  
  16.   
  17.                if (schemaTable.Rows.Count == 0)  
  18.                {  
  19.                    string SQLDDLCommand = "CREATE TABLE [Sheet1] (UserID INTEGER, UserName CHAR(255))";  
  20.                    System.Data.OleDb.OleDbCommand excelCommand = new System.Data.OleDb.OleDbCommand(SQLDDLCommand, databaseConnection);  
  21.                    excelCommand.ExecuteNonQuery();  
  22.                      
  23.                }  
  24.               
  25.                databaseConnection.Close();  
  26.            }  
  27.        }  
 

Answers (1)