使用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的路由方案,与原来的方案在使用上差别不 ...
随机推荐
- Cocos2d-x Box2D物理引擎编译设置
找到解决方案TestCpp工程.打开其属性=>C/C++ =>预处理器==>处理器定义. 参加"CC_ENABLE_BOX2D_INTEGRATION=1", 更 ...
- Python爬虫(一)
花了四天的时间用python写了个简单的爬虫程序.整个过程分为两个部分:工具的安装和程序的实现 本文并没有讲程序的详细实现遇到的问题,而是对着手前一些前期的准备 第一部分(工具的安装) 开发工具的下载 ...
- 【Web探索之旅】第三部分第二课:IP地址和域名
内容简介 1.第三部分第二课:IP地址和域名 2.第三部分第三课预告:协议 第三部分第二课:IP地址和域名 上一课我们说了在Web之中,全球各地有无数台机器,有些充当客户机,有些作为服务器. 那么这些 ...
- $.ajax通路RESTful Web Service一个错误:Unsupported Media Type
最近项目,使用头版jquery ajax访问背景CXF发布时间rest维修,结果遇到了错误"Unsupported Media Type". 公布的服务java代码例如以下: im ...
- Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW
一.Telnet 采用Telnet用来訪问远程计算机的TCP/IP协议以控制你的网络设备,相当于在离开某个建筑时大喊你的username和口令.非常快会有人进行监听, 并且他们会利用你安全意识的缺乏. ...
- [LeetCode116]Path Sum
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- TI C66x DSP 系统events及其应用 - 5.8(ISTP)
中断服务表指针ISTP(Interrupt Service Table Pointer)位置寄存器用于定位的中断服务例程,那ISTP去哪里找要运行的程序,ISTP(当中的ISTB字段)就是指向IST表 ...
- 使用Bootstrap 他写道网站
新发现vdceye有些难看的网站,为了找到一个bootstrap工具,我一次又一次地写信给网站 这个工具是很容易使用 http://vdceye.com/ 版权声明:本文博客原创文章,博客,未经同意, ...
- 第三章——使用系统函数、存储过程和DBCC SQLPERF命令来监控SQLServer(3)
原文:第三章--使用系统函数.存储过程和DBCC SQLPERF命令来监控SQLServer(3) 本文为这个系列最后一篇.将是如何使用DBCC命令来监控SQLServer日志空间的使用情况. 前言: ...
- AndroidSlidingUpPanel
使用控制和简单的分析方法
滑 - 向上的时间可以飞起来控件的显示区域.分类似至play music有效. 该控件在主界面中有一个例如以下图红色箭头所指的底部触发区域: 该区域点击的时候被隐藏在下方的内容将网上漂移到顶部,直到被 ...