asp.net中Table生成Excel表格的方法
程序员文章站
2024-02-22 11:00:46
本文实例讲述了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="/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>
本文实例讲述了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="/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程序设计有所帮助。