Summary of Exporting Excel methods
There are many ways in which MVC exports data to Excel, which is common:
1. Use the Excel COM component to dynamically generate the XLS file and save it to the server, then go to the file storage path;
Advantages: You can set rich Excel format, disadvantage: Need to rely on Excel components, and Excel process in the server can not close in time, and the server will persist a large number of unnecessary XLS files;
2. Set the output header to: Application/ms-excel, and then output the stitching HTML table data;
Advantages: No components, can set some simple format, disadvantage: Splicing HTML table process is more complex, not intuitive;
3. With third-party components (e.g., npoi)
The pros and cons are dependent on the ease of use of third-party components and are not described here;
You can also read my previous blog post: I wrote a excelhelper generic class that can be used to read or generate data
Implementing a new Export Excel method under MVC
Here's a new way to share your approach to MVC: convert a View or partial view to HTML and return directly to Fileresult to easily export Excel functionality, directly below the code.
First look at the contents of the partial view (indexdatalist):
@model Ienumerable<ccps. models.data.customercommentinfo> @using pagedlist.mvc;<table> <thead> <tr> <th > Views id</th> <th> Organization </th> <th> license plate </th> <th> model </th& Gt <th> Royal Edition </th> <th> customer </th> <th> Customer calls </th> <th> responsible Department </th> <th> Responsibility Team </th> <th> Business consultant </th> <th> opinion type </th> <th> status </th> <th> entry </th> <th> entry time </th> </t r> </thead> <tbody id= "List-table-body" > @foreach (var item in Model) {Stri ng className = ""; if (item. Status = = "Audited unhandled") {className = "uncl_yellow"; if (item. Auditdatetime! = null && datetime.now > (DateTime) item. Auditdatetime). AddhouRS) {className = "uncl_red"; }} <tr class= "@className" data-adt= "@item. Auditdatetime "> <td><a href=" http://oa.pfcn.com/flow/[email protected]&flow_id=147 "ta rget= "_blank" > @item. id</a></td> <td> @item .orgname</td> <td> @item. Plateno</td> <td> @item. Model</td> <td> @item. Isroyalver</td> <td> @item. Customername</td> <td> @item. Customerphoneno</td> <td> @item. Relevantdept</td> <td> @item. Relevantgroup</td> <td> @item. Consultant</td> <td> @item. Type</td> <td> @item. Status</td> <td> @item. Createby</td> <td> @string. Format ("{0:g}", item. CReatedatetime) </td> </tr> </tbody></table> @if (true!=viewbag.nopaging) {< Div class= "Pager" > @Html. Pagedlistpager (Model as Pagedlist.ipagedlist<ccps. models.data.customercommentinfo>, page = = string. Format ("Javascript:turnpage ({0});", page), pagedlistrenderoptions.classicplusfirstandlast) </div>}
My partial view here is not just for exporting Excel, so I can use it when I display the data with the main views, so I have a page to judge if I export Excel, that certainly does not need paging.
Here's how to implement generating HTML for a view, a partial view, with the following code:
[nonaction] protected string renderviewtostring (Controller controller, string viewName, String mastername) {IView view = ViewEngines.Engines.FindView (Controller. ControllerContext, ViewName, Mastername). View; using (StringWriter writer = new StringWriter ()) {ViewContext ViewContext = new ViewContext (con Troller. ControllerContext, view, controller. ViewData, Controller. TempData, writer); ViewContext.View.Render (ViewContext, writer); return writer. ToString (); }} [Nonaction] protected string renderpartialviewtostring (Controller controller, string partialviewn AME) {IView view = ViewEngines.Engines.FindPartialView (Controller. ControllerContext, Partialviewname). View; using (StringWriter writer = new StringWriter ()) {ViewContext ViewContext = new ViewContext (con Troller. ControllerContext, view, controller. ViEwdata, Controller. TempData, writer); ViewContext.View.Render (ViewContext, writer); return writer. ToString (); } }
These two methods are implemented according to the steps and principles of their view rendering, the general step is: Find View--materialized view context--render view to output container
Finally, you define an action method that exports Excel and return Fileresult, which completes the export of Excel functionality, with the following code:
Public ActionResult Export (datafilter<customercommentinfo>[] filters) { var resultlist = Dataprovider.getcustomercommentinfos (filters). (t = t.createdatetime); Viewbag.nopaging = true; Viewdata.model = resultlist; String viewhtml = Renderpartialviewtostring (This, "indexdatalist"); Return File (System.Text.Encoding.UTF8.GetBytes (viewhtml), "Application/ms-excel", String. Format ("Ccpi_{0}.xls", Guid.NewGuid ())); }
Isn't it simple? To export what kind of Excel format content, directly can be in the view of the design is OK.
MVC exports data to Excel new method: Convert view or partial view to HTML before returning directly to Fileresult