使用C#或javascript将Table里的数据导出到Excel
原文:使用C#或javascript将Table里的数据导出到Excel

本方法已经将导出excel做成分部视图,引用时只需在视图中使用如下代码即可:
@{
Html.RenderAction("Index", "ExportExcel", new { divId = "report", filename = "hidTitle" });
}
其中divId为table外层紧连的div的Id,filename为导出的excel名字。本模块使用MVC,以下为Controller部分,其中Index对应所述分布视图。
public class ExportExcelController : Controller
{
[HttpPost]
[ValidateInput(false)]
public ActionResult DownloadReport(FormCollection form)
{
string excelContent = form["hidTable"];
string filename = form["hidFileName"];
ExportToExcel("application/ms-excel", filename + ".xls", excelContent);
return View();
}
public void ExportToExcel(string FileType, string FileName, string ExcelContent)
{
System.Web.HttpContext.Current.Response.Charset = "UTF-8";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
System.Web.HttpContext.Current.Response.ContentType = FileType;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.HttpContext.Current.Response.Output.Write(ExcelContent.ToString());
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
public ActionResult Index(string divId, string filename)
{
ViewBag.HidDivId = divId;
ViewBag.FileName = filename;
return PartialView();
}
}
分布视图代码:
@{
Layout = null;
}
<script type="text/javascript" src="~/Scripts/jquery-1.10.2.js"></script>
<form action="/ExportExcel/DownloadReport" method="post" style="display:inline">
<input type="hidden" id="hidTable" name="hidTable" value="" />
<input type="hidden" id="hidFileName" name="hidFileName" value="" />
<input type="submit" name="btn" id="btnclick" class="btn blue" value="导出Excel文件" />
</form>
<script>
$(function () {
$("#btnclick").click(function () {
$("#hidTable").val($("#@ViewBag.HidDivId").html());
$("#hidFileName").val($("#@ViewBag.FileName").val());
});
});
</script>
js文件地址:链接:http://pan.baidu.com/s/1jGwynWy 密码:kay1
使用方法:
<a download="@(ViewBag.ExcelName).xls" href="#" onclick="return ExcellentExport.excel(this, 'excelTable', 'Sheet1');">导出Excel文件</a>
其中excelTable对应table的Id值。
@{
Layout = null;
ViewBag.Title = "ExcelDemo";
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>@ViewBag.Title</title>
<style>
* {
margin: ;
padding: ;
}
.Data {
width: %;
height: 50px;
background-color: #00ffff;
text-align: center;
font-size: 20px;
color: red;
line-height: 50px;
font-weight: bold;
}
#report {
width: %;
margin:10px %;
}
.tablereport1 tr:nth-child(2n+) {
background: #e4e4e4;
}
.tablereport1 td {
border: none !important;
padding: 0px 10px;
height: 40px;
text-align: center;
}
.Down {
margin: 10px 5px;
line-height: 30px;
}
.Down a {
border: 1px solid #a4a4a4;
background-color: #e5e5e5;
color: #;
font-size: 14px;
text-decoration: none;
border-radius: 2px;
}
</style>
<script src="~/Scripts/Excel/excellentexport.js"></script>
</head>
<body>
<div class="Data">
数据源
</div>
<input type="hidden" name="hidTitle" id="hidTitle" value="@ViewBag.ExcelName" />
<div id="report">
<table border="" class="tablereport1" cellpadding="" cellspacing="" id="excelTable">
<tr>
<td colspan="" style="text-align:center;font-size:20px">
测试数据表
</td>
</tr>
@for (int i = ; i < ; i++)
{
<tr>
@for (int j = ; j < ; j++)
{
<td>行@(i)列@(j)</td>
}
</tr>
}
</table>
</div>
<div class="Data">
数据导出
</div>
<div class="Down">
用C#将Table里的数据导出到Excel:<br />
@{
Html.RenderAction("Index", "ExportExcel", new { divId = "report", filename = "hidTitle" });
}
</div>
<div class="Down">
用js将Table里的数据导出到Excel:<br />
<a download="@(ViewBag.ExcelName).xls" href="#" onclick="return ExcellentExport.excel(this, 'excelTable', 'Sheet1');">导出Excel文件</a>
</div>
</body>
</html>
使用C#或javascript将Table里的数据导出到Excel的更多相关文章
- html5中 table数据导出到excel文件
JS代码: /** * table数据导出到excel * 形参 table : tableId ; * sheetName : 工作薄名 * fileName : 文件名 * linkId :隐藏的 ...
- 使用JavaScript把页面上的表格导出为Excel文件
如果在页面上展示了一个数据表格,而用户想把这个表格导出为Excel文件,那么在要求不高的情况下,可以不通过服务器生成表格,而是直接利用JavaScript的Blob和Object URL特性将表格导出 ...
- jquery.table2excel,将HTML的table标签数据导出成excel
<!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content=&quo ...
- 【JavaScript】table里面点击某td获取同一行tr的其他td值
某td的input(保存按钮)上绑定方法,点击按钮保存该行所有数据 function locationedit(num){ var ordernumber = $("#"+num) ...
- 在把table表格中的数据导出到Excel的时候,以科学计数法显示位数多的数字时怎么解决?
sbHtml.AppendFormat("<td> {0}</td>", data[i].IDcard.ToString()); sbHtml.Append ...
- flex+java将数据库里的数据导出到指定目录下excel表里(poi)
数据写入到excel中采用的是Apache POI: //java后台的一个工具类(该工具类适用于为不同字段添加,方便) /* 下面这个方法是将list转换为Excel工作表的 */ public s ...
- 表格(table)数据导出成Excel
使用xlxs-js库 function exportExcel () { var wb = XLSX.utils.table_to_book(document.querySelector('.my-e ...
- JavaScript怎么把对象里的数据整合进另外一个数组里
https://blog.csdn.net/qq_26222859/article/details/70331833 var json1 = [ {"guoshui":[ 3000 ...
- ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 try.dot.net 的正确使用姿势 .Net NPOI 根据excel模板导出excel、直接生成excel .Net NPOI 上传excel文件、提交后台获取excel里的数据
ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不 ...
随机推荐
- [转载] 树莓派读取温湿度传感器DHT11
原文地址: http://blog.csdn.net/liang890319/article/details/8739683 硬件: 树莓派 2.0 DHT模块 接树莓派5V GND GPIO1 功 ...
- 图解Http协议 (转)
一.技术基石及概述 问:什么是HTTP? 答:HTTP是一个客户端和服务器端请求和响应的标准TCP.其实建立在TCP之上的. 当我们打开百度网页时,是这样的: https://www.baidu.co ...
- Android:简单的弹幕效果达到
首先,效果图.分类似至360检测到的骚扰电话页面: 布局非常easy,上面是一个RelativeLayout,以下一个Button. 功能: (1)弹幕生成后自己主动从右側往左側滚动(Translat ...
- PIC16SCM设置不同IO功耗端口状态的影响
最近做的PIC低功耗微控制器,因此,要设置不同的IO端口状态有关电源的情况测试,在系列万用表的方法来测量电流,供应链管理IO港是在地狱,无头整个系统驱动器.的是PIC16F690单片机. 思路例如以下 ...
- 进程和线程之间的关系和区别 和 CPU牒
流程具有一定独立功能的程序关于某个数据集合上的一次执行活动,进程是系统进行资源分配和调度的一个独立单位. 线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立执行的基本单位. 进 ...
- 调用CachedRowSetImpl类时,为什么会出现这样的错误
Access restriction: The type CachedRowSetImpl is not accessible due to restriction on required libra ...
- Cntlm安装和配置体验
对于那些谁使用NTLM验证网络代理环境(即除了需要设置的代理主机和端口还需要提供一个域用户名和密码)供.通过代理上网头疼.这主要是由于非常大的软件不支持NTLM的代理(比方眼下的GIT就不能支持NTL ...
- rest服务器
一个轻量级rest服务器 RestServer直接发布数据库为json格式提供方法 RestSerRestServer直接发布数据库为json格式 支持MySQL,SqlServer,Oracle ...
- hdu4496 D-City(扭转和支票托收啊 )
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496 D-City Problem Description Luxer is a really bad ...
- ssl通关的概念(一个)
在公司最近的项目涉及多种加密.安全.我一直在这方面缺乏经验.很协议仅仅知道是什么概念.用于传输的加密SSL,也煞费苦心.非常easy一件事,折腾了很长一段时间.IT该行啊,真的是.难者不会,与会者困难 ...