TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
ahmed salah
NA
530
148.9k
How to create excel file using c# windows form
Jan 21 2017 11:11 AM
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
Reply
Answers (
5
)
Swapping elements in array using c#
HOW TO MAKE EACH TEXTBOX PERFORM SUMMATION