zoukankan      html  css  js  c++  java
  • asp.net using library ClosedXML to export excel

    Reference:  http://closedxml.codeplex.com/ 

    1. First add refenrence ClosedXML.dll and DocumentFormat.OpenXml.dll to the project. 

    2. add namespace: 

    using ClosedXML.Excel;
    using System.IO;




    3. Method:

    public void ExportDataToExcel(DataTable dt, string fileName)
    		{
    			using (XLWorkbook wb = new XLWorkbook())
    			{
     
    				var ws = wb.Worksheets.Add(dt, "ws");  // worksheets name must be added. 
    				wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
    				wb.Style.Font.Bold = true;
     
    				//set header style
    				ws.Rows(1, 1).Style.Fill.BackgroundColor = XLColor.White;
    				ws.Rows(1, 1).Style.Font.Bold = true;
    				ws.Rows(1, 1).Style.Font.FontColor = XLColor.Onyx;
    				ws.Columns().Width = 20;
     
    				//remove AutoFilter
    				ws.Tables.FirstOrDefault().ShowAutoFilter = false;
    				
     
    				Response.Clear();
    				Response.Buffer = true;
    				Response.Charset = "utf-8";
    				Response.ContentType = "application/vnd.ms-excel";
    				Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
     
    				using (MemoryStream MyMemoryStream = new MemoryStream())
    				{
    					wb.SaveAs(MyMemoryStream);
    					MyMemoryStream.WriteTo(Response.OutputStream);
    					Response.Flush();
    					Response.End();
    				}
    			}
    		}

  • 相关阅读:
    Wooden Sticks(hdu1051)
    Leftmost Digit(hdu1060)(数学题)
    Sum of Remainders(数学题)
    Brain Network (medium)(DFS)
    Brain Network (easy)(并查集水题)
    Collective Mindsets (medium) (逻辑题)
    Collective Mindsets (easy)(逻辑题)
    RMQ with Shifts(线段树)
    Throwing Dice(概率dp)
    圆桌会议
  • 原文地址:https://www.cnblogs.com/facial/p/5142832.html
Copyright © 2011-2022 走看看