本文实例讲述了asp.net中Table生成Excel表格的方法。分享给大家供大家参考。
具体实现方法如下:
复制代码 代码如下:
<!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">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="https://www.aidi.net.cn/Css/Balance.css" rel="stylesheet" />
<title></title>
<script runat="server">
protected void writeXls(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel.numberformat:@";
this.EnableViewState = false;
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");//设置输出流为简体中文
Response.AppendHeader("Content-Disposition", "attachment;filename=" + Title + ".xls");
Response.Write(printHid.Value);
Response.End();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:HiddenField ID="printHid" runat="server" />
<button onclick="getHtmltoValue()" >生成excel</button>
<script type="text/javascript">
function getHtmltoValue() {
document.getElementById("<%=printHid.ClientID%>").value = document.getElementById("printDiv").innerHTML;
}
</script>
<!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">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="https://www.aidi.net.cn/Css/Balance.css" rel="stylesheet" />
<title></title>
<script runat="server">
protected void writeXls(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel.numberformat:@";
this.EnableViewState = false;
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");//设置输出流为简体中文
Response.AppendHeader("Content-Disposition", "attachment;filename=" + Title + ".xls");
Response.Write(printHid.Value);
Response.End();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:HiddenField ID="printHid" runat="server" />
<button onclick="getHtmltoValue()" >生成excel</button>
<script type="text/javascript">
function getHtmltoValue() {
document.getElementById("<%=printHid.ClientID%>").value = document.getElementById("printDiv").innerHTML;
}
</script>
<table >
.......
</table>
</form>
</body>
<html>
希望本文所述对大家的asp.net程序设计有所帮助。
推荐阅读
VS2019离线安装方法图文教程
阅读 360 / 2020-03-02 23:45:59
ASP.NET Core 导入导出Excel xlsx 文件实例
阅读 332 / 2020-03-03 10:26:48
ASP.NET Core 过滤器中使用依赖注入知识点总结
阅读 267 / 2020-03-02 23:45:34
asp.net中Repeart选中整行操作实例
阅读 252 / 2020-03-06 00:30:27
asp.net发布后web.config中compilation的debug的值true和false区别点整理
阅读 241 / 2020-03-02 23:45:06
asp.net实现简单分页实例
阅读 225 / 2020-03-06 00:30:25
ASP.NET Core缓存静态资源示例详解
阅读 219 / 2020-03-06 00:30:31
使用SWFUpload实现无刷新上传图片
阅读 217 / 2020-03-06 03:30:14