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);

posted on 2012-10-29 13:44  潜龙  阅读(163)  评论(0编辑  收藏  举报

导航