国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

MVC下載Excel

2019-11-14 13:41:29
字體:
供稿:網(wǎng)友

方法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", "手機(jī)號碼"),
new ColumnMap("Status", "賬戶狀態(tài)"));

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

}

方法2:

public ActionResult DownLoadExcel()

{

var list=new List();//list,根據(jù)情況取數(shù)據(jù)

if (list!= null && list.Count > 0)
{
     //下載數(shù)據(jù)-導(dǎo)Excel
      CreateExcel(list, (HttpContextBase)HttpContext);

}
return null;

}

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


IWorkbook workbook = new HSSFWorkbook();//創(chuàng)建Workbook對象
ISheet sheet = workbook.CreateSheet("Sheet1");//創(chuàng)建工作表

#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 生成標(biāo)題行
//Title
string[] arrStr = { "編號",  "聯(lián)系人", "手機(jī)","狀態(tài)" };
int[] arrWidth = { 12, 30, 24, 20};

IRow row = sheet.CreateRow(0); //在工作表中標(biāo)題行
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;
//生成數(shù)據(jù)行

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 設(shè)定文件名
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));

}

 


上一篇:初次來訪

下一篇:在PetaPoco中使用Wherein

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 岑巩县| 双鸭山市| 屏山县| 德化县| 潞西市| 浦城县| 翼城县| 天峻县| 湛江市| 轮台县| 本溪市| 灌阳县| 临沧市| 望江县| 泗阳县| 韶山市| 新密市| 滨州市| 沛县| 杭锦旗| 申扎县| 瑞丽市| 屯留县| 德格县| 乌海市| 石阡县| 崇明县| 田林县| 论坛| 栾川县| 寿光市| 汝城县| 新邵县| 神农架林区| 阿拉尔市| 黄龙县| 忻州市| 霍邱县| 渑池县| 荆州市| 方山县|