mvc4 怎么样导出excel表格
展开全部
大概有两种做法:
1.把画面上的Html字串输出到档案供Client端下载(※开发快但缺点为 使用者开启该Excel档案会跳出警告视窗)
2.刻苦刻难,用ADO.net搭配NPOI套件,建立Excel物件…用ADO.net塞资料…Response给Client端下载(※开发慢,但没有上述缺点)
本文介绍的是第1种方法
※并不是在Controller里一口气把Html字串都组好再输出档案,虽然这做法也行…
实作概念
1.使用者按下汇出按钮
2.用jQuery把Html Table字串存入hidden栏位,表单post到Action
3.Action回传File即可
Sample Code:
View的Index.cshtml
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-2.0.0.min.js")"></script>
<script type="text/javascript">
function exportExcel() {
var sHtml = htmlEncode($("#MyTable")[0].outerHTML);//做html编码
$("input[name='hHtml']").val(sHtml);
//表单提交
$("form[name='myForm']").submit();
}
//↓出自:http://stackoverflow.com/questions/1219860/javascript-jquery-html-encoding
function htmlEncode(value) {
//create a in-memory div, set it's inner text(which jQuery automatically encodes)
//then grab the encoded contents back out. The div never exists on the page.
return $('<div/>').text(value).html();
}
</script>
</head>
<body>
<table width="100%" border="1" cellpadding="0" cellspacing="0" id="MyTable">
<thead>
<tr>
<th>编号</th>
<th>名称</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>测试1</td>
</tr>
<tr>
<td>2</td>
<td>测试2</td>
</tr>
<tr>
<td>3</td>
<td>测试3</td>
</tr>
</tbody>
</table>
<br />
<input type="button" value="导出" onclick="exportExcel();" />
</body>
</html>
@using (Html.BeginForm("ExportExcel", "Home", FormMethod.Post, new { name="myForm"}))
{
@Html.Hidden("hHtml")
}
HomeController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplicationExportHtmlTableExcel.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult ExportExcel(FormCollection form)
{
string strHtml = form["hHtml"];
strHtml = HttpUtility.HtmlDecode(strHtml);//Html解码
byte[] b = System.Text.Encoding.Default.GetBytes(strHtml);//字串转byte阵列
return File(b, "application/vnd.ms-excel", "这是Excel.xls");//输出档案给Client端
}
}
}
执行结果,按下导出
1.把画面上的Html字串输出到档案供Client端下载(※开发快但缺点为 使用者开启该Excel档案会跳出警告视窗)
2.刻苦刻难,用ADO.net搭配NPOI套件,建立Excel物件…用ADO.net塞资料…Response给Client端下载(※开发慢,但没有上述缺点)
本文介绍的是第1种方法
※并不是在Controller里一口气把Html字串都组好再输出档案,虽然这做法也行…
实作概念
1.使用者按下汇出按钮
2.用jQuery把Html Table字串存入hidden栏位,表单post到Action
3.Action回传File即可
Sample Code:
View的Index.cshtml
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-2.0.0.min.js")"></script>
<script type="text/javascript">
function exportExcel() {
var sHtml = htmlEncode($("#MyTable")[0].outerHTML);//做html编码
$("input[name='hHtml']").val(sHtml);
//表单提交
$("form[name='myForm']").submit();
}
//↓出自:http://stackoverflow.com/questions/1219860/javascript-jquery-html-encoding
function htmlEncode(value) {
//create a in-memory div, set it's inner text(which jQuery automatically encodes)
//then grab the encoded contents back out. The div never exists on the page.
return $('<div/>').text(value).html();
}
</script>
</head>
<body>
<table width="100%" border="1" cellpadding="0" cellspacing="0" id="MyTable">
<thead>
<tr>
<th>编号</th>
<th>名称</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>测试1</td>
</tr>
<tr>
<td>2</td>
<td>测试2</td>
</tr>
<tr>
<td>3</td>
<td>测试3</td>
</tr>
</tbody>
</table>
<br />
<input type="button" value="导出" onclick="exportExcel();" />
</body>
</html>
@using (Html.BeginForm("ExportExcel", "Home", FormMethod.Post, new { name="myForm"}))
{
@Html.Hidden("hHtml")
}
HomeController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplicationExportHtmlTableExcel.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult ExportExcel(FormCollection form)
{
string strHtml = form["hHtml"];
strHtml = HttpUtility.HtmlDecode(strHtml);//Html解码
byte[] b = System.Text.Encoding.Default.GetBytes(strHtml);//字串转byte阵列
return File(b, "application/vnd.ms-excel", "这是Excel.xls");//输出档案给Client端
}
}
}
执行结果,按下导出
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询