C# 之 下载EXCEL文件,自动用迅雷下载aspx

  在浏览器中导出 Excel 得时候,如果浏览器绑定了迅雷,则会下载aspx文件。

  

解决:下载EXCEL文件,自动用迅雷下载aspx

if (Request.QueryString["id"] != null && !string.IsNullOrEmpty(Request.QueryString["id"].ToString()))
        {
            if (Request.QueryString["oid"] != null && !string.IsNullOrEmpty(Request.QueryString["oid"].ToString()))
            {
                id = Request.QueryString["id"].ToString();
                oid = Request.QueryString["oid"].ToString();
                string sqlWhere = string.Format("PProductNumber='{0}' and OrdersNumber='{1}'", id, oid);
                List<SonOrders> li=   _SonOrdersManager.GetOrdersLists(sqlWhere);

            if (li.Count > 0&&li!=null)
            {
                MemoryStream ms = new MemoryStream();
                IWorkbook workbook = new HSSFWorkbook();
                ISheet sheet = workbook.CreateSheet();
                ICellStyle style1 = workbook.CreateCellStyle();
                IFont font = workbook.CreateFont();
                font.FontHeightInPoints = 11;
                style1.SetFont(font);
                sheet.DefaultRowHeight = 25 * 20;
      
                for (int i = 0; i < 9; i++)
                {
                    if (i == 0)
                        sheet.SetColumnWidth(i,28 * 256);                     
                    else
                        sheet.SetColumnWidth(i, 23 * 256);
                    sheet.SetDefaultColumnStyle(i, style1);
                }
                IRow row1 = sheet.CreateRow(0);
                ICell cell = row1.CreateCell(0);
                cell.SetCellValue(oid+"_状态详细");
                ICellStyle style2 = workbook.CreateCellStyle();
                style2.Alignment = HorizontalAlignment.CENTER;
                IFont font2 = workbook.CreateFont();
                font2.FontHeight = 20 * 20;
                style2.SetFont(font2);
                cell.CellStyle = style2;
                sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 7));
                IRow headerRow = sheet.CreateRow(1);
                headerRow.CreateCell(0).SetCellValue("产品编号");
                headerRow.CreateCell(1).SetCellValue("下单时间");
                headerRow.CreateCell(2).SetCellValue("原型验收时间");
                headerRow.CreateCell(3).SetCellValue("首件验收时间");
                headerRow.CreateCell(4).SetCellValue("组装验收时间");
                headerRow.CreateCell(5).SetCellValue("厂家出库时间");
                headerRow.CreateCell(6).SetCellValue("现场入库时间");
                headerRow.CreateCell(7).SetCellValue("领用入库时间");
                headerRow.CreateCell(8).SetCellValue("开箱验收时间");
                headerRow.CreateCell(9).SetCellValue("安装完成时间");
                headerRow.CreateCell(10).SetCellValue("安装验收时间");

                 int j = 2;
                 foreach (SonOrders so in li)
                 {
                     IRow dataRow = sheet.CreateRow(j++);
                     dataRow.CreateCell(0).SetCellValue(so.SProductNumber);
                     dataRow.CreateCell(1).SetCellValue(GetTimeS("下单时间", so.SProductNumber).ToString());
                     dataRow.CreateCell(2).SetCellValue(GetTimeS("原型验收时间", so.SProductNumber).ToString());
                     dataRow.CreateCell(3).SetCellValue(GetTimeS("首件验收时间", so.SProductNumber).ToString());
                     dataRow.CreateCell(4).SetCellValue(GetTimeS("组装验收时间", so.SProductNumber).ToString());
                     dataRow.CreateCell(5).SetCellValue(GetTimeS("厂家出库时间", so.SProductNumber).ToString());
                     dataRow.CreateCell(6).SetCellValue(GetTimeS("现场入库时间", so.SProductNumber).ToString());
                     dataRow.CreateCell(7).SetCellValue(GetTimeS("领用入库时间", so.SProductNumber).ToString());
                     dataRow.CreateCell(8).SetCellValue(GetTimeS("开箱验收时间", so.SProductNumber).ToString());
                     dataRow.CreateCell(9).SetCellValue(GetTimeS("安装完成时间", so.SProductNumber).ToString());
                     dataRow.CreateCell(10).SetCellValue(GetTimeS("安装验收时间", so.SProductNumber).ToString());
                 }
                 workbook.Write(ms);

               
                 ms.Flush();
                 ms.Position = 0;

                 try
                 {
                     string[] files = Directory.GetFiles(System.AppDomain.CurrentDomain.BaseDirectory + "excels/");
                     foreach (string file in files)
                     {
                         File.Delete(file);
                     }
                 }
                 catch (Exception exx)
                 {
                     Response.Write(exx.Message.ToString());
                 }
                 string pname = "状态详细_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";
                 NpoiHelper.SaveToFile(ms, System.AppDomain.CurrentDomain.BaseDirectory + "excels/" + pname);
                 Response.Redirect("~/excels/" + pname, false);
                 Response.Clear(); 

                // Response.Clear();
                // Response.ClearHeaders();
                // Response.Buffer = false;
                // Response.ContentType = "application/octet-stream";
                // if (Request.Browser.Browser == "Firefox")
                //     System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + oid + "_状态详细.xls");                     
                //else
                //     System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(oid + "_状态详细.xls", System.Text.Encoding.UTF8)); 
                // System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", ms.Length.ToString());
                // System.Web.HttpContext.Current.Response.BinaryWrite(ms.GetBuffer());
                // System.Web.HttpContext.Current.Response.Flush();
                // System.Web.HttpContext.Current.Response.End();
            }   
            }

 

posted on 2019-01-30 14:00  Now,DayBreak  阅读(992)  评论(0编辑  收藏  举报