麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

MVC下載Excel

2019-11-14 13:41:29
字體:
來源:轉載
供稿:網友

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

}

 


上一篇:初次來訪

下一篇:在PetaPoco中使用Wherein

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产亚洲精品久久午夜玫瑰园 | 国产亚洲精品久久午夜玫瑰园 | 免费黄色小网站 | 亚洲精品成人久久久 | 久久人人爽人人爽人人片av免费 | 国产 日韩 亚洲 欧美 | 欧美三级日本三级少妇99 | wankz100%videos | 黄色大片在线免费观看 | 久久恋 | 国产男女 爽爽爽爽视频 | caoporn国产一区二区 | 国产精品亚洲综合一区二区三区 | 欧美中文在线 | 免费观看视频在线观看 | 欧美一级做a | 国产69精品福利视频 | 蜜桃网站在线 | 精品一区二区免费视频视频 | hd porn 4k video xhicial | 真人一级毛片免费 | 成人毛片免费播放 | 最近免费观看高清韩国日本大全 | 性 毛片 | 久久久久免费精品国产小说色大师 | 久久久久久久久免费 | 337p日本欧洲亚洲大胆精蜜臀 | 草操影院| 黄www片| 久久草草影视免费网 | 久久亚洲视频网 | 91久久久久 | 中文字幕在线第二页 | 久久精品亚洲精品国产欧美kt∨ | 九九热久久免费视频 | 久久国产成人精品国产成人亚洲 | 精品国产一区二区三区久久久蜜 | 国产一级免费片 | 视屏一区 | 农村寡妇偷毛片一级 | 嫩草91在线 |