在瀏覽器中導出 Excel 得時候,若是瀏覽器綁定了迅雷,則會下載aspx文件。sql
解決:下載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(); } }