zoukankan      html  css  js  c++  java
  • asp.net——XML格式导出Excel

    下面介绍一种导出Excel的方法:

    此方法不需要在服务器上安装Excel,采用生成xmlexcel方式输出到客户端,可能需要客户机安装excel所以也不会有乱七八糟的权限设定,和莫名其妙的版本问题。而且此种方法比第二种更快试了下10W条数据(20列)不到10秒(小白pc)。

     

    protected void Btn_Up(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.ContentType = "application/vnd.ms-excel";
        this.EnableViewState = false;
        
        Hashtable ht = new Hashtable();
        try
        {
            ht["PayAcc"] = payeeaccno.Value;
            ht["BgDate"] = rzbDate.Value.Replace("-", "");
            ht["EdDate"] = rzEDate.Value.Replace("-", "");
            ht["BizBrid"] = bizBrandid;
        }
        catch (Exception)
        {
            throw;
        }
    
        WithholdingQueryBll wiBill = new WithholdingQueryBll();//数据操作类
        int count = 0;
        DataTable qdp = wiBill.QuerDeductPayee(ht, true, out count);//查询数据,返回DataTable
        CreateExcel(qdp, "", "xls");
    }
    
    public void CreateExcel(System.Data.DataTable dt, String TableTitle, string fileName)
    {
        //1)  文本:vnd.ms-excel.numberformat:@
        //2)  日期:vnd.ms-excel.numberformat:yyyy/mm/dd
        //3)  数字:vnd.ms-excel.numberformat:#,##0.00
        //4)  货币:vnd.ms-excel.numberformat:¥#,##0.00
        //5)  百分比:vnd.ms-excel.numberformat: #0.00%
        StringBuilder strb = new StringBuilder();
        DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
        strb.Append(" <html xmlns:o="urn:schemas-microsoft-com:office:office"");
        strb.Append("xmlns:x="urn:schemas-microsoft-com:office:excel"");
        strb.Append("xmlns=" strb.Append("<head> <meta http-equiv='Content-Type' content='text/html; charset=gb2312'>");
        strb.Append(" <style>");
        strb.Append("body");
        strb.Append(" {mso-style-parent:style0;");
        strb.Append(" font-family:"Times New Roman", serif;");
        strb.Append(" mso-font-charset:0;");
        strb.Append(" mso-number-format:"@";}");
        strb.Append("table");
        strb.Append(" {border-collapse:collapse;margin:1em 0;line-height:20px;font-size:12px;color:#222; margin:0px;}");
        strb.Append("thead tr td");
        strb.Append(" {background-color:#e3e6ea;color:#6e6e6e;text-align:center;font-size:14px;}");
        strb.Append("tbody tr td");
        strb.Append(" {font-size:12px;color:#666;}");
        strb.Append(" </style>");
        strb.Append(" <xml>");
        strb.Append(" <x:ExcelWorkbook>");
        strb.Append(" <x:ExcelWorksheets>");
        strb.Append(" <x:ExcelWorksheet>");
        strb.Append(" <x:Name>Sheet1 </x:Name>");
        strb.Append(" <x:WorksheetOptions>");
        strb.Append(" <x:DefaultRowHeight>285 </x:DefaultRowHeight>");
        strb.Append(" <x:Selected/>");
        strb.Append(" <x:Panes>");
        strb.Append(" <x:Pane>");
        strb.Append(" <x:Number>3 </x:Number>");
        strb.Append(" <x:ActiveCol>1 </x:ActiveCol>");
        strb.Append(" </x:Pane>");
        strb.Append(" </x:Panes>");
        strb.Append(" <x:ProtectContents>False </x:ProtectContents>");
        strb.Append(" <x:ProtectObjects>False </x:ProtectObjects>");
        strb.Append(" <x:ProtectScenarios>False </x:ProtectScenarios>");
        strb.Append(" </x:WorksheetOptions>");
        strb.Append(" </x:ExcelWorksheet>");
        strb.Append(" <x:WindowHeight>6750 </x:WindowHeight>");
        strb.Append(" <x:WindowWidth>10620 </x:WindowWidth>");
        strb.Append(" <x:WindowTopX>480 </x:WindowTopX>");
        strb.Append(" <x:WindowTopY>75 </x:WindowTopY>");
        strb.Append(" <x:ProtectStructure>False </x:ProtectStructure>");
        strb.Append(" <x:ProtectWindows>False </x:ProtectWindows>");
        strb.Append(" </x:ExcelWorkbook>");
        strb.Append(" </xml>");
        strb.Append("");
        strb.Append(" </head> <body> ");
        //strb.Append(" <table> <thead><tr>");
        //strb.Append(" <td colspan="" + ds.Columns.Count + "" >");
        //strb.Append(TableTitle);
        //strb.Append(" </td> ");
        //strb.Append(" </tr>");
        //strb.Append(" </thead><tbody><tr>");
        strb.Append(" <table> <tbody><tr>");
        //写列标题
        strb.Append(" <td> <b>" + "交易日期" + " </b> </td>");
        strb.Append(" <td> <b>" + "交易时间" + " </b> </td>");
        strb.Append(" <td> <b>" + "银行流水号" + " </b> </td>");
        strb.Append(" <td> <b>" + "发起方(第三方)流水号" + " </b> </td>");
        strb.Append(" <td> <b>" + "收款人账号" + " </b> </td>");
        strb.Append(" <td> <b>" + "收款人名称" + " </b> </td>");
        strb.Append(" <td> <b>" + "付款人账号" + " </b> </td>");
        strb.Append(" <td> <b>" + "付款人名称" + " </b> </td>");
        strb.Append(" <td> <b>" + "付款人开户行" + " </b> </td>");
        strb.Append(" <td> <b>" + "交易金额" + " </b> </td>");
        strb.Append(" <td> <b>" + "交易标志" + " </b> </td>");
        strb.Append(" <td> <b>" + "结果信息" + " </b> </td>");
        //strb.Append(" <td> <b>" + "付款人户名" + " </b> </td>");
        //strb.Append(" <td> <b>" + "成功/失败原因" + " </b> </td>");
        strb.Append(" </tr>");
        //根据查询出来的字段遍历数据
        foreach (DataRow row in myRow)
        {
            strb.Append(" <tr>");
            strb.Append(" <td>" + row["TRANDATE"].ToString() + " </td>");
            strb.Append(" <td>" + row["TRANTIME"].ToString() + " </td>");
            strb.Append(" <td style="vnd.ms-excel.numberformat:@">" + row["TRANSEQNO"].ToString() + " </td>");
            strb.Append(" <td style="vnd.ms-excel.numberformat:@">" + row["THSEQNO"].ToString() + " </td>");
            strb.Append(" <td style="vnd.ms-excel.numberformat:@">" + row["PAYEENO"].ToString() + " </td>");
            strb.Append(" <td>" + row["PAYEENAME"].ToString() + " </td>");
            strb.Append(" <td style="vnd.ms-excel.numberformat:@">" + row["PAYERNO"].ToString() + " </td>");
            strb.Append(" <td>" + row["PAYERNAME"].ToString() + " </td>");
            strb.Append(" <td style="vnd.ms-excel.numberformat:@">" + row["PAYERBANK"].ToString() + " </td>");
            strb.Append(" <td style="vnd.ms-excel.numberformat:¥#,##0.00">" + row["TRANAMT"].ToString() + " </td>");
            strb.Append(" <td>" + row["TRANSTAT"].ToString() + " </td>");
            strb.Append(" <td>" + row["REMSG"].ToString() + " </td>");
            //strb.Append(" <td>" + row["PAYER_NAME"].ToString() + " </td>");
            //strb.Append(" <td>" + row["RET_MSG"].ToString() + " </td>");
            strb.Append(" </tr>");
        }
    
        strb.Append(" </tbody> </table>");
        strb.Append(" </body> </html>");
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        //Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        //设置输出流为简体中文 
        Response.ContentType = "application/ms-excel";
        //设置输出文件类型为excel文件。 
        this.EnableViewState = false;
        Response.Write(strb);
        Response.End();
    }
    View Code

     

     

  • 相关阅读:
    JNI接口的使用(简单版)
    Android内核剖析(1)
    Spring 和 Mybatis 整合
    AppStore被拒原因及总结
    iOS开发中,应用内直接跳转到Appstore
    ios 中生成二维码和相册中识别二维码
    IOS开发中(null)与<null>的处理
    融云即时通讯~~
    iOS中菊花。。。
    关于判断邮箱 手机号等一系列的正则表达式
  • 原文地址:https://www.cnblogs.com/witeem/p/4229255.html
Copyright © 2011-2022 走看看