Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
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
Posted
Comments
DamithSL 17-Aug-15 1:14am    
what is the issue with current code?
Iris Shing 17-Aug-15 1:27am    
My code can now export the data of 2 table in 1 sheet in 1 excel, but what I want is export 2 table data in 2 sheet in 1 excel

 
Share this answer
 
Comments
Maciej Los 17-Aug-15 15:59pm    
5ed!
DamithSL 17-Aug-15 16:03pm    
Thank you Maciej
Iris Shing 18-Aug-15 3:59am    
Thanks! It works
You can use Microsoft Open XML SDK. For example, please see this CodeProject article: Creating basic Excel workbook with Open XML[^].

See also my past answers: How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].

—SA
 
Share this answer
 
Comments
Maciej Los 17-Aug-15 15:59pm    
5ed!
Sergey Alexandrovich Kryukov 17-Aug-15 16:21pm    
Thank you, Maciej.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900