看到其它大神的Epplus导出Excel,结合写出符合自己需求的将导出数据到Excel,给其它人参考一下,也可以学习http://www.cnblogs.com/caofangsheng/p/6149843.html#3576824灰太狼的梦想(大神)的教程。

视图:

1 <input type="button" value="订单导出" class="baseExport baseBtn" style="margin-right: 12px;" οnclick="ExportToExcel()" />
2
3 <script>
4 function ExportToExcel() {
5 document.location = "/Order/ExportToExcel";
6 } <script/>

控制器:

1 public FileContentResult ExportToExcel()
2 {
3 var where = "SQL语句 ";
4 List<AmazonOrdersCollectView> Export = SQLHelper.GetCollectReader(where);//查出后台数据
5 string[] columns = { "amazon_order_id", "purchase_date", "sales_channel", "buyer_name", "buyer_email", "buyer_phone_number", "total_Number"};//定义Excel列项
6 byte[] filecontent = ExcelExportHelper.ExportExcel(Export, "", false, columns);
7 return File(filecontent,ExcelExportHelper.ExcelContentType,"text.xlsx");
8 }

类:

  1 using OfficeOpenXml;
  2 using OfficeOpenXml.Style;
  3 using System;
  4 using System.Collections.Generic;
  5 using System.ComponentModel;
  6 using System.Data;
  7 using System.Linq;
  8 using System.Web;
  9
 10 namespace Order_Management.Library.Export
 11 {
 12     public class ExcelExportHelper
 13     {
 14         public static string ExcelContentType
 15         {
 16             get
 17             {
 18                 return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
 19             }
 20         }
 21
 22         /// <summary>
 23         /// List转DataTable
 24         /// </summary>
 25         /// <typeparam name="T"></typeparam>
 26         /// <param name="data"></param>
 27         /// <returns></returns>
 28         public static DataTable ListToDataTable<T>(List<T> data)
 29         {
 30             PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
 31             DataTable dataTable = new DataTable();
 32             for (int i = 0; i < properties.Count; i++)
 33             {
 34                 PropertyDescriptor property = properties[i];
 35                 dataTable.Columns.Add(property.Name, Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType);
 36             }
 37             object[] values = new object[properties.Count];
 38             foreach (T item in data)
 39             {
 40                 for (int i = 0; i < values.Length; i++)
 41                 {
 42                     values[i] = properties[i].GetValue(item);
 43                 }
 44                 dataTable.Rows.Add(values);
 45             }
 46             return dataTable;
 47         }
 48
 49         /// <summary>
 50         /// 导出Excel
 51         /// </summary>
 52         /// <param name="dataTable">数据源</param>
 53         /// <param name="heading">工作簿Worksheet</param>
 54         /// <param name="showSrNo">是否显示行编号</param>
 55         /// <param name="columnsToTale">要导出的列</param>
 56         /// <returns></returns>
 57         public static byte[] ExportExcel(DataTable dataTable,string heading="",bool showSrNo=false,params string[] columnsToTale)
 58         {
 59             byte[] reslut = null;
 60             using (ExcelPackage package=new ExcelPackage())
 61             {
 62                 ExcelWorksheet workSheet  = package.Workbook.Worksheets.Add(string.Format("{0}Data", heading));
 63                 int startRowFrom = string.IsNullOrEmpty(heading) ? 1 : 3;
 64                 if (showSrNo)
 65                 {
 66                     DataColumn dataColumn = dataTable.Columns.Add("#", typeof(int));
 67                     dataColumn.SetOrdinal(0);
 68                     int index = 1;
 69                     foreach (DataRow item in dataTable.Rows)
 70                     {
 71                         item[0] = index;
 72                         index++;
 73                     }
 74                 }
 75                 workSheet.Cells["A" + startRowFrom].LoadFromDataTable(dataTable, true);
 76                 int columnIndex = 1;
 77                 foreach (DataColumn item in dataTable.Columns)
 78                 {
 79                     ExcelRange columnCells = workSheet.Cells[workSheet.Dimension.Start.Row, columnIndex, workSheet.Dimension.End.Row, columnIndex];
 80                     int maxLength = columnCells.Max(cell => cell.Value == null ? 1 : cell.Value.ToString().Count());
 81                     if (maxLength<150)
 82                     {
 83                         workSheet.Column(columnIndex).AutoFit();
 84                     }
 85                     columnIndex++;
 86                 }
 87                 using (ExcelRange r=workSheet.Cells[startRowFrom,1,startRowFrom,dataTable.Columns.Count])
 88                 {
 89                     r.Style.Font.Color.SetColor(System.Drawing.Color.White);
 90                     r.Style.Font.Bold = true;
 91                     r.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
 92                     r.Style.Fill.BackgroundColor.SetColor(System.Drawing.ColorTranslator.FromHtml("#1fb5ad"));
 93                 }
 94                 using (ExcelRange r=workSheet.Cells[startRowFrom+1,1,startRowFrom+dataTable.Rows.Count,dataTable.Columns.Count])
 95                 {
 96                     r.Style.Border.Top.Style = ExcelBorderStyle.Thin;
 97                     r.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
 98                     r.Style.Border.Left.Style = ExcelBorderStyle.Thin;
 99                     r.Style.Border.Right.Style = ExcelBorderStyle.Thin;
100                     r.Style.Border.Top.Color.SetColor(System.Drawing.Color.Black);
101                     r.Style.Border.Bottom.Color.SetColor(System.Drawing.Color.Black);
102                     r.Style.Border.Left.Color.SetColor(System.Drawing.Color.Black);
103                     r.Style.Border.Right.Color.SetColor(System.Drawing.Color.Black);
104                 }
105                 for (int i = 0; i >= dataTable.Columns.Count - 1; i++)
106                 {
107                     if (i == 0 && showSrNo)
108                     {
109                         continue;
110                     }
111                     if (!columnsToTale.Contains(dataTable.Columns[i].ColumnName))
112                     {
113                         workSheet.DeleteRow(i + 1);
114                     }
115                 }
116                 if (!String.IsNullOrEmpty(heading))
117                 {
118                     workSheet.Cells["A1"].Value = heading;
119                     workSheet.Cells["A1"].Style.Font.Size = 20;
120                     workSheet.InsertColumn(1, 1);
121                     workSheet.InsertRow(1, 1);
122                     workSheet.Column(1).Width = 5;
123                 }
124                 reslut = package.GetAsByteArray();
125             }
126             return reslut;
127         }
128
129         /// <summary>
130         /// 导出Excel
131         /// </summary>
132         /// <typeparam name="T"></typeparam>
133         /// <param name="data"></param>
134         /// <param name="heading"></param>
135         /// <param name="isShowSlNo"></param>
136         /// <param name="ColumnsToTake"></param>
137         /// <returns></returns>
138         public static byte[] ExportExcel<T>(List<T> data, string heading = "", bool isShowSlNo = false, params string[] ColumnsToTake)
139         {
140             return ExportExcel(ListToDataTable<T>(data), heading, isShowSlNo, ColumnsToTake);
141         }
142     }

View Code

效果图:

以上就是我的分享,欢迎各路大神一起探讨学习!

转载于:https://www.cnblogs.com/W--Jing/p/7700104.html

Epplus:导出Excel相关推荐

  1. EPPlus导出Excel感觉很不错~~~

    前言 导出成为很多系统的必备功能,之前分享过导出PDF的功能,这里来分享一下Excel的导出: 提到Excel导出,NPOI肯定是很多小伙伴的首选,在以往的项目中也用其完成了很多导出需求:对于NPOI ...

  2. C# 使用Epplus导出Excel [4]:合并指定行

    C# 使用Epplus导出Excel [1]:导出固定列数据 C# 使用Epplus导出Excel [2]:导出动态列数据 C# 使用Epplus导出Excel [3]:合并列连续相同数据 C# 使用 ...

  3. .NetCore+EPPlus导出Excel报表

    .NetCore+EPPlus导出Excel报表 适用于跨平台部署 NuGet引入EPPlus.Core 定义公共调用方法 完成调用 NuGet引入EPPlus.Core 因为本人使用的框架是.Net ...

  4. C# EPPlus导出EXCEL,并生成Chart表

    一  在negut添加EPPlus.dll库文件. 之前有写过直接只用Microsoft.Office.Interop.Excel 导出EXCEL,并生成Chart表,非常耗时,所以找了个EPPlus ...

  5. epplus保存为流_通过EPPlus导出Excel文件

    小弟刚刚走上工作岗位不久,工作中常常遇到要导出Excel的情况,但感觉两种常见的导出方式(一种是通过GridView,另一种是通过微软的COM组 件)都不是很理想,前者导出的灵活度太低,后者又常常出现 ...

  6. C# 使用Epplus导出数据到Excel

    简介:Epplus是一个使用Open Office XML(Xlsx)文件格式,能读写Excel 2007/2010文件的开源组件 功效:支持对excel文档的汇入汇出,图表(excel自带的图表基本 ...

  7. 导出Excel之Epplus使用教程2(样式设置)

    导出Excel之Epplus使用教程1(基本介绍) 导出Excel之Epplus使用教程2(样式设置) 导出Excel之Epplus使用教程3(图表设置) 导出Excel之Epplus使用教程4(其他 ...

  8. epplus保存为流_ASP.NET Core使用EPPlus导入导出Excel

    开发过程中,经常会遇到导入导出数据的需求,本篇博客介绍在.NET Core中如何使用EPPlus组件导入导出Excel EPPlus: EPPlus是使用Open Office XML格式(xlsx) ...

  9. C#导出EXCEL之EPPlus的使用

    1.EPPlus是一个使用Open Office XML(xlsx)文件格式,能读写Excel 2007/2010 文件的开源组件,在导出Excel的时候不需要电脑上安装office,官网为:http ...

最新文章

  1. Java基础笔记之数据类型
  2. 前端学习(1739):前端调试值之页面元素的调试技巧
  3. 32位jdk最大内存_Java安装之JDK下载篇
  4. 字符串json转成json对象
  5. python error: no module named pylab的解决
  6. 关于springmvc配置validator的注意事项
  7. 微型计算机汇编用什么软件,EMU8086 汇编工具软件的使用
  8. C# string 保留数字英文字母
  9. 模拟文曲星上的猜数游戏c语言,文曲星上的“猜数字”游戏Python版
  10. Python画美队盾牌
  11. 解决谷歌浏览器打开控制台有延迟
  12. 笔记本 使用 Opencore 安装 苹果系统过程
  13. java的逻辑判断和结构
  14. 面试官最想听哪些话?
  15. HTTPS为啥子安全?
  16. 1949-2020年各省全要素生产率(年度)
  17. 如何制作数据可视化网页
  18. qq发送编程相关的命令或代码时,被转成表情该怎么解决
  19. 公网SSH远程Ubuntu:安装cpolar内网穿透映射22端口 2/3
  20. iOS 相机 点击拍照,长按录像

热门文章

  1. Pycharm同步git代码提示:Merge failed
  2. 前端学习笔记2017.6.12 CSS控制DIV
  3. Mysql 查看、创建、更改 数据库和表
  4. 4.3、Libgdx启动类和配置
  5. C语言中的位操作(4)--判断整数是否为2的幂
  6. Strong Consistency, 强一致性技术概述
  7. Delphi 2010 refactor / refactoring 重构不能使用的原因以及解决
  8. 基于子类的动态代理:
  9. TimeLine CSS/Javascript 时间线
  10. linux中录屏工具byzanz