• ASP.net中导出Excel的简单方法


          下面介绍一种ASP.net中导出Excel的简单方法

          先上代码:前台代码如下(这是自己项目里面写的一点代码先贴出来吧)

         <div id="export" runat="server" style=" 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">
                                    &nbsp;数量单位:支
                                </td>
                                <%--   <td width="10%" align="center" style="font-weight: bold;">
                                    &nbsp;
                                </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时直接调用为  ToExcel(this.export);  这里的export是加了runat="server"的div名称,

       这句代码的意思是调用ToExcel的方法导出export里面的页面数据(页面上的数据是怎么展示的,导出来以后的Excel数据会以同样的方式展示)

  • 相关阅读:
    还敢说你是程序员?一律师闲着没事写了个app,用户量600万
    cnentos中进行bond网卡配置,一切配置无问题,就是ping不通宿主机
    他曾被腾讯、百度、金山、遨游等联合封杀,如今他发展的却更好
    百度命不久矣?他为什么这么说?
    html实现下拉框、switch开关、复选框效果
    javascript拖拽滑动条
    行内元素默认间距的4种解决办法
    Html5 video用法详解
    npm装包时-S和-D的区别
    css3实现背景模糊的三种方式
  • 原文地址:https://www.cnblogs.com/wolipengbo/p/2584276.html
Copyright © 2020-2023  润新知