Mvc 导出 Excel

之前接触过Webform,winfrom 的导出Excel方法 ,优点:省事。缺点:服务器必须安装Office

  这几天做项目 和 大牛学习了一下 新的方法,自己加以总结。希望更多的博友获益。不多说 。先上图,后上源码。

  很简单的MVC4 的页面 献丑了

  

效果图

  

  你猜了对了  我用的是 ClosedXml、NPOI  不是很新的东西!

  看看代码怎么实现吧 !

  1、工厂封装直接调用:

public class ExportFactory
{
public static byte[] exportToExcel(string type)
{
byte[] bytes = null;
switch (type.ToLower())
{
case "npoi":
bytes = NpoiExcelHelp.GenerateXlsxBytes(GetHtml());
break;
case "closexml":
bytes = ClosedXmlExcelHelp.GenerateXlsxBytes(GetHtml());
break;
default:
break;
}
return bytes;
}

static string GetHtml()
{
StringBuilder strHtml = new StringBuilder();
strHtml.Append("<table>");
strHtml.Append("<tr>");
strHtml.Append("<td rowspan='2'>First Row/First Colunm</td>");
strHtml.Append("<td>Second Row/First Colunm</td>");
strHtml.Append("</tr>");
strHtml.Append("<tr>");
strHtml.Append("<td>First Row/Second Colunm</td>");
strHtml.Append("</tr>");
strHtml.Append("<tr>");
strHtml.Append("<td>Second Row/Second Colunm</td>");
strHtml.Append("<td>Third Row/Second Colunm</td>");
strHtml.Append("</tr>");
strHtml.Append("</table>");
return strHtml.ToString();
}
}

 1   public class ExportFactory
2 {
3 public static byte[] exportToExcel(string type)
4 {
5 byte[] bytes = null;
6 switch (type.ToLower())
7 {
8 case "npoi":
9 bytes = NpoiExcelHelp.GenerateXlsxBytes(GetHtml());
10 break;
11 case "closexml":
12 bytes = ClosedXmlExcelHelp.GenerateXlsxBytes(GetHtml());
13 break;
14 default:
15 break;
16 }
17 return bytes;
18 }
19
20 static string GetHtml()
21 {
22 StringBuilder strHtml = new StringBuilder();
23 strHtml.Append("<table>");
24 strHtml.Append("<tr>");
25 strHtml.Append("<td rowspan='2'>First Row/First Colunm</td>");
26 strHtml.Append("<td>Second Row/First Colunm</td>");
27 strHtml.Append("</tr>");
28 strHtml.Append("<tr>");
29 strHtml.Append("<td>First Row/Second Colunm</td>");
30 strHtml.Append("</tr>");
31 strHtml.Append("<tr>");
32 strHtml.Append("<td>Second Row/Second Colunm</td>");
33 strHtml.Append("<td>Third Row/Second Colunm</td>");
34 strHtml.Append("</tr>");
35 strHtml.Append("</table>");
36 return strHtml.ToString();
37 }
38 }

  2、ClosedXmlExportHelp

public class ClosedXmlExcelHelp
{
public static byte[] GenerateXlsxBytes(string tableHtml)
{
string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + tableHtml;

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNode table = doc.SelectSingleNode("/table");

int colspan = 1;
int rowspan = 1;

var workBook = new XLWorkbook();
var ws = workBook.Worksheets.Add("Export");

int rowNum;
int columnNum;

rowNum = 1;
columnNum = 1;

string mapKey = string.Empty;
string mergKey = string.Empty;

int rowCount = table.ChildNodes.Count;
int colCount = 0;

foreach (XmlNode row in table.ChildNodes)
{
if (colCount < row.ChildNodes.Count)
{
colCount = row.ChildNodes.Count;
}
}

bool[,] map = new bool[rowCount + 1, colCount + 1];

foreach (XmlNode row in table.ChildNodes)
{
columnNum = 1;
foreach (XmlNode column in row.ChildNodes)
{
if (column.Attributes["rowspan"] != null)
{
rowspan = Convert.ToInt32(column.Attributes["rowspan"].Value);
}
else
{
rowspan = 1;
}

if (column.Attributes["colspan"] != null)
{
colspan = Convert.ToInt32(column.Attributes["colspan"].Value);
}
else
{
colspan = 1;
}

while (map[rowNum, columnNum])
{
columnNum++;
}

if (rowspan == 1 && colspan == 1)
{
ws.Cell(string.Format("{0}{1}", Char.Chr(columnNum), rowNum)).Value = column.InnerText;
map[rowNum, columnNum] = true;
}
else
{
ws.Cell(string.Format("{0}{1}", Char.Chr(columnNum), rowNum)).Value = column.InnerText;
mergKey =
string.Format("{0}{1}:{2}{3}",
Char.Chr(columnNum), rowNum, Char.Chr(columnNum + colspan - 1), rowNum + rowspan - 1);
ws.Range(mergKey).Merge();

for (int m = 0; m < rowspan; m++)
{
for (int n = 0; n < colspan; n++)
{
map[rowNum + m, columnNum + n] = true;
}
}
}
columnNum++;
}
rowNum++;
}

MemoryStream stream = new MemoryStream();
workBook.SaveAs(stream);

return stream.ToArray();

}
}

  1   public class ClosedXmlExcelHelp
2 {
3 public static byte[] GenerateXlsxBytes(string tableHtml)
4 {
5 string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + tableHtml;
6
7 XmlDocument doc = new XmlDocument();
8 doc.LoadXml(xml);
9
10 XmlNode table = doc.SelectSingleNode("/table");
11
12 int colspan = 1;
13 int rowspan = 1;
14
15 var workBook = new XLWorkbook();
16 var ws = workBook.Worksheets.Add("Export");
17
18 int rowNum;
19 int columnNum;
20
21 rowNum = 1;
22 columnNum = 1;
23
24 string mapKey = string.Empty;
25 string mergKey = string.Empty;
26
27 int rowCount = table.ChildNodes.Count;
28 int colCount = 0;
29
30 foreach (XmlNode row in table.ChildNodes)
31 {
32 if (colCount < row.ChildNodes.Count)
33 {
34 colCount = row.ChildNodes.Count;
35 }
36 }
37
38 bool[,] map = new bool[rowCount + 1, colCount + 1];
39
40 foreach (XmlNode row in table.ChildNodes)
41 {
42 columnNum = 1;
43 foreach (XmlNode column in row.ChildNodes)
44 {
45 if (column.Attributes["rowspan"] != null)
46 {
47 rowspan = Convert.ToInt32(column.Attributes["rowspan"].Value);
48 }
49 else
50 {
51 rowspan = 1;
52 }
53
54 if (column.Attributes["colspan"] != null)
55 {
56 colspan = Convert.ToInt32(column.Attributes["colspan"].Value);
57 }
58 else
59 {
60 colspan = 1;
61 }
62
63 while (map[rowNum, columnNum])
64 {
65 columnNum++;
66 }
67
68 if (rowspan == 1 && colspan == 1)
69 {
70 ws.Cell(string.Format("{0}{1}", Char.Chr(columnNum), rowNum)).Value = column.InnerText;
71 map[rowNum, columnNum] = true;
72 }
73 else
74 {
75 ws.Cell(string.Format("{0}{1}", Char.Chr(columnNum), rowNum)).Value = column.InnerText;
76 mergKey =
77 string.Format("{0}{1}:{2}{3}",
78 Char.Chr(columnNum), rowNum, Char.Chr(columnNum + colspan - 1), rowNum + rowspan - 1);
79 ws.Range(mergKey).Merge();
80
81 for (int m = 0; m < rowspan; m++)
82 {
83 for (int n = 0; n < colspan; n++)
84 {
85 map[rowNum + m, columnNum + n] = true;
86 }
87 }
88 }
89 columnNum++;
90 }
91 rowNum++;
92 }
93
94 MemoryStream stream = new MemoryStream();
95 workBook.SaveAs(stream);
96
97 return stream.ToArray();
98
99 }
100 }

  3、NPOIExportHelp

public class NpoiExcelHelp
{
public static byte[] GenerateXlsxBytes(string tableHtml)
{
string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + tableHtml;

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNode table = doc.SelectSingleNode("/table");

int colspan = 1;
int rowspan = 1;

int rowNum;
int columnNum;
rowNum = 1;
columnNum = 1;

var workBook = new HSSFWorkbook();
var ws = workBook.CreateSheet("Export");

string mapKey = string.Empty;
string mergKey = string.Empty;

int rowCount = table.ChildNodes.Count;
int colCount = FetchColCount(table.ChildNodes);

InitSheet(ws, rowCount, colCount);

bool[,] map = new bool[rowCount + 1, colCount + 1];

foreach (XmlNode row in table.ChildNodes)
{
columnNum = 1;
foreach (XmlNode column in row.ChildNodes)
{
if (column.Attributes["rowspan"] != null)
{
rowspan = Convert.ToInt32(column.Attributes["rowspan"].Value);
}
else
{
rowspan = 1;
}

if (column.Attributes["colspan"] != null)
{
colspan = Convert.ToInt32(column.Attributes["colspan"].Value);
}
else
{
colspan = 1;
}

while (map[rowNum, columnNum])
{
columnNum++;
}

if (rowspan == 1 && colspan == 1)
{
SetCellValue(ws, string.Format("{0}{1}", Char.Chr(columnNum), rowNum), column.InnerText);
map[rowNum, columnNum] = true;
}
else
{
SetCellValue(ws, string.Format("{0}{1}", Char.Chr(columnNum), rowNum), column.InnerText);
mergKey =
string.Format("{0}{1}:{2}{3}",
Char.Chr(columnNum), rowNum, Char.Chr(columnNum + colspan - 1), rowNum + rowspan - 1);
MergCells(ws, mergKey);

for (int m = 0; m < rowspan; m++)
{
for (int n = 0; n < colspan; n++)
{
map[rowNum + m, columnNum + n] = true;
}
}
}
columnNum++;
}
rowNum++;
}

MemoryStream stream = new MemoryStream();
workBook.Write(stream);

return stream.ToArray();

}

static int FetchColCount(XmlNodeList nodes)
{
int colCount = 0;

foreach (XmlNode row in nodes)
{
if (colCount < row.ChildNodes.Count)
{
colCount = row.ChildNodes.Count;
}
}

return colCount;
}

static void InitSheet(ISheet sheet, int rowCount, int colCount)
{
for (int i = 0; i < rowCount; i++)
{
IRow row = sheet.CreateRow(i);
for (int j = 0; j < colCount; j++)
{
row.CreateCell(j);
}
}
}

static void SetCellValue(ISheet sheet, string cellReferenceText, string value)
{
CellReference cr = new CellReference(cellReferenceText);
IRow row = sheet.GetRow(cr.Row);
ICell cell = row.GetCell(cr.Col);
cell.SetCellValue(value);
}

static void MergCells(ISheet sheet, string mergeKey)
{
string[] cellReferences = mergeKey.Split(':');

CellReference first = new CellReference(cellReferences[0]);
CellReference last = new CellReference(cellReferences[1]);

CellRangeAddress region = new CellRangeAddress(first.Row, last.Row, first.Col, last.Col);
sheet.AddMergedRegion(region);
}
}

  1   public class NpoiExcelHelp
2 {
3 public static byte[] GenerateXlsxBytes(string tableHtml)
4 {
5 string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + tableHtml;
6
7 XmlDocument doc = new XmlDocument();
8 doc.LoadXml(xml);
9
10 XmlNode table = doc.SelectSingleNode("/table");
11
12 int colspan = 1;
13 int rowspan = 1;
14
15 int rowNum;
16 int columnNum;
17 rowNum = 1;
18 columnNum = 1;
19
20 var workBook = new HSSFWorkbook();
21 var ws = workBook.CreateSheet("Export");
22
23 string mapKey = string.Empty;
24 string mergKey = string.Empty;
25
26 int rowCount = table.ChildNodes.Count;
27 int colCount = FetchColCount(table.ChildNodes);
28
29 InitSheet(ws, rowCount, colCount);
30
31 bool[,] map = new bool[rowCount + 1, colCount + 1];
32
33 foreach (XmlNode row in table.ChildNodes)
34 {
35 columnNum = 1;
36 foreach (XmlNode column in row.ChildNodes)
37 {
38 if (column.Attributes["rowspan"] != null)
39 {
40 rowspan = Convert.ToInt32(column.Attributes["rowspan"].Value);
41 }
42 else
43 {
44 rowspan = 1;
45 }
46
47 if (column.Attributes["colspan"] != null)
48 {
49 colspan = Convert.ToInt32(column.Attributes["colspan"].Value);
50 }
51 else
52 {
53 colspan = 1;
54 }
55
56 while (map[rowNum, columnNum])
57 {
58 columnNum++;
59 }
60
61 if (rowspan == 1 && colspan == 1)
62 {
63 SetCellValue(ws, string.Format("{0}{1}", Char.Chr(columnNum), rowNum), column.InnerText);
64 map[rowNum, columnNum] = true;
65 }
66 else
67 {
68 SetCellValue(ws, string.Format("{0}{1}", Char.Chr(columnNum), rowNum), column.InnerText);
69 mergKey =
70 string.Format("{0}{1}:{2}{3}",
71 Char.Chr(columnNum), rowNum, Char.Chr(columnNum + colspan - 1), rowNum + rowspan - 1);
72 MergCells(ws, mergKey);
73
74 for (int m = 0; m < rowspan; m++)
75 {
76 for (int n = 0; n < colspan; n++)
77 {
78 map[rowNum + m, columnNum + n] = true;
79 }
80 }
81 }
82 columnNum++;
83 }
84 rowNum++;
85 }
86
87 MemoryStream stream = new MemoryStream();
88 workBook.Write(stream);
89
90 return stream.ToArray();
91
92 }
93
94 static int FetchColCount(XmlNodeList nodes)
95 {
96 int colCount = 0;
97
98 foreach (XmlNode row in nodes)
99 {
100 if (colCount < row.ChildNodes.Count)
101 {
102 colCount = row.ChildNodes.Count;
103 }
104 }
105
106 return colCount;
107 }
108
109 static void InitSheet(ISheet sheet, int rowCount, int colCount)
110 {
111 for (int i = 0; i < rowCount; i++)
112 {
113 IRow row = sheet.CreateRow(i);
114 for (int j = 0; j < colCount; j++)
115 {
116 row.CreateCell(j);
117 }
118 }
119 }
120
121 static void SetCellValue(ISheet sheet, string cellReferenceText, string value)
122 {
123 CellReference cr = new CellReference(cellReferenceText);
124 IRow row = sheet.GetRow(cr.Row);
125 ICell cell = row.GetCell(cr.Col);
126 cell.SetCellValue(value);
127 }
128
129 static void MergCells(ISheet sheet, string mergeKey)
130 {
131 string[] cellReferences = mergeKey.Split(':');
132
133 CellReference first = new CellReference(cellReferences[0]);
134 CellReference last = new CellReference(cellReferences[1]);
135
136 CellRangeAddress region = new CellRangeAddress(first.Row, last.Row, first.Col, last.Col);
137 sheet.AddMergedRegion(region);
138 }
139 }

  4、Ascii 转化

public class Char
{
public static string Chr(int i)
{
char c = (char)(64 + i);
return c.ToString();
}
}

1  public class Char
2 {
3 public static string Chr(int i)
4 {
5 char c = (char)(64 + i);
6 return c.ToString();
7 }
8 }

  以上代码就是实现Export Excel的全部代码

   思路:拼接字符串构造一个纯Html的结构。用rowspan colspan来跨行跨列,把Html当做参数直接传过去调用写好的导出方法

   返回数组。保存 完成!很简单!

   希望能帮助大家!我的可能不是最好的方法!但是我在尽力去想!希望广大的博友一起想!想出更好的方法解决中国的所有技术人员的困惑!如有想法请留下您的宝贵评论!

Check Me Give You Source!

作者:Elite.net 
出处:http://www.cnblogs.com/yhyjy/ 
新浪微博:http://weibo.com/u/2709913775

Mvc 导出 Excel的更多相关文章

  1. mvc导出excel 之 新

    前段时间做的mvc导出excel 老大说要进行优化,我原来导出是用npoi插件进行导出,格式是将数据放入到datatable中,然后进行导出. 说要优化的时候就想着将datatable数据导出格式改为 ...

  2. ASP.NET MVC导出excel

    ASP.NET MVC导出excel 要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式 ...

  3. MVC 导出Excel 的其中一方法(View导出excel)

    场景:mvc下导出excel 思路:使用View导出excel 步骤: 1.导出标签添加事件 $("#export_A").click(function(){ //省略代码.... ...

  4. ASP.NET MVC导出excel(数据量大,非常耗时的,异步导出)

    要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式,后台开辟一个线程将excel导出到指 ...

  5. MVC导出Excel,提供下载Excel

    类1: using System.Collections.Generic;using System.Data;using System.Web.Mvc;using System.IO;using Sy ...

  6. MVC学习笔记---MVC导出excel(数据量大,非常耗时的,异步导出)

    要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式,后台开辟一个线程将excel导出到指 ...

  7. spring mvc 导出 excel

    // js 触发导出 excel 方法 导出当前页的数据 含有条件查询的结果 // js 框架使用的 是 easyui function doExport(){ var optins = $(&quo ...

  8. mvc导出EXCEL

    /// <summary> /// 导出EXCEL /// </summary> /// <returns></returns> public Acti ...

  9. 三层+MVC导出Excel(2)

    背景: 出门在外,一切以健康为主,学习为辅,健康搞好了,学习也不能拉下,在外工作期间,我们在做数据导出的时候,自己封了一个类,利用NPOI进行数据导出Excel,自我感觉良好,特给大家分享一下,希望对 ...

随机推荐

  1. ASP.NET 异步编程

    ASP.NET 异步编程 相关博文: 异步编程 In .NET(回味无穷!!!) ASP.NET sync over async(异步中同步,什么鬼?) 本来这篇博文想探讨下异步中的异常操作,但自己在 ...

  2. ffmpeg和opencv 播放视频文件和显示器

    ffmpeg它是基于最新版本,在官网下载http://ffmpeg.zeranoe.com/builds/.编译时VS2010配置相关头文件及库的路径就可以.opencv的搭建參考上一个博客. 首先简 ...

  3. hadoop工作平台梳理

    文章 http://blog.csdn.net/lili72/article/details/41130743 lili72 数据平台: 一.  hadoop平台:Hbase.hive,storm,s ...

  4. c# 硬件开源神器netduino的开发中慎用Cpu.Pin

    最近为了测试netduino开发板的各个端口是否正常使用,让同事写了一些测试程序,结果出了问题,他的测试程序导致开发板无法发布程序进去,按他的结论是开发板有问题,针对这个情况,我们经过仔细分析代码,认 ...

  5. nefu117 素数个数的位数,素数定理

    素数个数的位数 Time Limit 1000ms Memory Limit 65536K description 小明是一个聪明的孩子,对数论有着非常浓烈的兴趣.他发现求1到正整数10n 之间有多少 ...

  6. WordPress添加评论回复的电子邮件警报通知

    评论回复帖子,主动发送电子邮件通知评论员,这是提高的一大举措的用户体验.倡导孟一直在使用Willin Kan主评论回复电子邮件警报通知码,我相信很多人也使用,假设你没有使用.最好的尝试. 根据自己的需 ...

  7. MVC 快速开发框架

    ASP.NET MVC 快速开发框架之 SqlSugar+SyntacticSugar+JQWidgetsSugar+jqwidgets jqwidgets.js: 是一个功能完整的框架,它具有专业的 ...

  8. osx launchpad删除图标

    安装了个parallels desktop之后,OSX中的launchpad中的图标多了不少,但是好多都不是我自己想要的,我们该怎么删除或者改动呢,以下介绍一些方法: ①直接操作Appications ...

  9. Javascript学习4 - 对象和数组

    原文:Javascript学习4 - 对象和数组 在Javascript中,对象和数组是两种基本的数据类型,而且它们也是最重要的两种数据类型. 对象是已命名的值的一个集合,而数组是一种特殊对象,它就像 ...

  10. 软体project(四)——一生

    软件生存周期是软件project中的一个重要概念,把整个生存周期划分为若干个阶段,是实现软件生产project化的重要步骤. 软件的生存周期一般划分为软件计划.软件开发和软件执行三个时期,例如以下图: ...