MVC导出数据到EXCEL新方法:将视图或分部视图转换为HTML后再直接返回FileResult

简介:

导出EXCEL方法总结

MVC导出数据到EXCEL的方法有很多种,常见的是:

1.采用EXCEL COM组件来动态生成XLS文件并保存到服务器上,然后转到该文件存放路径即可;

优点:可设置丰富的EXCEL格式,缺点:需要依赖EXCEL组件,且EXCEL进程在服务器中无法及时关闭,以及服务器上会存留大量的不必要的XLS文件;

2.设置输出头为:application/ms-excel,再输出拼接的HTML TABLE数据;

优点:无需组件,可设置一些简单的格式,缺点:拼接HTML TABLE过程较复杂,不够直观;

3.借助第三方组件(如:NPOI)

优点及缺点均依赖于第三方组件的易用性上面,在此不作说明;

大家也可以看我之前发表的一篇博文:我写的一个ExcelHelper通用类,可用于读取或生成数据

实现在MVC下新的导出EXCEL方法

这里给大家分享一个在MVC下的新方法:将视图或分部视图转换为HTML后再直接返回FileResult即可轻松实现导出EXCEL功能,下面直接上代码。

首先看一下分部视图(IndexDataList)的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
@model IEnumerable< CCPS.Models.Data.CustomerCommentInfo >
@using PagedList.Mvc;
 
< table >
     < thead >
         < tr >
             < th >意见ID</ th >
             < th >组 织</ th >
             < th >车牌</ th >
             < th >车型</ th >
             < th >皇家版</ th >
             < th >客户</ th >
             < th >客户电话</ th >
             < th >责任部门</ th >
             < th >责任班组</ th >
             < th >业务顾问</ th >
             < th >意见类型</ th >
             < th >状态</ th >
             < th >录入员</ th >
             < th >录入时间</ th >
         </ tr >
     </ thead >
     < tbody  id="list-table-body">
         @foreach (var item in Model)
         {
             string className = "";
             if (item.Status == "已审核未处理")
             {
                 className = "uncl_yellow";
                 if (item.AuditDatetime != null && DateTime.Now > ((DateTime)item.AuditDatetime).AddHours(24))
                 {
                     className = "uncl_red";
                 }
             }
             < tr  class="@className" data-adt="@item.AuditDatetime">
                 < td >< a  href="http://oa.pfcn.com/flow/fl_ui_main.aspx?ID=@item.Id&flow_id=147" target="_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 >
}

我这里的分部视图不仅仅只是为了导出EXCEL用,在搭配主视图显示数据时照样可以可以用,所以我这里有是否分页判断,如果导出EXCEL,那肯定就不需要分页了。

下面是实现将视图、分部视图生成HTML的方法,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[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(controller.ControllerContext, view, controller.ViewData, controller.TempData, writer);
         viewContext.View.Render(viewContext, writer);
         return  writer.ToString();
     }
}
 
[NonAction]
protected  string  RenderPartialViewToString(Controller controller,  string  partialViewName)
{
     IView view = ViewEngines.Engines.FindPartialView(controller.ControllerContext, partialViewName).View;
     using  (StringWriter writer =  new  StringWriter())
     {
         ViewContext viewContext =  new  ViewContext(controller.ControllerContext, view, controller.ViewData, controller.TempData, writer);
         viewContext.View.Render(viewContext, writer);
         return  writer.ToString();
     }
}

这两个方法是按照其视图呈现的步骤与原理来实现的,一般步骤是:找到视图-->实例化视图上下文-->呈现视图到输出容器

最后就是定义一个导出EXCEL的Action方法,并返回FileResult,这样就完成了导出EXCEL功能,代码如下:

1
2
3
4
5
6
7
8
public  ActionResult Export(DataFilter<CustomerCommentInfo>[] filters)
{
     var  resultList = DataProvider.GetCustomerCommentInfos(filters).OrderBy(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()));
}

是不是很简单呢?想要导出什么样的EXCEL格式内容,直接可以在视图中设计好就可以了。

本文转自 梦在旅途 博客园博客,原文链接:http://www.cnblogs.com/zuowj/p/4846076.html  ,如需转载请自行联系原作者

相关文章
|
18天前
|
SQL 缓存 easyexcel
面试官问10W 行级别数据的 Excel 导入如何10秒处理
面试官问10W 行级别数据的 Excel 导入如何10秒处理
47 0
|
18天前
html中div内容居中的方法使用弹性盒子居中
html中div内容居中的方法使用弹性盒子居中
8 0
|
29天前
|
安全 Java 数据库连接
jdbc解析excel文件,批量插入数据至库中
jdbc解析excel文件,批量插入数据至库中
20 0
|
1月前
|
Java API Apache
使用AOP+反射实现Excel数据的读取
使用AOP+反射实现Excel数据的读取
|
1月前
|
存储 数据处理 索引
Python操作Excel常用方法汇总
Python操作Excel常用方法汇总
33 0
|
1月前
|
SQL 数据可视化 数据处理
使用SQL和Python处理Excel文件数据
使用SQL和Python处理Excel文件数据
54 0
|
29天前
|
安全 Java 数据库连接
jdbc实现批量给多个表中更新数据(解析Excel表数据插入到数据库中)
jdbc实现批量给多个表中更新数据(解析Excel表数据插入到数据库中)
153 0
|
1月前
|
存储 数据处理 Python
使用Python批量合并Excel文件的所有Sheet数据
使用Python批量合并Excel文件的所有Sheet数据
28 0
|
1月前
|
存储 数据处理 Python
使用openpyxl库从Excel文件中提取指定的数据并生成新的文件
使用openpyxl库从Excel文件中提取指定的数据并生成新的文件
28 0
|
1月前
|
数据处理 Python
4种方法用Python批量实现多Excel多Sheet合并
4种方法用Python批量实现多Excel多Sheet合并
25 0