ASP.NET MVC 拓展ViewResult实现word文档下载
最近项目中有同事用到word文档导出功能,遇到了一些导出失败问题,帮其看了下解决问题的同事,看了下之前的代码发现几个问题:
- 代码编写不规范,word导出功能未收口
- 重复代码导出都是
- 实现逻辑比较复杂,不易于维护及使用
在帮其解决问题后,写了下面这个ViewResult拓展,依赖Razor视图,能够直接转换页面为word文档,但是不支持外联样式表,样式可以定义在<head>头部
废话不多说,直接上代码:
|
public { public
public WordFileResult(object model, string viewName, string fileName) { ViewData = new ViewName = viewName; FileName = fileName; }
public WordFileResult() : this(new {
}
public WordFileResult(string fileName) : this(new {
}
public WordFileResult(object model) : this(model, null, null) {
}
public WordFileResult(object model, string fileName) : this(model, null, fileName) {
}
protected { context.HttpContext.Response.Charset = "utf-8"; context.HttpContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); if (string.IsNullOrWhiteSpace(FileName)) { FileName = ViewName; } context.HttpContext.Response.AppendHeader("Content-Disposition", string.Format("filename={0}.doc", FileName)); context.HttpContext.Response.ContentType = "application/msword"; ViewEngineResult result = ViewEngineCollection.FindView(context, ViewName, string.Empty); if (result.View != null) { return result; } return } }
public { public { return }
public { return }
public { return }
public { return }
} |
使用方式比较简单:
- 定义视图,使用Razor视图布局word内容,
|
@using RazorWord.Models @model List<LoginViewModel> @{ Layout = null; }
<!DOCTYPE html>
<html> <head> <meta name="viewport" content="width=device-width" /> <title>Export</title> <style type="text/css"> .table td, .table th { background-color: #fff !important; }
.table { border-collapse: collapse !important; background-color: #ffffff; width: 100%; margin-bottom: 20px; }
.table thead > tr > th, .table tbody > tr > th, .table tfoot > tr > th, .table thead > tr > td, .table tbody > tr > td, .table tfoot > tr > td { padding: 8px; line-height: 1.428571429; vertical-align: top; border-top: 1px solid #dddddd; }
.table thead > tr > th { vertical-align: bottom; border-bottom: 2px solid #dddddd; } </style> </head> <body> <div> <table class="table"> <tr> <th>电子邮件</th> <th>密码</th> <th>是否记住密码</th> </tr> @foreach (LoginViewModel item in Model) { <tr> <td>@item.Email</td> <td>@item.Password]</td> <td>@(item.RememberMe ? "Y" : "N")</td> </tr> } </table> </div> </body> </html> |
- 添加Action修改对应的Action方法返回值
|
public { List<LoginViewModel> models = new for (int i = 0; i < 20; i++) { models.Add(new { Email = $"{i}user@123.com", Password = new RememberMe = i % 2 == 0 ? true : false }); }
return }
public { List<LoginViewModel> models = new for (int i = 0; i < 20; i++) { models.Add(new { Email = $"{i}user@123.com", Password = new RememberMe = i % 2 == 0 ? true : false }); } return } |
还有一种更方便的方式:使用ActionFilterAttribute过滤器
直接上代码:
|
public { public public { var result = filterContext.Result as if (string.IsNullOrWhiteSpace(FileName)) { FileName = result == null ? DateTime.Now.ToString("yyyy_MM_dd") : result.ViewName; } filterContext.HttpContext.Response.AppendHeader("Content-Disposition", string.Format("filename={0}.doc", FileName)); filterContext.HttpContext.Response.ContentType = "application/msword"; base.OnResultExecuted(filterContext); } } |
使用方式:在一个Action方法上面添加特性:WordDocumentAttribute
|
[WordDocument(FileName = "下载文件名称")] public { return View(); } |
搞定word导出功能,抛砖引玉,希望能够帮助到大家,也希望有更好实现方式的同学能够一起讨论!!!!!!
ASP.NET MVC 拓展ViewResult实现word文档下载的更多相关文章
- SpringBoot+FreeMarker开发word文档下载,预览
背景: 开发一个根据模版,自动填充用户数据并下载word文档的功能 使用freemarker进行定义模版,然后把数据进行填充. maven依赖: <parent> <groupId& ...
- php将html转成word文档下载
<meta charset="utf-8" /> <?php class word{ function start(){ ob_start(); echo '&l ...
- django admin 中实现word文档下载
为了实现此功能,需要用到的知识点: (1)django admin 自定义字段: 参考:http://www.cnblogs.com/wumingxiaoyao/p/6928297.html ...
- Asp.Net MVC WebApi2 自动生成帮助文档
WebAPI Help文档配置 开发环境VS2013+mvc5+WebApi2 一.通过NuGet引用Web API Test Client 安装后会多一个Areas文件夹 二.设置xml文档项目-- ...
- MVC post 方法导出word文档
View code: function ExportWord(){ var html = $("#div_workInfo").html(); $("#hidWord&q ...
- FreeMarker生成Word文档
FreeMarker简介: FreeMarker是一款模板引擎:即一种基于模板和要改变的数据,并用来生成输出文本(HTML网页.电子邮件.配置文件.源代码等)的通用工具,它不是面向最终用户的,而是一个 ...
- Android中使用POI加载与显示word文档
最近打算实现一个功能:在Android中加载显示Word文档,当然这里不是使用外部程序打开.查看一些资料后,打算采用poi实现,确定了以下实现思路: 将ftp中的word文档下载到本地. 调用poi将 ...
- JSP生成WORD文档,EXCEL文档及PDF文档的方法
转自:https://www.jb51.net/article/73528.htm 本文实例讲述了JSP生成WORD文档,EXCEL文档及PDF文档的方法.分享给大家供大家参考,具体如下: 在web- ...
- C# 动态生成word文档 [C#学习笔记3]关于Main(string[ ] args)中args命令行参数 实现DataTables搜索框查询结果高亮显示 二维码神器QRCoder Asp.net MVC 中 CodeFirst 开发模式实例
C# 动态生成word文档 本文以一个简单的小例子,简述利用C#语言开发word表格相关的知识,仅供学习分享使用,如有不足之处,还请指正. 在工程中引用word的动态库 在项目中,点击项目名称右键-- ...
随机推荐
- 传智播客JavaWeb听课总结
一. JavaWeb基础 第一天: 1.Eclipse详解: (1).Bad versionnumber in .class file:编译器版本和运行(JRE)版本不符合.高的JRE版本兼容低版本的 ...
- kFreeBSD有活过来的迹象?UbuntuBSD
那些年追过的FreeBSD分支,有debian系的kFreeBSD,arch系的archBSD现在叫pacBSD,gentoo系的gentooBSD:但基本上在虚拟机环境很难成功安装http://ww ...
- 关联查询 join on 和比较运算符 in
join on多表之间的关联查询 写法select 字段 from 表1 t join 表2 s on t.字段1 = s.字段1 where 条件: 也可以这么写select 字段 from 表1 ...
- 项目总结笔记系列 Social Hub KT Session2
KT Session2: 主要是代码的实现(KT is knowledge Transfer) 如图所示,在整个Social Hub项目中,我们的项目Data Feed Service处于承上启下的作 ...
- MySQL配置文件mysql.ini参数详解、MySQL性能优化
my.ini(Linux系统下是my.cnf),当mysql服务器启动时它会读取这个文件,设置相关的运行环境参数. my.ini分为两块:Client Section和Server Section. ...
- Ouibounce – 在用户离开你网站时显示模态弹窗
Ouibounce 是一个微小的库,用于实现在用户离开你的网站的时候显示一个模式窗口.这个库可以帮助你增加着陆页的转换率. Ouibounce 会在当鼠标光标移动到接近(或通过)视口(viewport ...
- 1001Freedownloads – 免费下载海量素材
1001FreeDownloads.com 力求超越所有其他的互联网搜索体验,它拥有超过1000个免费的设计图像和字体的目录.这个独特的网站提供了多种可以用来作为剪贴簿页面,图标和壁纸.不管你需要什么 ...
- 总结CSS3新特性(Animation篇)
动画(Animation),是CSS3的亮点.//之一 通过animation属性指定@keyframe来完成关键帧动画; @keyframe用法: @keyframes name { 0% { to ...
- chrome developer tool—— 断点调试篇
断点,调试器的功能之一,可以让程序中断在需要的地方,从而方便其分析.也可以在一次调试中设置断点,下一次只需让程序自动运行到设置断点位置,便可在上次设置断点的位置中断下来,极大的方便了操作,同时节省了时 ...
- UITableView中的cell的优化
1.cell的重用 所谓的cell的重用就是,视图加载的时候只会创建当前视图中的cell,或者比当前视图多一点的cell, 当视图滚动的时候,滚出屏幕的cell会放进缓存中,滚进屏幕的cell会根据I ...