首页 > 代码库 > ASP.net中导出Excel的简单方法介绍
ASP.net中导出Excel的简单方法介绍
下面介绍一种ASP.net中导出Excel的简单方法
先上代码:前台代码如下(这是自己项目里面写的一点代码先贴出来吧)
<div id="export" runat="server" style="width: 700px; margin-left: auto; margin-right: auto;">
<!--startprint-->
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" style="word-break: break-all;">
<tr>
<td align="center" colspan="7" valign="middle" style="font-size: 14px; font-family: 宋体; font-weight: bold;
height: 30px">
<span id="spanYEAR" runat="server"></span>年<span id="spanMONTH" runat="server"></span>月分公司月度需求生产任务汇总
</td>
</tr>
<tr>
<td align="right" valign="middle" colspan="7">
<table width="100%">
<tr>
<td colspan="2" align="left">
数量单位:支
</td>
<%-- <td width="10%" align="center" style="font-weight: bold;">
</td>--%>
<td width="20%" align="center" style="font-weight: bold;">
</td>
<td width="25%" colspan="2" align="center">
</td>
<td width="40%" colspan="2" align="center">
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" style="font-size: 12px;
font-family: 宋体; word-break: break-all;">
<tr>
<td colspan="4" align="left" valign="top">
<div id="div" runat="server">
<table width="100%" border="1" align="center" cellpadding="0" cellspacing="0" style="font-family: 宋体;
word-break: break-all;">
<tr>
<td width="8%" align="center" style="font-weight: bold; height: 25px">
序号
</td>
<td width="20%" align="center" style="font-weight: bold;">
硒鼓型号
</td>
<td width="12%" align="center" style="font-weight: bold;">
硒鼓类别
</td>
<td width="15%" align="center" style="font-weight: bold;">
第一批次
</td>
<td width="15%" align="center" style="font-weight: bold;">
第二批次
</td>
<td width="15%" align="center" style="font-weight: bold;">
第三批次
</td>
<td width="15%" align="center" style="font-weight: bold;">
第四批次
</td>
</tr>
<asp:Repeater ID="rptXQPC" runat="server">
<ItemTemplate>
<tr>
<td width="8%" align="center" height="25px">
<%#Eval("ROWID")%>
</td>
<td width="20%" align="center">
<%#Eval("xgxh")%>
</td>
<td width="12%" align="center">
<%#Eval("XGLB")%>
</td>
<td width="15%" align="center">
<%#Eval("First")%>
</td>
<td width="15%" align="center">
<%#Eval("Second")%>
</td>
<td width="15%" align="center">
<%#Eval("Third")%>
</td>
<td width="15%" align="center">
<%#Eval("Fourth")%>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
<tr>
<td colspan="3" width="40%" align="right" style="font-weight: bold; height: 25px">
批次合计:
</td>
<td width="15%" align="center" style="font-weight: bold;">
<div id="divFirst" runat="server"></div>
</td>
<td width="15%" align="center" style="font-weight: bold;">
<div id="divSecond" runat="server"></div>
</td>
<td width="15%" align="center" style="font-weight: bold;">
<div id="divThird" runat="server"></div>
</td>
<td width="15%" align="center" style="font-weight: bold;">
<div id="divFourth" runat="server"></div>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
<!--endprint-->
</div>
<!--startprint-->
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" style="word-break: break-all;">
<tr>
<td align="center" colspan="7" valign="middle" style="font-size: 14px; font-family: 宋体; font-weight: bold;
height: 30px">
<span id="spanYEAR" runat="server"></span>年<span id="spanMONTH" runat="server"></span>月分公司月度需求生产任务汇总
</td>
</tr>
<tr>
<td align="right" valign="middle" colspan="7">
<table width="100%">
<tr>
<td colspan="2" align="left">
数量单位:支
</td>
<%-- <td width="10%" align="center" style="font-weight: bold;">
</td>--%>
<td width="20%" align="center" style="font-weight: bold;">
</td>
<td width="25%" colspan="2" align="center">
</td>
<td width="40%" colspan="2" align="center">
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" style="font-size: 12px;
font-family: 宋体; word-break: break-all;">
<tr>
<td colspan="4" align="left" valign="top">
<div id="div" runat="server">
<table width="100%" border="1" align="center" cellpadding="0" cellspacing="0" style="font-family: 宋体;
word-break: break-all;">
<tr>
<td width="8%" align="center" style="font-weight: bold; height: 25px">
序号
</td>
<td width="20%" align="center" style="font-weight: bold;">
硒鼓型号
</td>
<td width="12%" align="center" style="font-weight: bold;">
硒鼓类别
</td>
<td width="15%" align="center" style="font-weight: bold;">
第一批次
</td>
<td width="15%" align="center" style="font-weight: bold;">
第二批次
</td>
<td width="15%" align="center" style="font-weight: bold;">
第三批次
</td>
<td width="15%" align="center" style="font-weight: bold;">
第四批次
</td>
</tr>
<asp:Repeater ID="rptXQPC" runat="server">
<ItemTemplate>
<tr>
<td width="8%" align="center" height="25px">
<%#Eval("ROWID")%>
</td>
<td width="20%" align="center">
<%#Eval("xgxh")%>
</td>
<td width="12%" align="center">
<%#Eval("XGLB")%>
</td>
<td width="15%" align="center">
<%#Eval("First")%>
</td>
<td width="15%" align="center">
<%#Eval("Second")%>
</td>
<td width="15%" align="center">
<%#Eval("Third")%>
</td>
<td width="15%" align="center">
<%#Eval("Fourth")%>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
<tr>
<td colspan="3" width="40%" align="right" style="font-weight: bold; height: 25px">
批次合计:
</td>
<td width="15%" align="center" style="font-weight: bold;">
<div id="divFirst" runat="server"></div>
</td>
<td width="15%" align="center" style="font-weight: bold;">
<div id="divSecond" runat="server"></div>
</td>
<td width="15%" align="center" style="font-weight: bold;">
<div id="divThird" runat="server"></div>
</td>
<td width="15%" align="center" style="font-weight: bold;">
<div id="divFourth" runat="server"></div>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
<!--endprint-->
</div>
后台代码如下 :
绑定Repeater数据这里接就不多做介绍了(用的Repeater嵌套)
下面贴出后台导出Excel的方法:
/// <summary>
/// 将数据导出到excel,与下面的函数同时使用才能正常工作
/// </summary>
/// <param name="ctl"></param>
public void ToExcel(System.Web.UI.Control ctl)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "";
string filename = "Report" + System.DateTime.Now.ToString("_yyyyMMddHHmm");
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" +
System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
/// 将数据导出到excel,与下面的函数同时使用才能正常工作
/// </summary>
/// <param name="ctl"></param>
public void ToExcel(System.Web.UI.Control ctl)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "";
string filename = "Report" + System.DateTime.Now.ToString("_yyyyMMddHHmm");
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" +
System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
后台导出Excel时直接调用为 ToExcel(this.export); 这里的export是加了runat="server"的div名称,
这句代码的意思是调用ToExcel的方法导出export里面的页面数据(页面上的数据是怎么展示的,导出来以后的Excel数据会以同样的方式展示)
ASP.net中导出Excel的简单方法介绍
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。