mvc导出EXCEL
/// <summary>
/// 导出EXCEL
/// </summary>
/// <returns></returns>
public ActionResult ExportExcel(FormCollection form)
{ //var sbHtml = new StringBuilder();
//sbHtml.Append("<table border='1' cellspacing='0' cellpadding='0'>");
//sbHtml.Append("<tr>");
//var lstTitle = new List<string> { "编号", "姓名", "年龄", "创建时间" };
//foreach (var item in lstTitle)
//{
// sbHtml.AppendFormat("<td style='font-size: 14px;text-align:center;background-color: #DCE0E2; font-weight:bold;' height='25'>{0}</td>", item);
//}
//sbHtml.Append("</tr>"); //for (int i = 0; i < 1000; i++)
//{
// sbHtml.Append("<tr>");
// sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", i);
// sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>屌丝{0}号</td>", i);
// sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", new Random().Next(20, 30) + i);
// sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", DateTime.Now);
// sbHtml.Append("</tr>");
//}
//sbHtml.Append("</table>");
//byte[] fileContents = Encoding.UTF8.GetBytes(sbHtml.ToString());
//var fileStream = new MemoryStream(fileContents);
//return File(fileStream, "application/ms-excel", "fileStream.xls"); //获取查询后条件的数据
var query = from s in iWorkOrderService.Search(c => true)
where
(string.IsNullOrEmpty(form["hid"]) ? true : s.Id == form["hid"]) &&
(string.IsNullOrEmpty(form["hState"]) ? true : s.State.Contains(form["hState"])) &&
(string.IsNullOrEmpty(form["hType"]) ? true : s.Type.Contains(form["hType"])) &&
(string.IsNullOrEmpty(form["hPhone"]) ? true : s.Phone.Contains(form["hPhone"])) &&
(string.IsNullOrEmpty(form["hService"]) ? true : s.Service.Contains(form["hService"])) &&
(string.IsNullOrEmpty(form["hProcessingMode"]) ? true : s.ProcessingMode.Contains(form["hProcessingMode"])) &&
(string.IsNullOrEmpty(form["hProductId"]) ? true : s.ProductId == int.Parse(form["hProductId"])) &&
(string.IsNullOrEmpty(form["hCstart"]) && string.IsNullOrEmpty(form["hCend"]) ? true : DateTime.Parse(s.CreateTime) >= DateTime.Parse(form["hCstart"]) && DateTime.Parse(s.CreateTime) <= DateTime.Parse(form["hCend"])) &&
(string.IsNullOrEmpty(form["hAstart"]) && string.IsNullOrEmpty(form["hAend"]) ? true : DateTime.Parse(s.ProcessingTime) >= DateTime.Parse(form["hAstart"]) && DateTime.Parse(s.ProcessingTime) <= DateTime.Parse(form["hAend"])) select new
{
s.Id,
s.Type,
s.SIM,
s.Product.Name,
s.Terminal,
s.Service,
s.Quantity,
s.Company,
s.Contact,
s.Sex,
s.Phone,
s.ProblemDescription,
s.Attachment,
s.ProcessingMode,
s.SI,
s.Suggestion,
s.State
}; #region MyRegion
System.Web.UI.WebControls.DataGrid dgExport = null; // 当前对话 System.Web.HttpContext curContext = System.Web.HttpContext.Current; // IO用于导出并返回excel文件 System.IO.StringWriter strWriter = null; System.Web.UI.HtmlTextWriter htmlWriter = null; string filename = DateTime.Now.Year + "_" + DateTime.Now.Month + "_" + DateTime.Now.Day + "_" + DateTime.Now.Hour + "_" + DateTime.Now.Minute; byte[] str = null; // 设置编码和附件格式 curContext.Response.Charset = "GB2312"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls"); curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文 curContext.Response.ContentType = "application/vnd.ms-excel"; //System.Text.Encoding.UTF8; // 导出excel文件 strWriter = new System.IO.StringWriter(); htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter); // 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid dgExport = new System.Web.UI.WebControls.DataGrid(); dgExport.DataSource = query;// db.WorkOrders.ToList(); dgExport.AllowPaging = false; dgExport.DataBind(); dgExport.RenderControl(htmlWriter); // 返回客户端 str = System.Text.Encoding.UTF8.GetBytes(strWriter.ToString()); return File(str, "attachment;filename=" + filename + ".xls");
#endregion }
mvc导出EXCEL的更多相关文章
- mvc导出excel 之 新
前段时间做的mvc导出excel 老大说要进行优化,我原来导出是用npoi插件进行导出,格式是将数据放入到datatable中,然后进行导出. 说要优化的时候就想着将datatable数据导出格式改为 ...
- ASP.NET MVC导出excel
ASP.NET MVC导出excel 要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式 ...
- Mvc 导出 Excel
Mvc 导出 Excel 之前接触过Webform,winfrom 的导出Excel方法 ,优点:省事.缺点:服务器必须安装Office 这几天做项目 和 大牛学习了一下 新的方法,自己加以总结.希望 ...
- MVC 导出Excel 的其中一方法(View导出excel)
场景:mvc下导出excel 思路:使用View导出excel 步骤: 1.导出标签添加事件 $("#export_A").click(function(){ //省略代码.... ...
- ASP.NET MVC导出excel(数据量大,非常耗时的,异步导出)
要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式,后台开辟一个线程将excel导出到指 ...
- MVC导出Excel,提供下载Excel
类1: using System.Collections.Generic;using System.Data;using System.Web.Mvc;using System.IO;using Sy ...
- MVC学习笔记---MVC导出excel(数据量大,非常耗时的,异步导出)
要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式,后台开辟一个线程将excel导出到指 ...
- spring mvc 导出 excel
// js 触发导出 excel 方法 导出当前页的数据 含有条件查询的结果 // js 框架使用的 是 easyui function doExport(){ var optins = $(&quo ...
- 三层+MVC导出Excel(2)
背景: 出门在外,一切以健康为主,学习为辅,健康搞好了,学习也不能拉下,在外工作期间,我们在做数据导出的时候,自己封了一个类,利用NPOI进行数据导出Excel,自我感觉良好,特给大家分享一下,希望对 ...
随机推荐
- Linux系统的压缩技术
1.常见的压缩文件扩展名 *.Z ---> compress程序压缩的文件. *.gz --->gzip 程序压缩的文件: *.bz2------>bzip2程序压缩的文件: *.t ...
- 【javascript进阶】异常
前言 最近有些时间了,今天看了看博客的后台记录,好多都没有写博客呢,争取尽快把以前的补上,javascrit中的异常在前端大家用的好像不是很多,其实javascript的异常和大多数的后端语言差不大, ...
- win7 audio repeater 虚拟声卡 屏幕录像专家
- Linux中执行shell脚本的4种方法总结
bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一:切换到shell脚本所在 ...
- linux命令-find {查找}
一 命令解释 常用示例 :find path -option [ -print ] [ -exec -ok command ] {} \; path: find命令所 ...
- 查看Oracle加锁情况及解锁方法
注意:必须有Oracle DBA 权限才能操作一下SQL语句: ---查询锁定的会话 select * from v$session t where t.SID in (select t2.sessi ...
- Git Pro - (1) 基础
近乎所有操作都可本地执行 在Git中的绝大多数操作都只需要访问本地文件和资源,不用连网. 三种状态 对于任何一个文件,在 Git 内都只有三 种状态:已提交(committed),已修改(modifi ...
- 关于一次oracle sqlplus可登陆,但监听起不来的解决。由于listener.log文件超过4G
1.在oracle服务器上cmd 执行 lsnrctl 执行start 过了好久,提示监听程序已经启动. 再执行status 过来好久,才提示命令执行成功. 最后找到原因是因为C:\Oracle\di ...
- vs2012中添加lib,.h文件方法(原)
项目.属性.C/C++.附加包含目录:填写附加头文件(*.h)所在目录 分号间隔多项项目.属性.链接器.附加库目录:填写附加依赖库(*.lib)所在目录 分号间隔多项项目.属性.链接器(点前面的+展开 ...
- 使用PerfView诊断.Net GC的问题
PerfView 概述: PerfView是一个可以帮助你分析CPU和内存问题的工具软件.它非常轻量级也不会入侵诊断的程序,在诊断过程中对诊断的程序影响甚微. Visual Studio自带的性能分析 ...