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

 

 

 

转载于:https://www.cnblogs.com/witeem/p/4229255.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值