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
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to Export Data to Excel from a DataSet
Jisny Av
Aug 02
2016
Code
533
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
protected
void
btntoExcel_Click(
object
sender, EventArgs e)
{
ds = rs.onemindelayeduser(
"selectall"
, 0,
""
,
""
,
""
,
""
,
""
,
""
);
if
(ds !=
null
)
{
if
(ds.Tables[0].Rows.Count > 0)
{
DataTable dt = ds.Tables[0];
dt.Columns.Remove(
"srno"
);
string
attachment =
"attachment; filename=users.xls"
;
Response.ClearContent();
Response.AddHeader(
"content-disposition"
, attachment);
Response.ContentType =
"application/vnd.ms-excel"
;
string
tab =
""
;
foreach
(DataColumn dc
in
dt.Columns)
{
Response.Write(tab + dc.ColumnName);
tab =
"\t"
;
}
Response.Write(
"\n"
);
int
i;
foreach
(DataRow dr
in
dt.Rows)
{
tab =
""
;
for
(i = 0; i < dt.Columns.Count; i++)
{
Response.Write(tab + dr[i].ToString());
tab =
"\t"
;
}
Response.Write(
"\n"
);
}
Response.End();
}
}
}
.net
c#