2016-08-19 54 views
4

這是我的第一個問題。MVC 5如何在rdlc報告中使用對象

我正在使用VS社區2015和一個MVC 5項目與實體框架6.我使用代碼第一次遷移數據建模。

我已經有報告使用每個視圖。每個視圖都將C#模型用於顯示報告,作爲HTML5網頁。

現在我必須將輸出發送到pdf,word,excel ...爲此,我將使用RDLC,但我不知道如何將對象模型設置爲數據集。這個想法是發送相同的對象,已經使用該視圖來構建報告。報告的數據不是。

任何想法或建議或教程我該怎麼做?

我對RDLC非常陌生,以前從未使用過數據集。

謝謝

回答

4

您可以使用ReportViewer對象將RDLC呈現爲PDF或HTML。對於我的情況(下文),我想要一個PDF文檔,並將其作爲FileContentResult ActionResult返回。如果您希望它以下載的形式返回,請使用File ActionResult(我已經評論過,以供您使用)。

public ActionResult GetPackingSlipPDF(int shipmentId) 
    { 
     var shipment = _inboundShipmentService.GetInboundShipmentById(shipmentId); 

     Warning[] warnings; 
     string mimeType; 
     string[] streamids; 
     string encoding; 
     string filenameExtension; 

     var viewer = new ReportViewer(); 
     viewer.LocalReport.ReportPath = @"Labels\PackingSlip.rdlc"; 

     var shipLabel = new ShippingLabel { ShipmentId = shipment.FBAShipmentId, Barcode = GetBarcode(shipment.FBAShipmentId) }; 

     viewer.LocalReport.DataSources.Add(new ReportDataSource("ShippingLabel", new List<ShippingLabel> { shipLabel })); 
     viewer.LocalReport.Refresh(); 

     var bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings); 

     return new FileContentResult(bytes, mimeType); 

     //return File(bytes, mimeType, shipment.FBAShipmentId + "_PackingSlip.pdf"); 
    } 

Rendering an RDLC report in HTML in ASP.NET MVC

+0

感謝穆罕默德!有我很多新的東西在這裏學習。 –

+0

如何設計rdlc報告?它要求一個數據集或數據源...它迫使我使用到SQL的數據連接,但我想要一個C#模型對象的數據源。 –