方法1:

public ActionResult DownExcel()

{

var stream = list.Select(p => new
{
p.UserName,
p.Mobile,
Status = CommonUtilities.GetEnumDescription<UserStatus>(p.Status ?? 0)
}).ToExcel("sheet1",
new ColumnMap("UserName", "员工姓名"),
new ColumnMap("Mobile", "手机号码"),
new ColumnMap("Status", "账户状态"));

return File(stream, "application/vnd.ms-excel", string.Format("员工信息_{0:yyyyMMdd}.xls", DateTime.Now));

}

方法2:

public ActionResult DownLoadExcel()

{

var list=new List();//list,根据情况取数据

if (list!= null && list.Count > 0)
{
     //下载数据-导Excel
      CreateExcel(list, (HttpContextBase)HttpContext);

}
return null;

}

public void CreateExcel(List<CompanyUserInfoViewModel> list, HttpContextBase context)
{

IWorkbook workbook = new HSSFWorkbook();//创建Workbook对象
ISheet sheet = workbook.CreateSheet("Sheet1");//创建工作表

#region CellStyle
ICellStyle CellStyle = workbook.CreateCellStyle();
CellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
CellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
CellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
CellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
CellStyle.VerticalAlignment = VerticalAlignment.Center;
#endregion

#region TitleStyle
IFont fontStyle = workbook.CreateFont();
fontStyle.Color = NPOI.HSSF.Util.HSSFColor.White.Index;

ICellStyle TitleStyle = workbook.CreateCellStyle();
TitleStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
TitleStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
TitleStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
TitleStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
TitleStyle.FillPattern = NPOI.SS.UserModel.FillPattern.SolidForeground;
TitleStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Blue.Index;
TitleStyle.SetFont(fontStyle);
#endregion

#region 生成标题行
//Title
string[] arrStr = { "编号",  "联系人", "手机","状态" };
int[] arrWidth = { 12, 30, 24, 20};

IRow row = sheet.CreateRow(0); //在工作表中标题行
for (int i = 0; i < arrStr.Length; i++)
{
     sheet.SetColumnWidth(i, arrWidth[i] * 256); //列宽

ICell cell = row.CreateCell(i);
     cell.SetCellValue(arrStr[i]);
     cell.CellStyle = TitleStyle;
}
#endregion

int currentRow = 0;
//生成数据行

foreach (var item in list)
{
       CreateRow(sheet, item, ref currentRow, CellStyle);

}

#region 输出文件

string sFileName="文件名称";
MemoryStream sw = new MemoryStream();
workbook.Write(sw);
sw.Seek(0, SeekOrigin.Begin);
byte[] bf = sw.GetBuffer();
sw.Close();

context.Response.Clear();
context.Response.Buffer = true;
context.Response.Charset = "GB2312";
#region 设定文件名
if (context.Request.UserAgent.ToLower().IndexOf("msie") > -1)
{
sFileName = HttpUtility.UrlPathEncode(sFileName);
}
if (context.Request.UserAgent.ToLower().IndexOf("firefox") > -1)
{
context.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + sFileName + "\"");
}
else
{
context.Response.AddHeader("Content-Disposition", "attachment;filename=" + sFileName);
}
#endregion
context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
context.Response.ContentType = "application/ms-excel";
context.Response.BinaryWrite(bf);
#endregion

}

protected void CreateRow(ISheet _sheet, ViewModel info, ref int _currentRow, ICellStyle _CellStyle)

{

IRow newRow = _sheet.CreateRow(++_currentRow);
newRow.CreateCell(0).SetCellValue(info.ID);
newRow.CreateCell(1).SetCellValue(info.UserName);
newRow.CreateCell(2).SetCellValue(info.Mobile);
newRow.CreateCell(3).SetCellValue(CommonUtilities.GetCompanyStatus(info.Status));

}

MVC下载Excel的更多相关文章

  1. ASP.NET MVC下载excel文档

    问题来自论坛: 很早以前,学习做asp.net练习时,就是分享过<ASP.NET MVC应用程序实现下载功能>http://www.cnblogs.com/insus/p/3615714. ...

  2. ASP.NET MVC实现Excel文件的上传下载

    在应用系统开发当中,文件的上传和下载是非常普遍的需求.在基于.NET的C/S架构的项目开发当中,有多种方案可以实现文件的上传和下载(httpwebrequest.webclient等),而且多采用异步 ...

  3. MVC导出Excel,提供下载Excel

    类1: using System.Collections.Generic;using System.Data;using System.Web.Mvc;using System.IO;using Sy ...

  4. ASP.NET MVC导入excel到数据库

    MVC导入excel和webform其实没多大区别,以下为代码: 视图StationImport.cshtml的代码: @{ ViewBag.Title = "StationImport&q ...

  5. ASP.NET MVC导出excel

    ASP.NET MVC导出excel 要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式 ...

  6. Asp.net mvc 下载文件

    前言 最近有需求需要下载文件,可能是image的图片,也可能是pdf报告,也可能是微软的word或者excel文件. 这里就整理了asp.net mvc 和asp.net webapi 下载的方法 A ...

  7. 360浏览器下载excel问题解决方式

    亲们有没有碰到过今天我遇到的这件事. 如果使用简单的链接.或者get方式提交的表单,去下载excel,那么360浏览器就会有问题. 问题是:它没把我用java生成的excel表格下载,而是去把我的列表 ...

  8. mvc导出excel 之 新

    前段时间做的mvc导出excel 老大说要进行优化,我原来导出是用npoi插件进行导出,格式是将数据放入到datatable中,然后进行导出. 说要优化的时候就想着将datatable数据导出格式改为 ...

  9. MVC下载文件方式

    MVC下载文件方式 http://www.cnblogs.com/liang--liang/archive/2012/10/20/2732745.html 方式一: public FileStream ...

随机推荐

  1. 利用新版ShareSDK进行手动分享(自定义分享界面)

    之前有用过Share SDK进行快捷分享,可是官方demo中的快捷分享的界面已经设置死了,而公司的产品又设计了自己的分享界面,这就需要我进行手动分享了.当前ShareSDK版本是2.5.4. 看了一堆 ...

  2. c# 获取移动硬盘信息、监听移动设备的弹出与插入事件

    原文 http://www.cnblogs.com/coolkiss/p/3328825.html 备忘一下改功能,主要通过WMI来实现,对于监听外接设备的弹出和插入事件一开始使用IntPtr Wnd ...

  3. [Drools]JAVA规则引擎 -- Drools

    Drools是一个基于Java的规则引擎,开源的,可以将复杂多变的规则从硬编码中解放出来,以规则脚本的形式存放在文件中,使得规则的变更不需要修正代码重启机器就可以立即在线上环境生效. 本文所使用的de ...

  4. restful_api

    http://www.ruanyifeng.com/blog/2014/05/restful_api.html

  5. 九度OJ 题目1534:数组中第K小的数字(二分解)

    题目链接:点击打开链接 题目描述: 给定两个整型数组A和B.我们将A和B中的元素两两相加可以得到数组C. 譬如A为[1,2],B为[3,4].那么由A和B中的元素两两相加得到的数组C为[4,5,5,6 ...

  6. 每天学点Linux:一

    软链接和硬链接: 软链接,又称符号链接,它的原理是通过一个文本文件记录真实文件在系统中的位置,然后在文件操作的时候通过该地址查找原文件然后对其操作.类似于Windows里面的快捷方式.软链接可以链接不 ...

  7. mongoose post方法总结and疑点

    官方文档代码: var schema = new Schema(..); schema.post('save', function (doc) { console.log('this fired af ...

  8. 链表-Reverse Linked List II

    [题目要求直接翻转链表,而非申请新的空间] 这道题的一个关键在于,当m=1时,需要翻转的链表段前没有其他的结点(leetcode的测试用例不含头结点),这个特例给解题带来了一点小小的困难.一个比较直观 ...

  9. c++打印环境变量

    直接上代码:cpp版本 #include <stdio.h> #include <stdlib.h> #include <string.h> extern char ...

  10. Zookeeper 编程

    ZooKeeper编程(一) 杂记 ZooKeeper的用途:distributed coordination;maintaining configuration information, namin ...