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

时间:2021-10-17 17:51:03

  在浏览器中导出 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 > &&li!=null)
{
MemoryStream ms = new MemoryStream();
IWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet();
ICellStyle style1 = workbook.CreateCellStyle();
IFont font = workbook.CreateFont();
font.FontHeightInPoints = ;
style1.SetFont(font);
sheet.DefaultRowHeight = * ; for (int i = ; i < ; i++)
{
if (i == )
sheet.SetColumnWidth(i, * );
else
sheet.SetColumnWidth(i, * );
sheet.SetDefaultColumnStyle(i, style1);
}
IRow row1 = sheet.CreateRow();
ICell cell = row1.CreateCell();
cell.SetCellValue(oid+"_状态详细");
ICellStyle style2 = workbook.CreateCellStyle();
style2.Alignment = HorizontalAlignment.CENTER;
IFont font2 = workbook.CreateFont();
font2.FontHeight = * ;
style2.SetFont(font2);
cell.CellStyle = style2;
sheet.AddMergedRegion(new CellRangeAddress(, , , ));
IRow headerRow = sheet.CreateRow();
headerRow.CreateCell().SetCellValue("产品编号");
headerRow.CreateCell().SetCellValue("下单时间");
headerRow.CreateCell().SetCellValue("原型验收时间");
headerRow.CreateCell().SetCellValue("首件验收时间");
headerRow.CreateCell().SetCellValue("组装验收时间");
headerRow.CreateCell().SetCellValue("厂家出库时间");
headerRow.CreateCell().SetCellValue("现场入库时间");
headerRow.CreateCell().SetCellValue("领用入库时间");
headerRow.CreateCell().SetCellValue("开箱验收时间");
headerRow.CreateCell().SetCellValue("安装完成时间");
headerRow.CreateCell().SetCellValue("安装验收时间"); int j = ;
foreach (SonOrders so in li)
{
IRow dataRow = sheet.CreateRow(j++);
dataRow.CreateCell().SetCellValue(so.SProductNumber);
dataRow.CreateCell().SetCellValue(GetTimeS("下单时间", so.SProductNumber).ToString());
dataRow.CreateCell().SetCellValue(GetTimeS("原型验收时间", so.SProductNumber).ToString());
dataRow.CreateCell().SetCellValue(GetTimeS("首件验收时间", so.SProductNumber).ToString());
dataRow.CreateCell().SetCellValue(GetTimeS("组装验收时间", so.SProductNumber).ToString());
dataRow.CreateCell().SetCellValue(GetTimeS("厂家出库时间", so.SProductNumber).ToString());
dataRow.CreateCell().SetCellValue(GetTimeS("现场入库时间", so.SProductNumber).ToString());
dataRow.CreateCell().SetCellValue(GetTimeS("领用入库时间", so.SProductNumber).ToString());
dataRow.CreateCell().SetCellValue(GetTimeS("开箱验收时间", so.SProductNumber).ToString());
dataRow.CreateCell().SetCellValue(GetTimeS("安装完成时间", so.SProductNumber).ToString());
dataRow.CreateCell().SetCellValue(GetTimeS("安装验收时间", so.SProductNumber).ToString());
}
workbook.Write(ms); ms.Flush();
ms.Position = ; 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();
}
}