Tech
Forums
Jobs
Books
Events
Live
Interviews
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
3
Reply
How to create excel file using c# windows form
ahmed salah
Jan 21 2017 11:11 AM
287
Reply
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 :
public
Boolean AddExcelRow2007(String strFilePath)
{
if
(!File.Exists(strFilePath))
return
false
;
string
strExcelConn =
"Provider = Microsoft.ACE.OLEDB.12.0;"
+
"Data Source ="
+strFilePath +
"; Excel 12.0; HDR = YES;"
;
OleDbConnection connExcel =
new
OleDbConnection(strExcelConn);
OleDbCommand cmdExcel =
new
OleDbCommand();
try
{
cmdExcel.Connection = connExcel;
//Check if the Sheet Exists
connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema =
connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
null
);
connExcel.Close();
DataRow[] dr =
dtExcelSchema.Select(
"TABLE_NAME = 'tblData'"
);
//if not Create the Sheet
if
(dr ==
null
|| dr.Length == 0)
{
cmdExcel.CommandText =
"CREATE TABLE[tblData]"
+
" (ID varchar(10), Name varchar(50));"
;
connExcel.Open();
cmdExcel.ExecuteNonQuery();
connExcel.Close();
}
//Add New Row to Excel File
cmdExcel.CommandText =
"INSERT INTO[tblData]"
+
"(ID, Name) values('0', 'Start')"
;
connExcel.Open();
cmdExcel.ExecuteNonQuery();
connExcel.Close();
return
true
;
}
catch
{
return
false
;
}
finally
{
cmdExcel.Dispose();
connExcel.Dispose();
}
}
and after that i call
private
void
button6_Click_1(
object
sender, EventArgs e)
{
AddExcelRow2007(
"D:\\Book300.xlsx"
);
}
But excel file not created
so that what is wrong in code above
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
3
)
Next Recommended Forum
Swapping elements in array using c#
HOW TO MAKE EACH TEXTBOX PERFORM SUMMATION