打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
EPPlus与Excel完美的结合

笔者近期在公司项目中需要生产比较复杂的Excel报表, 问题点是单个Excel文件中必须能包含多个sheet, 按照以前项目的经验, 此情况需要使用MS Office组件实现。但是客观情况是office组件其版本兼容问题比较多(Excel版本不一致导致无法使用、excel进程无法回收、导致w3wp进程崩溃等), 无法把控摒弃之。

在codeplex上看到EPPlus组件, 好像可以解决上述问题。

EPPlus使用的是Open Office XML Format, 其读写支持Excel 2003/2007。

项目地址:http://epplus.codeplex.com/

在asp.net页面上导出Excel代码如下:

public static void DumpExcel(HttpContext context,string flieName,IDictionary<string,DataTable> dict)        {            using (ExcelPackage pck = new ExcelPackage())            {                foreach (var kp in dict)                {                    //Create the worksheet                    ExcelWorksheet ws = pck.Workbook.Worksheets.Add(kp.Key);                    //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1                    ws.Cells["A1"].LoadFromDataTable(kp.Value, true);                    ////Format the header for column 1-3                    //using (ExcelRange rng = ws.Cells["A1:C1"])                    //{                    //    rng.Style.Font.Bold = true;                    //    rng.Style.Fill.PatternType = ExcelFillStyle.Solid;                      //Set Pattern for the background to Solid                    //    rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(79, 129, 189));  //Set color to dark blue                    //    rng.Style.Font.Color.SetColor(Color.White);                    //}                    ////Example how to Format Column 1 as numeric                     //using (ExcelRange col = ws.Cells[2, 1, 2 + tbl.Rows.Count, 1])                    //{                    //    col.Style.Numberformat.Format = "#,##0.00";                    //    col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;                    //}                }                //Write it back to the client                var data = pck.GetAsByteArray();                context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";                context.Response.AddHeader("content-disposition", "attachment;  filename=" + flieName + ".xlsx");                context.Response.AddHeader("Content-Length", data.Length.ToString());                context.Response.BinaryWrite(data);            }        }

参数dict传递的是sheetname和DataTable的键值对!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Advanced Excels With EPPlus
Excel VBA语句集
c#中Excel数据的导入、导出
自学资料(Excel VBA)[收集整理3]
Excel的Range对象(C#)
Excel VBA 电子发票管理助手/重磅更新/电子发票登记管理系统(EXCEL版)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服