前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >EPPlus与Excel完美的结合

EPPlus与Excel完美的结合

作者头像
跟着阿笨一起玩NET
发布2018-09-19 15:24:10
2.5K0
发布2018-09-19 15:24:10
举报
文章被收录于专栏:跟着阿笨一起玩NET

笔者近期在公司项目中需要生产比较复杂的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代码如下:

代码语言:javascript
复制
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的键值对!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2012-07-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云 BI
腾讯云 BI(Business Intelligence,BI)提供从数据源接入、数据建模到数据可视化分析全流程的BI能力,帮助经营者快速获取决策数据依据。系统采用敏捷自助式设计,使用者仅需通过简单拖拽即可完成原本复杂的报表开发过程,并支持报表的分享、推送等企业协作场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档