MVC下载Excel

方法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));

}

 

转载于:https://www.cnblogs.com/liu-xia/p/5025556.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JSP(JavaServer Pages)是一种动态网页技术,允许开发者将Java代码嵌入到HTML页面中。当Web服务器接收到对JSP页面的请求时,它将执行页面中的Java代码,并将结果嵌入到生成的HTML中发送给客户端。 MVC(Model-View-Controller)是一种设计模式,用于将应用程序分为三个核心组件:模型(Model)、视图(View)和控制器(Controller)。模型负责数据和业务逻辑,视图负责展示数据,控制器负责接收用户输入并调用模型和视图去完成用户的请求。 导出Excel通常是指将Web应用中的数据导出为Microsoft Excel可识别的文件格式,比如.xlsx或.xls。在JSP中使用MVC模式导出Excel,通常需要以下步骤: 1. 创建数据模型(Model):定义需要导出的数据结构。 2. 创建视图(View):设计导出数据的布局,通常是HTML表格。 3. 创建控制器(Controller):编写处理导出逻辑的代码,包括接收用户的导出请求、数据的检索和转换、以及响应的生成和发送。 具体到导出Excel,控制器需要生成Excel文件的数据流。可以使用Apache POI库来帮助处理Excel文件的创建和写入。然后,控制器会将这个文件流作为HTTP响应发送给客户端,通常会设置响应头,告知浏览器这是一个附件,浏览器会提示用户保存文件。 纯手写代码的话,你需要处理以下几个方面: - Java代码的组织,确保按照MVC模式分离逻辑。 - 在视图层面,通过JSP标签生成静态HTML表格。 - 在控制器层面,使用Java代码处理HTTP请求和响应,以及调用Apache POI或其他库来生成Excel文件。 - 设置正确的HTTP响应头,使得浏览器能够识别并处理生成的文件。 以下是一个简化的示例步骤: 1. 引入Apache POI依赖到你的项目中。 2. 在控制器中,编写导出数据的逻辑,创建一个HSSFWorkbook或XSSFWorkbook对象(取决于你想要创建的是旧版的.xls还是新版的.xlsx文件)。 3. 使用Apache POI提供的API填充工作簿、工作表、行和单元格等。 4. 将工作簿对象写入到HTTP响应输出流中。 5. 设置响应的内容类型为Excel文件类型,并添加头信息提示浏览器将响应作为文件下载。 这里没有提供具体的代码实现,因为编写完整的代码超出了简短回答的范围,但它可以根据上述步骤来完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值