导出模型数据到Excel MVC [英] export model data to excel mvc

查看:125
本文介绍了导出模型数据到Excel MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @model SA.MarketingManager.WebClient.Areas.Reports.Models.ViewReportModel
@ {
    布局=〜/ CustomViews / lennoxcap.net /共享/ _DealerLayout.cshtml
}
@using(Html.BeginForm())
{
    < D​​IV的风格=宽度:100%; FONT-SIZE:9px; ID =结果>
        < H3风格=背景颜色:#CC0000;颜色:#FFF; FONT-SIZE:1.5em;>
            客户调查报告< / H3 GT&;
        @if(Model.Report.Count()大于0)
        {
            <表ID =SurveyResponseWIDTH =100%的cellpadding =0CELLSPACING =0>
                <&THEAD GT;
                    < TR类=头>
                        &所述; TD>
                            经销商#
                        < / TD>
                        &所述; TD>
                            区
                        < / TD>
                        &所述; TD>
                            TM
                        < / TD>
                        &所述; TD>
                            调查code
                        < / TD>
                        &所述; TD>
                            名字
                        < / TD>
                        &所述; TD>
                            姓
                        < / TD>
                        &所述; TD>
                            地址
                        < / TD>
                        &所述; TD>
                            市
                        < / TD>
                        &所述; TD>
                            州
                        < / TD>
                        &所述; TD>
                            邮政code
                        < / TD>
                        &所述; TD>
                            电话
                        < / TD>
                        &所述; TD>
                            发送邮件
                        < / TD>
                        &所述; TD>
                            邮件已发送
                        < / TD>
                    < / TR>
                < / THEAD>
                <&TBODY GT;
                    @ {布尔备用= FALSE;}
                    @foreach(在Model.Report VAR TR)
                    {
                        < TR @(候补级=替代:)>
                            &所述; TD>
                                @ tr.DealerId
                            < / TD>
                            &所述; TD>
                                @ tr.District
                            < / TD>
                            < TD> @ tr.TM
                            < / TD>
                            < TD> @ tr.Survey code
                            < / TD>
                            < TD> @ tr.FirstName
                            < / TD>
                            < TD> @ tr.LastName
                            < / TD>
                            < TD> @ tr.Address
                            < / TD>
                            < TD> @ tr.City
                            < / TD>
                            < TD> @ tr.State
                            < / TD>
                            < TD> @ tr.Postal code
                            < / TD>
                            < TD> @ tr.Phone
                            < / TD>
                            < TD> @ tr.MailSent
                            < / TD>
                            < TD> @ tr.DateCompleted
                            < / TD>
                        < / TR>
                           备用=备用!;
                    }
                < / TBODY>
            < /表>
        }
        其他
        {
            <文字和GT;没有记录显示< /文字和GT;
        }
        &所述p为H.;
            <输入类型=提交ID =提交值=导出数据级=premierSubmitButton/>
       &所述; / P>
    < / DIV>

控制器

 公众的ActionResult CustomerReport()
    {
        ViewReportModel模式=新ViewReportModel();
            在SessionHandler.CurrentContext.LennoxSurveyResponses VAR的查询=(从u
                            将C在u.Survey code SessionHandler.CurrentContext.MailingListEntries等于c.Survey code
                            加入CL在SessionHandler.CurrentContext.MailingLists上c.MailingListId等于cl.MailingListId
                            加入CH在SessionHandler.CurrentContext.Channels上cl.ChannelId等于ch.ChannelId
                            加入CG在SessionHandler.CurrentContext.ChannelGroups上ch.ChannelGroupId等于cg.ChannelGroupId
                            //让CON = ch.Contacts.FirstOrDefault()
                            其中,ch.OrganizationId == 8
                            //&功放;&安培; con.ContactTypeId == ContactTypeConstants.TMId
                            //选择新ReportDetails(){SurveyResponse = U,MailingListEntry = C,海峡= cl.Channel,跟= CON});
                            选择新ReportDetails {DealerId = ch.ExternalChannelId,
                                                        区= ch.ChannelAMSData.District,
                                                        TM = cg.Name,
                                                        调查code = u.Survey code,
                                                        名字= c.FirstName,
                                                        姓氏= c.LastName,
                                                        地址= c.Address1,
                                                        市= c.City,
                                                        状态= c.State,
                                                        邮政code = c.Postal code,
                                                        电子邮件= c.Email,
                                                        手机= c.Phone,
                                                        MailSent = c.LetterDate,
                                                        DateCompleted = c.EmailDate});
               model.Report =查询;
               返回视图(CustomerReport模型);
    }
    [HttpPost]
    公众的ActionResult CustomerReport(ViewReportModel模型)
    {
        在SessionHandler.CurrentContext.LennoxSurveyResponses VAR的查询=(从u
                     将C在u.Survey code SessionHandler.CurrentContext.MailingListEntries等于c.Survey code
                     加入CL在SessionHandler.CurrentContext.MailingLists上c.MailingListId等于cl.MailingListId
                     加入CH在SessionHandler.CurrentContext.Channels上cl.ChannelId等于ch.ChannelId
                     加入CG在SessionHandler.CurrentContext.ChannelGroups上ch.ChannelGroupId等于cg.ChannelGroupId
                     其中,ch.OrganizationId == 8
                     选择新ReportDetails
                     {
                         DealerId = ch.ExternalChannelId,
                         区= ch.ChannelAMSData.District,
                         TM = cg.Name,
                         调查code = u.Survey code,
                         名字= c.FirstName,
                         姓氏= c.LastName,
                         地址= c.Address1,
                         市= c.City,
                         状态= c.State,
                         邮政code = c.Postal code,
                         电子邮件= c.Email,
                         手机= c.Phone,
                         MailSent = c.LetterDate,
                         DateCompleted = c.EmailDate
                     });
        model.Report =查询;
        返回新Utilities.ExcelResult< ViewReportModel>(
            ControllerContext,
            〜/ ExcelReport.aspx
            CustomerReport.xls
            模型
            );
    }

我的模型ReportsModel.cs

 公共类ReportDetails
{
    公共字符串DealerId {搞定;组; }
    公共字符串区{搞定;组; }
    公共字符串TM {搞定;组; }
    公共字符串调查code {搞定;组; }
    公共字符串名字{获得;组; }
    公共字符串名字{获得;组; }
    公共字符串地址{搞定;组; }
    公共字符串城{搞定;组; }
    公共字符串状态{搞定;组; }
    公共字符串邮政code {搞定;组; }
    公共字符串电子邮件{获得;组; }
    公共字符串电话{搞定;组; }
    公众的DateTime? MailSent {搞定;组; }
    公众的DateTime? DateCompleted {搞定;组; }
}公共类ViewReportModel:PageModel
{
    公共IEnumerable的< ReportDetails>报告{搞定;组; }
    公共字符串结果{搞定;组; }
}


解决方案

我写了一个小博客文章这个位置: HTTP://landokal.word$p$pss.com/2011/04/28/asp-net-mvc-export -to-Excel的伎俩/

从本质上讲,你可以做的是,创造一个按钮,发送您的页面上的元素之一的内容并将其分配给你的模型的属性,因此每次你导出的观点,你的Export方法可以检查模型属性,而不是直接渲染视图。

此外,在后有一些code样本如何做一个基本的出口MVC到excel,如果你不想去其他路线。

@model SA.MarketingManager.WebClient.Areas.Reports.Models.ViewReportModel
@{
    Layout = "~/CustomViews/lennoxcap.net/Shared/_DealerLayout.cshtml";
}
@using (Html.BeginForm())
{
    <div style="width: 100%; font-size: 9px;" id="results">
        <h3  style="background-color: #CC0000; color: #fff; font-size: 1.5em;">
            Customer Survey Report</h3>
        @if (Model.Report.Count() > 0)
        {
            <table id="SurveyResponse" width="100%" cellpadding="0" cellspacing="0">
                <thead>
                    <tr class="header">
                        <td>
                            Dealer #
                        </td>
                        <td>
                            District
                        </td>
                        <td>
                            TM
                        </td>
                        <td>
                            Survey Code
                        </td>
                        <td>
                            First Name
                        </td>
                        <td>
                            Last Name
                        </td>
                        <td>
                            Address
                        </td>
                        <td>
                            City
                        </td>
                        <td>
                            State
                        </td>
                        <td>
                            Postal Code
                        </td>
                        <td>
                            Phone
                        </td>
                        <td>
                            Mail Sent
                        </td>
                        <td>
                            Email Sent
                        </td>
                    </tr>
                </thead>
                <tbody>
                    @{bool alternate = false;}
                    @foreach (var tr in Model.Report)
                    {
                        <tr @(alternate ? "class=alternate" : "")>
                            <td>
                                @tr.DealerId
                            </td>
                            <td>
                                @tr.District
                            </td>
                            <td>@tr.TM
                            </td>
                            <td>@tr.SurveyCode
                            </td>
                            <td>@tr.FirstName
                            </td>
                            <td>@tr.LastName
                            </td>
                            <td>@tr.Address
                            </td>
                            <td>@tr.City
                            </td>
                            <td>@tr.State
                            </td>
                            <td>@tr.PostalCode
                            </td>
                            <td>@tr.Phone
                            </td>
                            <td>@tr.MailSent
                            </td>
                            <td>@tr.DateCompleted
                            </td>
                        </tr>
                           alternate = !alternate;
                    }
                </tbody>
            </table>
        }
        else
        {
            <text>There are no records to display</text>
        }
        <p>
            <input type="submit" id="Submit" value="Export Data" class="PremierSubmitButton" />
       </p>
    </div>

controller

public ActionResult CustomerReport()
    { 
        ViewReportModel model = new ViewReportModel();
            var query = (from u in SessionHandler.CurrentContext.LennoxSurveyResponses
                            join c in SessionHandler.CurrentContext.MailingListEntries on u.SurveyCode equals c.SurveyCode
                            join cl in SessionHandler.CurrentContext.MailingLists on c.MailingListId equals cl.MailingListId
                            join ch in SessionHandler.CurrentContext.Channels on cl.ChannelId equals ch.ChannelId
                            join cg in SessionHandler.CurrentContext.ChannelGroups on ch.ChannelGroupId equals cg.ChannelGroupId
                            //let con = ch.Contacts.FirstOrDefault()
                            where ch.OrganizationId == 8
                            //&& con.ContactTypeId == ContactTypeConstants.TMId
                            //select new ReportDetails() { SurveyResponse = u, MailingListEntry = c, Channel = cl.Channel, Contact = con }); 
                            select new ReportDetails{   DealerId = ch.ExternalChannelId,
                                                        District = ch.ChannelAMSData.District,
                                                        TM = cg.Name,
                                                        SurveyCode = u.SurveyCode,
                                                        FirstName = c.FirstName,
                                                        LastName = c.LastName,
                                                        Address = c.Address1,
                                                        City = c.City,
                                                        State = c.State,
                                                        PostalCode = c.PostalCode,
                                                        Email = c.Email,
                                                        Phone = c.Phone,
                                                        MailSent = c.LetterDate,
                                                        DateCompleted = c.EmailDate});
               model.Report = query;
               return View("CustomerReport", model);
    }
    [HttpPost]
    public ActionResult CustomerReport(ViewReportModel model)
    {
        var query = (from u in SessionHandler.CurrentContext.LennoxSurveyResponses
                     join c in SessionHandler.CurrentContext.MailingListEntries on u.SurveyCode equals c.SurveyCode
                     join cl in SessionHandler.CurrentContext.MailingLists on c.MailingListId equals cl.MailingListId
                     join ch in SessionHandler.CurrentContext.Channels on cl.ChannelId equals ch.ChannelId
                     join cg in SessionHandler.CurrentContext.ChannelGroups on ch.ChannelGroupId equals cg.ChannelGroupId
                     where ch.OrganizationId == 8
                     select new ReportDetails
                     {
                         DealerId = ch.ExternalChannelId,
                         District = ch.ChannelAMSData.District,
                         TM = cg.Name,
                         SurveyCode = u.SurveyCode,
                         FirstName = c.FirstName,
                         LastName = c.LastName,
                         Address = c.Address1,
                         City = c.City,
                         State = c.State,
                         PostalCode = c.PostalCode,
                         Email = c.Email,
                         Phone = c.Phone,
                         MailSent = c.LetterDate,
                         DateCompleted = c.EmailDate
                     });
        model.Report = query;
        return new Utilities.ExcelResult<ViewReportModel>(
            ControllerContext,
            "~/ExcelReport.aspx",
            "CustomerReport.xls",
            model
            );
    }

My model ReportsModel.cs

public class ReportDetails 
{
    public string DealerId { get; set; }
    public string District { get; set; }
    public string TM { get; set; }
    public string SurveyCode { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public DateTime? MailSent { get; set; }
    public DateTime? DateCompleted { get; set; }
}

public class ViewReportModel : PageModel
{
    public IEnumerable<ReportDetails> Report { get; set; }
    public string Results { get; set; }
}

解决方案

I wrote a little blog post about this here : http://landokal.wordpress.com/2011/04/28/asp-net-mvc-export-to-excel-trick/

Essentially, what you could do is, create a button that sent the contents of one of the elements on your page and assigned it to a property on your model, therefore everytime you exported that view, your Export method could check the model property instead of rendering the view directly.

Also in the post are some code samples how to do a basic export to excel in MVC if you don't want to go the other route.

这篇关于导出模型数据到Excel MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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