惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

AI
AI
TaoSecurity Blog
TaoSecurity Blog
H
Heimdal Security Blog
Help Net Security
Help Net Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
N
News | PayPal Newsroom
V2EX - 技术
V2EX - 技术
博客园 - 【当耐特】
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Secure Thoughts
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
有赞技术团队
有赞技术团队
S
Schneier on Security
S
SegmentFault 最新的问题
Google Online Security Blog
Google Online Security Blog
H
Hacker News: Front Page
The Last Watchdog
The Last Watchdog
Schneier on Security
Schneier on Security
PCI Perspectives
PCI Perspectives
IT之家
IT之家
Project Zero
Project Zero
博客园 - 司徒正美
P
Privacy International News Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Security Latest
Security Latest
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
V
Vulnerabilities – Threatpost
W
WeLiveSecurity
NISL@THU
NISL@THU
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
L
Lohrmann on Cybersecurity

博客园 - Leo(binbin)

Vue的生命周期 博文阅读密码验证 - 博客园 博文阅读密码验证 - 博客园 hmtl弹出框样式 博文阅读密码验证 - 博客园 博文阅读密码验证 - 博客园 博文阅读密码验证 - 博客园 博文阅读密码验证 - 博客园 博文阅读密码验证 - 博客园 博文阅读密码验证 - 博客园 在星巴克买咖啡思考技术团队的管理 RealTimePerformanceDemoView scichart by Kline 博文阅读密码验证 - 博客园 博文阅读密码验证 - 博客园 开放英语第三册 DevExpress ChartControl大数据加载时有哪些性能优化方法 博文阅读密码验证 - 博客园 dev
在.NET中使用EPPlus生成Excel报表 .
Leo(binbin) · 2014-06-21 · via 博客园 - Leo(binbin)

--摘抄自:http://blog.csdn.net/zhoufoxcn/article/details/14112473

在开发.NET应用中可能会遇到需要生成带图表(Chart)的Excel报表的需求,特别是在一些ASP.NET网站中,有时候我们并不能保证Web服务器上一定安装了Office组件,所以使用微软的Office来生成Excel并不保证在所有情况下都使用,有时候即使Web服务器上安装了Office也会出现一些运行权限方面的原因到导致调用Excel组件生成Excel失败,所以在这里介绍一种无需安装Office并且无需较高权限就能生成Excel的方法。
  EPPlus简介
  在介绍EPPlus之前,首先要介绍一下Office Open XML。以下文字来自于维基百科(网址:维基百科中Office Open XML的介绍,有删节):
  Office Open XML(缩写:Open XML、OpenXML或OOXML),是由Microsoft开发的一种以XML为基础并以ZIP格式压缩的电子文件,支持Word、Excel、Office Note、PPT等文件格式。OOXML在2006年12月成为了ECMA规范的一部分,编号为ECMA-376;并于2008年4月国际标准化组织(ISO)的表決,在两个月公布为ISO/IEC 29500国际标准。从Microsoft Office 2007开始,Office Open XML文件格式已经成为Microsoft Office默认的文件格式。Microsoft Office 2010支持对ECMA-376标准文档的读操作,ISO/IEC 29500 Transitional的读/写,ISO/IEC 29500 Strict的读取。Microsoft Office 2013同时支持ISO/IEC 29500 Strict的读写操作。
  EPPlus就是一个通过Open XML方式来读写Office文件的开源.NET类库,所以使用它生成Office文件完全不需要Microsoft Office(当然如果你需需要查看生成的文件就需要Office了)。它的官方网址是:http://epplus.codeplex.com/。如果需要体验本文中提到的效果,需要从这个网址下载最新版本的类库,我现在使用的这个名为EPPlus.dll类库仅658K,非常方便部署。
  代码示例
  为了演示EPPlus的用法,这里写了一个简单的例子,在这个例子里演示模拟了几大公司实际业绩与计划业绩的百分比,如果这个百分比大于95%则会将所在的单元格显示为绿色,如果小于90%则会显示为红色,否则就显示为黄色,并且还会生成一个图标来直观表示每月实际完成情况与计划的百分比。

  完整代码如下:

  1.   
  2.   using System;  
  3.   using System.Data;  
  4.   using System.Drawing;  
  5.   using System.IO;  
  6.   using OfficeOpenXml;  
  7.   using OfficeOpenXml.Drawing.Chart;  
  8.   using OfficeOpenXml.Style;  
  9.     
  10.   namespace ExcelReportApplication  
  11.   {  
  12.       
  13.       
  14.       
  15.       
  16.       
  17.       
  18.       
  19.       public class ExcelExportPage  
  20.       {  
  21.           private static readonly string[] MonthNames = new string[] { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};  
  22.           
  23.           private static readonly string[] CommpanyNames = new string[] { "Microsoft", "IBM", "Oracle", "Google","Yahoo", "HP" };  
  24.     
  25.           static void Main(string[] args)  
  26.           {  
  27.               ExcelExportPage.GenerateExcelReport();  
  28.           }  
  29.     
  30.           
  31.           
  32.           
  33.           public static void GenerateExcelReport()  
  34.           {  
  35.               string fileName = "ExcelReport-"+DateTime.Now.ToString("yyyy_MM_dd_HHmmss") + ".xlsx";  
  36.               string reportTitle = "2013年度五大公司实际情况与原计划的百分比";  
  37.               FileInfo file = new FileInfo("C:\\"+fileName);  
  38.               using (ExcelPackage package = new ExcelPackage(file))  
  39.               {  
  40.                   ExcelWorksheet worksheet = null;  
  41.                   ExcelChartSerie chartSerie = null;  
  42.                   ExcelLineChart chart = null;  
  43.                   #region research  
  44.                   worksheet = package.Workbook.Worksheets.Add("Data");  
  45.                   DataTable dataPercent = GetDataPercent();  
  46.                   
  47.                   chart = worksheet.Drawings.AddChart("ColumnStackedChart", eChartType.LineMarkers) as ExcelLineChart;
  48.                   chart.Legend.Position = eLegendPosition.Right;  
  49.                   chart.Legend.Add();  
  50.                   chart.Title.Text = reportTitle;
  51.                   
  52.                   chart.SetSize(800, 400);
  53.                   chart.ShowHiddenData = true;  
  54.                   
  55.                   chart.XAxis.MinorUnit = 1;
  56.                   
  57.                   chart.DataLabel.ShowPercent = true;
  58.     
  59.                   
  60.                   for (int col = 1; col <= dataPercent.Columns.Count; col++)  
  61.                   {  
  62.                       worksheet.Cells[1, col].Value = dataPercent.Columns[col - 1].ColumnName;  
  63.                   }  
  64.                   
  65.                   for (int row = 1; row <= dataPercent.Rows.Count; row++)  
  66.                   {  
  67.                       for (int col = 1; col <= dataPercent.Columns.Count; col++)  
  68.                       {  
  69.                           string strValue = dataPercent.Rows[row - 1][col - 1].ToString();  
  70.                           if (col == 1)  
  71.                           {  
  72.                               worksheet.Cells[row + 1, col].Value = strValue;  
  73.                           }  
  74.                           else  
  75.                           {  
  76.                               double realValue = double.Parse(strValue);  
  77.                               worksheet.Cells[row + 1, col].Style.Fill.PatternType = ExcelFillStyle.Solid;  
  78.                               worksheet.Cells[row + 1, col].Style.Numberformat.Format = "#0\\.00%";
  79.                               worksheet.Cells[row + 1, col].Value = realValue;  
  80.                               if (realValue< 0.90d)
  81.                               {  
  82.     
  83.                                   worksheet.Cells[row + 1, col].Style.Fill.BackgroundColor.SetColor(Color.Red);  
  84.                               }  
  85.                               else if (realValue>= 0.90d && realValue <= 0.95d)
  86.                               {  
  87.                                   worksheet.Cells[row + 1, col].Style.Fill.BackgroundColor.SetColor(Color.Yellow);  
  88.                               }  
  89.                               else  
  90.                               {  
  91.                                   worksheet.Cells[row + 1, col].Style.Fill.BackgroundColor.SetColor(Color.Green);
  92.                               }  
  93.                           }  
  94.                       }  
  95.                       
  96.                       
  97.                       
  98.                       chartSerie = chart.Series.Add(worksheet.Cells[row + 1, 2, row + 1, 2 + dataPercent.Columns.Count - 2], worksheet.Cells["B1:M1"]);  
  99.                       chartSerie.HeaderAddress = worksheet.Cells[row + 1, 1];
  100.                   }  
  101.                   
  102.                   chart.YAxis.MinValue = 0.8d;  
  103.                   
  104.                   
  105.                   
  106.                   chart.SetPosition(CommpanyNames.Length + 1, 10, 3, 20);  
  107.                   #endregion research  
  108.                   package.Save();
  109.               }  
  110.           }  
  111.     
  112.           
  113.           
  114.           
  115.           
  116.           private static DataTable GetDataPercent()  
  117.           {  
  118.               DataTable data = new DataTable();  
  119.               DataRow row = null;  
  120.               Random random=new Random();  
  121.               data.Columns.Add(new DataColumn("公司名", typeof(string)));  
  122.               foreach(string monthName in MonthNames){  
  123.                   data.Columns.Add(new DataColumn(monthName, typeof(double)));  
  124.               }  
  125.               
  126.               for (int i = 0; i < CommpanyNames.Length; i++)  
  127.               {  
  128.                   row = data.NewRow();  
  129.                   row[0] = CommpanyNames[i];  
  130.                   for (int j = 1; j <= MonthNames.Length; j++)  
  131.                   {  
  132.                       
  133.                       row[j] = 0.85d + random.Next(0, 15) / 100d;  
  134.                   }  
  135.                   data.Rows.Add(row);  
  136.               }  
  137.                 
  138.     
  139.               return data;  
  140.           }  
  141.       }  
  142.   }  
  /*
   ********************************************************************************
   *
   *    Project           A report project
   *    Module Name       Excel Report
   *    Author            Zhou, Jin-Qiao (周金桥)
   *    Creation Date     [11/03/2013]
   *    Description       Generate Excel Report with Chat demo by Epplus
   *    
   *    © Copyright 2013 zhoufoxcn.
   *    
   ********************************************************************************
   */
  using System;
  using System.Data;
  using System.Drawing;
  using System.IO;
  using OfficeOpenXml;
  using OfficeOpenXml.Drawing.Chart;
  using OfficeOpenXml.Style;
  
  namespace ExcelReportApplication
  {
      /// <summary>  
      /// 使用EPPlus生成带图表(Chart)的Excel文件的例子,注意在运行的机器上无需安装Office,因为EPPlus是使用基于OpenXML技术生成的Excel文件。
      /// 任何网站和个人均可在不对本代码做任何修改的情况下转载本文及本文中示例的代码用于非商业用途,任何除去版权的行为均为侵权。
      /// 作者:周公(周金桥)   
      /// 创建日期:2013-11-03   
      /// 博客地址:http://blog.csdn.net/zhoufoxcn http://zhoufoxcn.blog.51cto.com   
      /// 新浪微博地址:http://weibo.com/zhoufoxcn
      public class ExcelExportPage
      {
          private static readonly string[] MonthNames = new string[] { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};
          //private static readonly string[] CommpanyNames = new string[] { "Microsoft", "IBM", "Oracle", "Amazon", "Google", "Facebook", "Twitter", "Paypal", "Yahoo", "HP" };
          private static readonly string[] CommpanyNames = new string[] { "Microsoft", "IBM", "Oracle", "Google","Yahoo", "HP" };
  
          static void Main(string[] args)
          {
              ExcelExportPage.GenerateExcelReport();
          }
  
          /// <summary>
          /// 周公(周金桥)说明:这个方法就是主要演示如何生成带图表(Chart)的Excel文件的例子
          /// </summary>
          public static void GenerateExcelReport()
          {
              string fileName = "ExcelReport-"+DateTime.Now.ToString("yyyy_MM_dd_HHmmss") + ".xlsx";
              string reportTitle = "2013年度五大公司实际情况与原计划的百分比";
              FileInfo file = new FileInfo("C:\\"+fileName);
              using (ExcelPackage package = new ExcelPackage(file))
              {
                  ExcelWorksheet worksheet = null;
                  ExcelChartSerie chartSerie = null;
                  ExcelLineChart chart = null;
                  #region research
                  worksheet = package.Workbook.Worksheets.Add("Data");
                  DataTable dataPercent = GetDataPercent();
                  //chart = Worksheet.Drawings.AddChart("ColumnStackedChart", eChartType.Line) as ExcelLineChart;
                  chart = worksheet.Drawings.AddChart("ColumnStackedChart", eChartType.LineMarkers) as ExcelLineChart;//设置图表样式
                  chart.Legend.Position = eLegendPosition.Right;
                  chart.Legend.Add();
                  chart.Title.Text = reportTitle;//设置图表的名称
                  //chart.SetPosition(200, 50);//设置图表位置
                  chart.SetSize(800, 400);//设置图表大小
                  chart.ShowHiddenData = true;
                  //chart.YAxis.MinorUnit = 1;
                  chart.XAxis.MinorUnit = 1;//设置X轴的最小刻度
                  //chart.DataLabel.ShowCategory = true;
                  chart.DataLabel.ShowPercent = true;//显示百分比
  
                  //设置月份
                  for (int col = 1; col <= dataPercent.Columns.Count; col++)
                  {
                      worksheet.Cells[1, col].Value = dataPercent.Columns[col - 1].ColumnName;
                  }
                  //设置数据
                  for (int row = 1; row <= dataPercent.Rows.Count; row++)
                  {
                      for (int col = 1; col <= dataPercent.Columns.Count; col++)
                      {
                          string strValue = dataPercent.Rows[row - 1][col - 1].ToString();
                          if (col == 1)
                          {
                              worksheet.Cells[row + 1, col].Value = strValue;
                          }
                          else
                          {
                              double realValue = double.Parse(strValue);
                              worksheet.Cells[row + 1, col].Style.Fill.PatternType = ExcelFillStyle.Solid;
                              worksheet.Cells[row + 1, col].Style.Numberformat.Format = "#0\\.00%";//设置数据的格式为百分比
                              worksheet.Cells[row + 1, col].Value = realValue;
                              if (realValue< 0.90d)//如果小于90%则该单元格底色显示为红色
                              {
  
                                  worksheet.Cells[row + 1, col].Style.Fill.BackgroundColor.SetColor(Color.Red);
                              }
                              else if (realValue>= 0.90d && realValue <= 0.95d)//如果在90%与95%之间则该单元格底色显示为黄色
                              {
                                  worksheet.Cells[row + 1, col].Style.Fill.BackgroundColor.SetColor(Color.Yellow);
                              }
                              else
                              {
                                  worksheet.Cells[row + 1, col].Style.Fill.BackgroundColor.SetColor(Color.Green);//如果大于95%则该单元格底色显示为绿色
                              }
                          }
                      }
                      //chartSerie = chart.Series.Add(worksheet.Cells["A2:M2"], worksheet.Cells["B1:M1"]);
                      //chartSerie.HeaderAddress = worksheet.Cells["A2"];
                      //chart.Series.Add()方法所需参数为:chart.Series.Add(X轴数据区,Y轴数据区)
                      chartSerie = chart.Series.Add(worksheet.Cells[row + 1, 2, row + 1, 2 + dataPercent.Columns.Count - 2], worksheet.Cells["B1:M1"]);
                      chartSerie.HeaderAddress = worksheet.Cells[row + 1, 1];//设置每条线的名称
                  }
                  //因为假定每家公司至少完成了80%以上,所以这里设置Y轴的最小刻度为80%,这样使图表上的折线更清晰
                  chart.YAxis.MinValue = 0.8d;
                  //chart.SetPosition(200, 50);//可以通过制定左上角坐标来设置图表位置
                  //通过指定图表左上角所在的行和列及对应偏移来指定图表位置
                  //这里CommpanyNames.Length + 1及3分别表示行和列
                  chart.SetPosition(CommpanyNames.Length + 1, 10, 3, 20);
                  #endregion research
                  package.Save();//保存文件
              }
          }
  
          /// <summary>
          /// 生成数据,由于这一步不是主要逻辑,所以采用随机生成数据的方式,实际中可根据需要从数据库或其它数据源中读取需要的数据
          /// </summary>
          /// <returns></returns>
          private static DataTable GetDataPercent()
          {
              DataTable data = new DataTable();
              DataRow row = null;
              Random random=new Random();
              data.Columns.Add(new DataColumn("公司名", typeof(string)));
              foreach(string monthName in MonthNames){
                  data.Columns.Add(new DataColumn(monthName, typeof(double)));
              }
              //每个公司每月的百分比表示完成的业绩与计划的百分比
              for (int i = 0; i < CommpanyNames.Length; i++)
              {
                  row = data.NewRow();
                  row[0] = CommpanyNames[i];
                  for (int j = 1; j <= MonthNames.Length; j++)
                  {
                      //这里采用了随机生成数据,但假定每家公司至少完成了计划的85%以上
                      row[j] = 0.85d + random.Next(0, 15) / 100d;
                  }
                  data.Rows.Add(row);
              }
              
  
              return data;
          }
      }
  }

最终生成的Excel文件内容如下: