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的动态库 在项目中,点击项目名称右键-- ...
随机推荐
- XE8 for iOS 状态栏的几种效果
XE8 实现 iOS 状态栏的几种效果: 一.状态栏底色: 开一个新工程. 设定 Fill.Color 颜色属性. 设定 Fill.Kind = Solid. 无需修改任何官方源码. 二.隐藏状态栏( ...
- 实现在Android 进程和线程
进程和线程 如果某个应用程序组件是第一次被启动,且这时应用程序也没有其他组件在运行,则Android系统会为应用程序创建一个包含单个线程的linux进程.默认情况下,同一个应用程序的所有组件都运行在同 ...
- JVM内存格局总结
最近一次面试,面试官让我讲讲JVM的内存这一块的理解.我回答的不满意,今天做一个总结. 做一个产品,最终要做到高并发.高可靠.归根结底,是对CPU.内存等资源受限所作出的解决方案.就内存而言,我们写的 ...
- No.020:Valid Parentheses
问题: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- ahjesus使用T4模板自动维护实体
在entity项目里新建模板DBEntity.tt <#@ template debug="false" hostspecific="true" lang ...
- PHP中使用redis执行lua脚本示例
摸索了一下在PHP中如何使用redis执行lua脚本,写了一个脚本如下,供以后参考 <?php $redis = new Redis(); #实例化redis类 $redis->conne ...
- Mac OS Git 安装
一.Git是一个分布式的代码版本管理工具.类似的常用工具还有SVN,CVS.最大的特点也是优点在于提供分布式的代码管理 1.分支代码只有一份! 使用过svn的童鞋想必都知道,当我们要开发一个新功能或者 ...
- Lucene.net站内搜索—2、Lucene.Net简介和分词
目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...
- ASP.NET使用jQuery AJAX实现MD5加密实例
一个asp.net ajax例子,使用jquery,实现md5加密.在.NET 4.0,Visual Studio 2010上成功运行. 效果体验:http://tool.keleyi.com/t/m ...
- php代码美化/格式化 还原 -问题
使用某个PHP代码格式化的工具.源代码: if ($this->_standardize_newlines == TRUE) { if (strpos($str, "\r") ...