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. inner join on, left join on, right join on

    1.定义: inner join(等值连接) : 仅仅返回两个表中联结字段相等的记录 left join(左联接) :返回包含左表中的全部记录和右表中联结字段相等的记录 right join(右联接) ...

  2. Eclipse SDK构建J2EE开发环境

    鄙视官Java EE Developers 体积庞大的兄弟们可以提出自己的J2EE开发环境! 1.第一次去Eclipse官网下载Eclipse IDE 我使用的是:Eclipse IDE for Ja ...

  3. 实现BUG自动检测 - ASP.NET Core依赖注入

    我个人比较懒,能自动做的事绝不手动做,最近在用ASP.NET Core写一个项目,过程中会积累一些方便的工具类或框架,分享出来欢迎大家点评. 如果以后有时间的话,我打算写一个系列的[实现BUG自动检测 ...

  4. flipsnap.js 源码阅读备份

    这是官网:http://hokaccha.github.io/js-flipsnap/ 1.引入全局命名空间 类似jQuery插件写法   传入window, document,提高内部访问速度: ; ...

  5. 他们控制的定义-DragButton

    一个.叙述性说明 可拖动Button 两.无图无真相 这是用在实际工程效果图.和demo不太一样. 三.源代码 https://github.com/mentor811/Demo_DragButton ...

  6. (DDD)仓储的思考

    (DDD)仓储的思考 为什么需要仓储呢?领域对象(一般是聚合根)的被创建出来后的到最后持久化到数据库都需要跟数据库打交道,这样我们就需要一个类似数据库访问层的东西来管理领域对象.那是不是我们就可以设计 ...

  7. &quot;伪中国移动client&quot;--伪基站诈骗

    一.简单介绍: 近日,百度安全实验室发现一款"伪中国移动client"病毒.犯罪分子通过伪基站方式大量发送伪10086的短信,诱导用户点击钓鱼链接:并在钓鱼页面诱导用户输入网银账号 ...

  8. jsoup分解HTML DOM

    采用jsoup 分解HTML .使用和JS操作DOM分类似. 示例代码: import java.io.IOException; import org.jsoup.Jsoup; import org. ...

  9. java中System类简介(转)

    上次面试中遇到的一个问题,问到System.out.println()中的out是不是内部类[不是内部类],当时就给问蒙了,直观感觉out应该是System类的一个属性,跟内部类有什么关系?而且之前整 ...

  10. 单节点伪分布式Hadoop配置

    本文所用软件版本: VMware-workstation-full-11.1.0 jdk-6u45-linux-i586.bin ubuntukylin-14.04-desktop-i386.iso ...