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

原文地址:https://www.cnblogs.com/xinaixia/p/10337833.html

时间: 2024-08-29 15:23:23

C# 之 下载EXCEL文件,自动用迅雷下载aspx的相关文章

C# Web 下载excel文件后直接导入下载的文件报错的问题

下载代码如下: System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.ClearHeaders(); System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");System.Web.HttpContext.Current.R

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

解决:下载EXCEL文件,自动用迅雷下载aspx if (Request.QueryString["id"] != null && !string.IsNullOrEmpty(Request.QueryString["id"].ToString())) { if (Request.QueryString["oid"] != null && !string.IsNullOrEmpty(Request.QueryStr

自动更新开奖数据的excel文件,供大家下载

自动更新开奖数据的excel文件,供大家下载 2010-03-14 20:22 228492人阅读打印来源:乐彩网 作者:eren 很多人拥有自制excel电子表格,常要更新最基本的开奖信息.如有多期未更新,则费时更多.乐彩网为大家提供八种彩票的自动更新文件,供下载.您只需点击更新按钮,就能得到最及时全面的开奖信息,省时省力. 2011年10月12日修改说明:已更新全部开奖数据.福彩3Dexcel文件中,因描述更新范围的高度小了,导致更新后,最下面内容会右移.如此文件单独存在,请直接下载.如已将

前端下载excel文件功能的三种方法

1 从后端接收json数据,前端处理生成excel下载 JsonExportExcel的github地址:https://github.com/cuikangjie/JsonExportExcel 这种方式比较适用于该数据需要能够导出下载并且同时要展现在页面的场景 2 通过form表单接收文件 如果后端已经处理成了excel,就不需要前端在处理生成,但是Ajax能够返回的数据格式只能为html,script,json,xml,不能直接接受excel文件,如果你直接通过ajax去获取文件就会报错.

jsp下载excel文件

jsp下载excel文件的的实现方法很多,今天也遇到这个问题,乱敲了一阵,终于搞定了,记下来和朋友们分享吧. 假设需要下载excel文件的jsp页面名为:down.jsp 对应的后台action名为:downExcel.java(用struts实现,其他框架也类似) 在downExcel.java的添加downAction方法,实现下载步骤 public ActionForward downAction(ActionMapping mapping, ActionForm form,    Htt

下载EXCEL文件Utils

import jxl.*; import jxl.biff.DisplayFormat; import jxl.format.Alignment; import jxl.format.BoldStyle; import jxl.format.Border; import jxl.format.BorderLineStyle; import jxl.format.*; import jxl.format.VerticalAlignment; import jxl.write.*; import j

angularJS通过post方法下载excel文件

最近工作中遇到,要使用angularJS的post方法来下载excel的情况.网上找到一个帖子:http://stackoverflow.com/questions/22447952/angularjs-http-post-convert-binary-to-excel-file-and-download ,改动了里面部分代码搞定. 详细代码: $http.post($rootScope.restful_api.last_output_excel,body_data,{responseType:

JavaWeb项目导出Excel文件并弹出下载框

引言 在Java Web开发中经常涉及到报表,最近做的项目中需要实现将数据库中的数据显示为表格,并且实现导出为Excel文件的功能. 相关jar包 使用POI可以很好的解决Excel的导入和导出的问题,POI下载地址: poi-3.6-20091214.jar 关键代码 首先导入上述jar包. 在生成excel时一般数据源形式为一个List,下面把生成Excel格式的代码贴出来: /** * 以下为生成Excel操作 */ // 1.创建一个workbook,对应一个Excel文件 HSSFWo

MVC下载Excel文件:js接受乱码 简单处理办法

在做考试系统的时候,需要向题库里面批量导入试题.所以在导题之前需要下载一个Excel模版. 在MVC中下载模版,需要创建一个Excel模版,然后发到前台.即点击之后,弹出一个下载框,提示下载(保存位置). 现象:在MVC中,点击下载模版按钮,不提示下载. 原因:js接受 FileResult类型乱码. 如果是单纯的生成一个特定的Excel的文件,然后发到前台,即没有参数的下载.如果是这样的话直接让Controller创建,返回一个FileResult文件,前台就直接弹窗提示了. 但是下载导入模版