将模型数据导出为ex​​cel mvc

时间:2011-04-26 20:37:27

标签: asp.net asp.net-mvc-3

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

控制器

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

我的模型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; }
}

1 个答案:

答案 0 :(得分:1)

我在这里写了一篇关于此的博文:http://landokal.wordpress.com/2011/04/28/asp-net-mvc-export-to-excel-trick/

基本上,你可以做的是,创建一个按钮,发送页面上其中一个元素的内容并将其分配给模型上的属性,因此每次导出该视图时,Export方法都可以检查模型属性而不是直接渲染视图。

另外在帖子中有一些代码示例如果你不想去其他路线,如何在MVC中进行基本导出到excel。