2011-04-26 65 views
1
@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> 

控制器出口模型數據到excel MVC

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

我寫了這個在這裏一個小博客文章:http://landokal.wordpress.com/2011/04/28/asp-net-mvc-export-to-excel-trick/

從本質上講,你可以做的是,創建一個按鈕,發送頁面上的一個元素的內容並將其分配給一個屬性o在您的模型中,因此每次導出該視圖時,Export方法都可以檢查模型屬性,而不是直接渲染視圖。

另外,在這篇文章中還有一些代碼示例,如果你不想去其他路由,那麼如何在MVC中做一個基本的導出到excel。

+0

太棒了,非常感謝。 – bladerunner 2011-05-03 14:16:13

+0

np,很高興它的工作 – slandau 2011-05-03 14:23:48

+0

嘿slandau,我有一個問題。如果在一個模式,我我的觀點的數據,如何將我能夠把它傳遞給你的ExcelResult class.public的ActionResult CustomerReport(ViewReportModel模型) { 回報新Utilities.ExcelResult ( ControllerContext, 「Export.aspx」 「ExcelExport」, model ); }我找不到ModelBase。使用你的解決方案,我應該能夠做到這一點。 – bladerunner 2011-05-03 14:47:47