程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> MVC下載Excel,mvcexcel

MVC下載Excel,mvcexcel

編輯:C#入門知識

MVC下載Excel,mvcexcel


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

}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved