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

视图:

 <input type="button" value="订单导出" class="baseExport baseBtn" style="margin-right: 12px;" onclick="ExportToExcel()" />

 <script>
function ExportToExcel() {
document.location = "/Order/ExportToExcel";
}
<script/>

控制器:

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

类:

 using OfficeOpenXml;
using OfficeOpenXml.Style;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web; namespace Order_Management.Library.Export
{
public class ExcelExportHelper
{
public static string ExcelContentType
{
get
{
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
} /// <summary>
/// List转DataTable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
public static DataTable ListToDataTable<T>(List<T> data)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable dataTable = new DataTable();
for (int i = ; i < properties.Count; i++)
{
PropertyDescriptor property = properties[i];
dataTable.Columns.Add(property.Name, Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType);
}
object[] values = new object[properties.Count];
foreach (T item in data)
{
for (int i = ; i < values.Length; i++)
{
values[i] = properties[i].GetValue(item);
}
dataTable.Rows.Add(values);
}
return dataTable;
} /// <summary>
/// 导出Excel
/// </summary>
/// <param name="dataTable">数据源</param>
/// <param name="heading">工作簿Worksheet</param>
/// <param name="showSrNo">是否显示行编号</param>
/// <param name="columnsToTale">要导出的列</param>
/// <returns></returns>
public static byte[] ExportExcel(DataTable dataTable,string heading="",bool showSrNo=false,params string[] columnsToTale)
{
byte[] reslut = null;
using (ExcelPackage package=new ExcelPackage())
{
ExcelWorksheet workSheet = package.Workbook.Worksheets.Add(string.Format("{0}Data", heading));
int startRowFrom = string.IsNullOrEmpty(heading) ? : ;
if (showSrNo)
{
DataColumn dataColumn = dataTable.Columns.Add("#", typeof(int));
dataColumn.SetOrdinal();
int index = ;
foreach (DataRow item in dataTable.Rows)
{
item[] = index;
index++;
}
}
workSheet.Cells["A" + startRowFrom].LoadFromDataTable(dataTable, true);
int columnIndex = ;
foreach (DataColumn item in dataTable.Columns)
{
ExcelRange columnCells = workSheet.Cells[workSheet.Dimension.Start.Row, columnIndex, workSheet.Dimension.End.Row, columnIndex];
int maxLength = columnCells.Max(cell => cell.Value == null ? : cell.Value.ToString().Count());
if (maxLength<)
{
workSheet.Column(columnIndex).AutoFit();
}
columnIndex++;
}
using (ExcelRange r=workSheet.Cells[startRowFrom,,startRowFrom,dataTable.Columns.Count])
{
r.Style.Font.Color.SetColor(System.Drawing.Color.White);
r.Style.Font.Bold = true;
r.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
r.Style.Fill.BackgroundColor.SetColor(System.Drawing.ColorTranslator.FromHtml("#1fb5ad"));
}
using (ExcelRange r=workSheet.Cells[startRowFrom+,,startRowFrom+dataTable.Rows.Count,dataTable.Columns.Count])
{
r.Style.Border.Top.Style = ExcelBorderStyle.Thin;
r.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
r.Style.Border.Left.Style = ExcelBorderStyle.Thin;
r.Style.Border.Right.Style = ExcelBorderStyle.Thin;
r.Style.Border.Top.Color.SetColor(System.Drawing.Color.Black);
r.Style.Border.Bottom.Color.SetColor(System.Drawing.Color.Black);
r.Style.Border.Left.Color.SetColor(System.Drawing.Color.Black);
r.Style.Border.Right.Color.SetColor(System.Drawing.Color.Black);
}
for (int i = ; i >= dataTable.Columns.Count - ; i++)
{
if (i == && showSrNo)
{
continue;
}
if (!columnsToTale.Contains(dataTable.Columns[i].ColumnName))
{
workSheet.DeleteRow(i + );
}
}
if (!String.IsNullOrEmpty(heading))
{
workSheet.Cells["A1"].Value = heading;
workSheet.Cells["A1"].Style.Font.Size = ;
workSheet.InsertColumn(, );
workSheet.InsertRow(, );
workSheet.Column().Width = ;
}
reslut = package.GetAsByteArray();
}
return reslut;
} /// <summary>
/// 导出Excel
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <param name="heading"></param>
/// <param name="isShowSlNo"></param>
/// <param name="ColumnsToTake"></param>
/// <returns></returns>
public static byte[] ExportExcel<T>(List<T> data, string heading = "", bool isShowSlNo = false, params string[] ColumnsToTake)
{
return ExportExcel(ListToDataTable<T>(data), heading, isShowSlNo, ColumnsToTake);
}
}
效果图:

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

Epplus:导出Excel的更多相关文章

  1. C# NPOI导出Excel和EPPlus导出Excel比较

    系统中经常会使用导出Excel的功能. 之前使用的是NPOI,但是导出数据行数多就报内存溢出. 最近看到EPPlus可以用来导出Excel,就自己测了下两者导出上的差异. NPIO官网地址:http: ...

  2. C# NPOI导出Excel和EPPlus导出Excel

    转自:http://www.cnblogs.com/tanpeng/p/6155749.html 系统中经常会使用导出Excel的功能.之前使用的是NPOI,但是导出数据行数多就报内存溢出. 最近看到 ...

  3. C# 使用Epplus导出Excel [5]:样式

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

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

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

  5. C# 使用Epplus导出Excel [3]:合并列连续相同数据

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

  6. C# 使用Epplus导出Excel [2]:导出动态列数据

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

  7. C# 使用Epplus导出Excel [1]:导出固定列数据

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

  8. asp.net下简单的Epplus导出excel

    引用的命名空间 using System.IO; using OfficeOpenXml; /// <summary> /// 导出excel /// </summary> / ...

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

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

  10. C# EPPlus 导出Excel

    一.Excel导出帮助类 /*引用NuGet包 EPPlus*/ /// <summary> /// Excel导出帮助类 /// </summary> public clas ...

随机推荐

  1. PHP常用的函数与小技巧

    密码加密与验证 password_hash - 创建密码的哈希(hash) string password_hash ( string $password , integer $algo [, arr ...

  2. linux 安装nginx 详解

    1 nginx安装环境 nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境. n gcc 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没 ...

  3. mysql数据库表卡死解决方法

    ---恢复内容开始--- 问题引起原因: 由于在执行大量插入操作的时候意外终止程序之后, MySQl的线程并没有被终止,导致表不能打开和操作 -  解决思路就是找到等待的线程并kill -- 查看所有 ...

  4. 我们是80后 golang入坑系统

    现在这个系列,已经开始两极分化了. 点赞的认为风格轻松,看着不困.反之,就有人嫌写的罗里吧嗦,上纲上线.所以善意提醒,里面不只是技术语言,还有段子.专心看技术的,千万别点!别怪我没提醒!差点忘说,版权 ...

  5. SaltStack 安装介绍 01

    一.入门指南 1.1 SALTSTACK是什么? The backbone of Salt is the remote execution engine, which creates a high-s ...

  6. sql server 2008 r2 登陆时显示无法打开默认的数据库

    解决! 第一步: 远程其他服务器的数据库能连上,本地的数据库某个用户名就是打不开,一开始以为是用户名或者密码错误, 后来用sqlcmd dos命令 -S . -U an -P sa 的方式登陆时可以的 ...

  7. PAT 1002. A+B for Polynomials (25) 简单模拟

    1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...

  8. Linux 配置Jenkins

    一.安装包下载: 1. jdk-8u152-linux-x64.tar.gz下载: wget http://download.oracle.com/otn-pub/java/jdk/8u152-b16 ...

  9. 【垃圾回收】Java内存回收实践经验 防止内存报警

    jdk6和7服务器端(-server) 默认的新生代的垃圾回收器为:PS Scavenge,老年代默认的垃圾回收器为:PS MarkSweep 目前项目使用了jdk7,tomcat7,经常出现内存堆使 ...

  10. Swift 线程安全数组

    欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 作者:BigNerdCoding 有并发的地方就存在线程安全问题,尤其是对于 Swift 这种还没有内置并发支持的语言来说线程安全问题更为突出 ...