3
Answers

How to create excel file using c# windows form

Photo of ahmed salah

ahmed salah

8y
314
1
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 not found file found in path
so that what is wrong in code bellow :
  1. public Boolean AddExcelRow2007(String strFilePath)  
  2.        {  
  3.            if (!File.Exists(strFilePath)) return false;  
  4.            string strExcelConn = "Provider = Microsoft.ACE.OLEDB.12.0;" +  
  5.    "Data Source =" +strFilePath + "; Excel 12.0; HDR = YES;";  
  6.            OleDbConnection connExcel = new OleDbConnection(strExcelConn);  
  7.            OleDbCommand cmdExcel = new OleDbCommand();  
  8.            try  
  9.            {  
  10.                cmdExcel.Connection = connExcel;  
  11.   
  12.                //Check if the Sheet Exists  
  13.                connExcel.Open();  
  14.                DataTable dtExcelSchema;  
  15.                dtExcelSchema =  
  16.                connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);  
  17.                connExcel.Close();  
  18.                DataRow[] dr =  
  19.          dtExcelSchema.Select("TABLE_NAME = 'tblData'");  
  20.          
  21.        //if not Create the Sheet  
  22.                if (dr == null || dr.Length == 0)  
  23.                {  
  24.                    cmdExcel.CommandText = "CREATE TABLE[tblData]" +  
  25.            " (ID varchar(10), Name varchar(50));";  
  26.                    connExcel.Open();  
  27.                    cmdExcel.ExecuteNonQuery();  
  28.                    connExcel.Close();  
  29.                }  
  30.   
  31.                //Add New Row to Excel File  
  32.                cmdExcel.CommandText = "INSERT INTO[tblData]" + "(ID, Name) values('0', 'Start')";  
  33.   
  34.   
  35.                connExcel.Open();  
  36.                cmdExcel.ExecuteNonQuery();  
  37.                connExcel.Close();  
  38.                return true;  
  39.            }  
  40.            catch  
  41.            {  
  42.                return false;  
  43.            }  
  44.            finally  
  45.            {  
  46.                cmdExcel.Dispose();  
  47.                connExcel.Dispose();  
  48.            }  
  49.        }  
and after that i call
  1. private void button6_Click_1(object sender, EventArgs e)  
  2.        {  
  3.              
  4.           AddExcelRow2007("D:\\Book300.xlsx");  
  5.        }  
But excel file not created
so that what is wrong in code above 

Answers (3)

1
Photo of Amit Gupta
NA 16.5k 25.7k 8y
Hey Ahmed,
Please review your code and question, as you are asking 'excel file was not created' and in your code, you are returning false, if file doesnot exists
 
if (!File.Exists(strFilePath)) return false;
 
Please check the below link for your reference: 
 http://csharp.net-informations.com/excel/csharp-create-excel.htm
0
Photo of Alex Smith
NA 356 6.8k 8y
Hi, check this link for generating Excel from database.
 
https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Data-Export-/Import-Export-Datatable-to-Excel-from-Database.html
-1
Photo of ahmed salah
NA 510 30.3k 8y
my problem only in creating excel so that i try with another way 
in the code here excel file  created but not open so that can you tell me why not open after created
my code as bellow
  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.   
  8.                databaseConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=D:\\Book310.xlsx;Extended Properties=Excel 12.0;";  
  9.   
  10.                databaseConnection.Open();  
  11.   
  12.                schemaTable = databaseConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] {  
  13.            null,  
  14.            null,  
  15.            "Sheet1$"  
  16.        });  
  17.   
  18.                if (schemaTable.Rows.Count == 0)  
  19.                {  
  20.                    string SQLDDLCommand = "CREATE TABLE [Sheet1] (UserID INTEGER, UserName CHAR(255))";  
  21.                    System.Data.OleDb.OleDbCommand excelCommand = new System.Data.OleDb.OleDbCommand(SQLDDLCommand, databaseConnection);  
  22.                    excelCommand.ExecuteNonQuery();  
  23.                      
  24.                }  
  25.               
  26.                databaseConnection.Close();  
  27.            }  
  28.        }  
after that file 310 created but not open
when open file i get this error