export model data to excel mvc

2020-03-31 03:56发布

问题:

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

回答1:

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.