如何将db数据导出到多个excel工作表 [英] How to export db data to mulitple excel worksheet

查看:119
本文介绍了如何将db数据导出到多个excel工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有谷歌这个问题,但找不到任何有用的答案。



我想要做的是,将2个表格数据输出到2张1 excel。



我尝试过 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 [ ^ ]



但结果仍然不是我想要的。



这是我的代码:



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();

}





LinqToExcel可以提供帮助吗?因为我正在使用LinqToExcel将数据导入数据库。

谢谢



Did LinqToExcel can help? coz I'm using LinqToExcel to import data to db.
Thanks

推荐答案

检查将多个数据网格导出到单个Excel文件上的不同工作表 [ ^ ]

您可以使用EPPlus .net库
check Export multiple datagrids to different worksheets on a single excel file[^]
you can use EPPlus .net library


您可以使用Microsoft Open XML SDK。例如,请参阅此CodeProject文章:使用Open XML创建基本Excel工作簿 [ ^ ]。



另见我过去的答案:如何从MS Visual Studio 2010中的添加引用添加microsoft excel 15.0对象库 [ ^ ]。



-SA
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


这篇关于如何将db数据导出到多个excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆