Hi, I have google this problem but can't find any helpful answer.
What I want to do is, export 2 table data to 2 sheet in 1 excel.
I have tried
http://mvc4beginner.com/Sample-Code/ImportExportExcelData/MVC4-Export-Data-to-Excel.html[
^]
http://www.aspsnippets.com/Articles/Exporting-Multiple-GridViews-To-Excel-SpreadSheet-in-ASP.Net.aspx[
^]
But the result still not what I want.
Here are my code:
using (DBEntities entity = new DBEntities())
{
string name = "UserInformationMaintenance_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
GridView gv = new GridView();
gv.DataSource = (from a in entity.USER_ACCOUNT
select
new UserAccExcelClass
{
Branch = a.BRANCH,
Domain_ID = a.DOMAIN_ID,
Email = a.EMAIL,
Lotus = a.LOTUS,
Mobile_NO = a.MOBILE_NO,
POST = a.POST,
RANK = a.RANK,
Section = a.SECTION,
Tel_no = a.TEL_NO,
User_ID = a.USER_ID,
User_Name = a.USER_NAME,
User_Status = a.USER_STATUS,
User_Type = a.USER_TYPE
}).ToList();
gv.DataBind();
GridView gv2 = new GridView();
gv2.DataSource = (from a in entity.USER_PROJECT_INFO
select
new UserProjectExcelClass
{
Domain_id = a.DOMAIN_ID,
Project = a.PROJECT,
Role_Id = a.ROLE_ID,
Staff_no = a.STAFF_NO,
Supervisor_Ind = a.SUPERVISOR_IND,
Supervisor_User = a.SUPERVISOR_USER_ID,
Team = a.TEAM
}).ToList();
gv2.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename="+name);
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
HtmlTextWriter htw2 = new HtmlTextWriter(sw);
gv.RenderControl(htw);
gv2.RenderControl(htw2);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
Did LinqToExcel can help? coz I'm using LinqToExcel to import data to db.
Thanks