asp.net Table方式生成Excel表格的例子

发布时间:2019-09-05编辑:脚本学堂
如何在asp.net中Table生成Excel表格,将html的table表格生成excel,例子很简单,供初学者参考。

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>asp.net Table方式生成Excel表格 - www.osxue.com</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>