• EXcel


    #region 生成Excel文件

                FileStream fsExcel = null;
                StreamWriter swExcel = null;

                string excelPath = Request.PhysicalApplicationPath + ExportSeqNo + ".xls";

                if (File.Exists(excelPath))
                {
                    File.Delete(excelPath);
                }
                fsExcel = new FileStream(excelPath, FileMode.CreateNew, FileAccess.ReadWrite);
                swExcel = new StreamWriter(fsExcel);
                swExcel.WriteLine("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
                swExcel.WriteLine("<head>");
                swExcel.WriteLine("<!--[if gte mso 9]>");
                swExcel.WriteLine("<xml>");
                swExcel.WriteLine(" <x:ExcelWorkbook>");
                swExcel.WriteLine("   <x:ExcelWorksheets>");
                swExcel.WriteLine("    <x:ExcelWorksheet>");
                swExcel.WriteLine("     <x:Name>手机充值表</x:Name>");
                swExcel.WriteLine("     <x:WorksheetOptions>");
                swExcel.WriteLine("       <x:Print>");
                swExcel.WriteLine("        <x:ValidPrinterInfo />");
                swExcel.WriteLine("       </x:Print>");
                swExcel.WriteLine("     </x:WorksheetOptions>");
                swExcel.WriteLine("    </x:ExcelWorksheet>");
                swExcel.WriteLine("   </x:ExcelWorksheets>");
                swExcel.WriteLine("</x:ExcelWorkbook>");
                swExcel.WriteLine("</xml>");
                swExcel.WriteLine("<![endif]-->");
                swExcel.WriteLine("</head>");
                swExcel.WriteLine("<body>");
                swExcel.WriteLine("<table style='text-align:center;'>");
                swExcel.WriteLine(" <tr>");
                swExcel.WriteLine("   <td><strong>序号(订单号)</strong></td>");
                swExcel.WriteLine("   <td><strong>UID</strong></td>");
                swExcel.WriteLine("   <td><strong>手机号</strong></td>");
                swExcel.WriteLine("   <td><strong>充值金额</strong></td>");
                swExcel.WriteLine("   <td><strong>状态</strong></td>");
                swExcel.WriteLine("   <td><strong>提现日期</strong></td>");
                swExcel.WriteLine(" </tr>");
                foreach (DataRow dr in dt.Rows)
                {
                    string status = EnumHelper.GetEnumDescriptionByValue<Payment.CouponAccount.Model.Common.Enums.PayOutChangeStatusEnum>(Convert.ToInt32(dr["PayStatus"].ToString().Trim()));
                    swExcel.WriteLine(" <tr>");
                    swExcel.WriteLine("   <td style='vnd.ms-excel.numberformat:@;'>" + dr["ID"].ToString().Trim() + "</td>");
                    swExcel.WriteLine("   <td>" + dr["UID"].ToString().Trim() + "</td>");
                    swExcel.WriteLine("   <td>" + dr["Mobile"].ToString().Trim() + "</td>");
                    swExcel.WriteLine("   <td style='vnd.ms-excel.numberformat:@;'>" + dr["Amount"].ToString().Trim() + "</td>");
                    swExcel.WriteLine("   <td>" + status + "</td>");
                    swExcel.WriteLine("   <td>" + dr["PayOutTime"].ToString().Trim() + "</td>");
                    swExcel.WriteLine(" </tr>");
                }
                swExcel.WriteLine("</table>");
                swExcel.WriteLine("</body>");
                swExcel.WriteLine("</html>");
                swExcel.Dispose();
                fsExcel.Dispose();

                byte[] excelBuffer = File.ReadAllBytes(excelPath);
                //删除Excel文件
                File.Delete(excelPath);

                Response.Clear();
                Response.Charset = "utf-8";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + ExportSeqNo + ".xls");
                Response.ContentType = "application/ms-excel";
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.HeaderEncoding = System.Text.Encoding.UTF8;
                Response.BinaryWrite(excelBuffer);

  • 相关阅读:
    Oracle中的4大空值处理函数用法举例
    PyCharm安装
    Python安装与环境变量的配置
    多层分组排序问题
    将时间点的数据变成时间段的数据
    根据状态变化情况,求最大值和最小值
    ubuntu 源码安装 swig
    CSDN博客排名第一名,何许人也
    thinkPHP的常用配置项
    拔一拔 ExtJS 3.4 里你遇到的没遇到的 BUG(1)
  • 原文地址:https://www.cnblogs.com/qianlong/p/2744621.html
Copyright © 2020-2023  润新知