zoukankan      html  css  js  c++  java
  • excel export using EPPlus

    Have a look at ExeclPackagePlus http://epplus.codeplex.com

    It's great for these kinds of scenarios.

     private void DumpExcel(DataTable tbl)
        {
            try{
             //   OfficeOpenXml.ExcelPackage
            using (ExcelPackage pck = new ExcelPackage())
            {
                //Create the worksheet
                ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Mailing List");
    
                //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
                ws.Cells["A2"].LoadFromDataTable(tbl, false);
    
                //Header Titles
                ws.Cells["A1"].Value = "Employee Name";
                ws.Cells["B1"].Value = "Email Address";
                ws.Cells["C1"].Value = "Phone";
                ws.Cells["D1"].Value = "Business Unit";
                ws.Cells["E1"].Value = "Site";
    
                ws.Cells["A1"].AutoFitColumns();
    
                //Format the header for column 1-3
                using (ExcelRange rng = ws.Cells["A1:E1"])            {
                  
                    rng.Style.Font.Bold = true;
                    //Set Pattern for the background to Solid
                    rng.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //Set color to dark blue
                    rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(79, 129, 189));
                    rng.Style.Font.Color.SetColor(System.Drawing.Color.White);
                }
    
    
                //Write it back to the client          
    
                Response.Clear();
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment;  filename=MailingList.xlsx");
                Response.BinaryWrite(pck.GetAsByteArray());           
            }
            }
            catch (Exception ex) 
            {
        //log error
            }
               Response.End();
            }
    
  • 相关阅读:
    pip 提示import error,cannot import name locations
    Selenium 报错 提示“unable to find an ant file to run”
    python-unexpected content storage modification出错
    C++对象的动态建立与释放
    C++构造函数和析构函数
    C++类的访问控制关键字
    C++之namespace、bool
    C++ 学习笔记
    内存泄漏
    内存回收机制
  • 原文地址:https://www.cnblogs.com/happy-Chen/p/3622782.html
Copyright © 2011-2022 走看看