Epplus:导出Excel
看到其它大神的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相关推荐
- EPPlus导出Excel感觉很不错~~~
前言 导出成为很多系统的必备功能,之前分享过导出PDF的功能,这里来分享一下Excel的导出: 提到Excel导出,NPOI肯定是很多小伙伴的首选,在以往的项目中也用其完成了很多导出需求:对于NPOI ...
- C# 使用Epplus导出Excel [4]:合并指定行
C# 使用Epplus导出Excel [1]:导出固定列数据 C# 使用Epplus导出Excel [2]:导出动态列数据 C# 使用Epplus导出Excel [3]:合并列连续相同数据 C# 使用 ...
- .NetCore+EPPlus导出Excel报表
.NetCore+EPPlus导出Excel报表 适用于跨平台部署 NuGet引入EPPlus.Core 定义公共调用方法 完成调用 NuGet引入EPPlus.Core 因为本人使用的框架是.Net ...
- C# EPPlus导出EXCEL,并生成Chart表
一 在negut添加EPPlus.dll库文件. 之前有写过直接只用Microsoft.Office.Interop.Excel 导出EXCEL,并生成Chart表,非常耗时,所以找了个EPPlus ...
- epplus保存为流_通过EPPlus导出Excel文件
小弟刚刚走上工作岗位不久,工作中常常遇到要导出Excel的情况,但感觉两种常见的导出方式(一种是通过GridView,另一种是通过微软的COM组 件)都不是很理想,前者导出的灵活度太低,后者又常常出现 ...
- C# 使用Epplus导出数据到Excel
简介:Epplus是一个使用Open Office XML(Xlsx)文件格式,能读写Excel 2007/2010文件的开源组件 功效:支持对excel文档的汇入汇出,图表(excel自带的图表基本 ...
- 导出Excel之Epplus使用教程2(样式设置)
导出Excel之Epplus使用教程1(基本介绍) 导出Excel之Epplus使用教程2(样式设置) 导出Excel之Epplus使用教程3(图表设置) 导出Excel之Epplus使用教程4(其他 ...
- epplus保存为流_ASP.NET Core使用EPPlus导入导出Excel
开发过程中,经常会遇到导入导出数据的需求,本篇博客介绍在.NET Core中如何使用EPPlus组件导入导出Excel EPPlus: EPPlus是使用Open Office XML格式(xlsx) ...
- C#导出EXCEL之EPPlus的使用
1.EPPlus是一个使用Open Office XML(xlsx)文件格式,能读写Excel 2007/2010 文件的开源组件,在导出Excel的时候不需要电脑上安装office,官网为:http ...
最新文章
- Java基础笔记之数据类型
- 前端学习(1739):前端调试值之页面元素的调试技巧
- 32位jdk最大内存_Java安装之JDK下载篇
- 字符串json转成json对象
- python error: no module named pylab的解决
- 关于springmvc配置validator的注意事项
- 微型计算机汇编用什么软件,EMU8086 汇编工具软件的使用
- C# string 保留数字英文字母
- 模拟文曲星上的猜数游戏c语言,文曲星上的“猜数字”游戏Python版
- Python画美队盾牌
- 解决谷歌浏览器打开控制台有延迟
- 笔记本 使用 Opencore 安装 苹果系统过程
- java的逻辑判断和结构
- 面试官最想听哪些话?
- HTTPS为啥子安全?
- 1949-2020年各省全要素生产率(年度)
- 如何制作数据可视化网页
- qq发送编程相关的命令或代码时,被转成表情该怎么解决
- 公网SSH远程Ubuntu:安装cpolar内网穿透映射22端口 2/3
- iOS 相机 点击拍照,长按录像