我把数据导入到EXCEL,后保存到一个路径,然后用Response.WfriteFile(excel路径)把它输出到客户端.
在生成EXCEL时,我是通过提供客户端下载的方式,让客户端得到该EXCEL文件.
但问题是:每次我运行,提供的文件下载并不是我生成的EXCEL文件,而是些什么CA6NS5QJ.aspx/CA3AY1VB.aspx之类的网页文件,并且每次运行后都是不相同的.以前我做的都可以提供生成的EXCEL文件下载的,不知道怎么搞的现在不行了.
请大家帮帮我啊??

解决方案 »

  1.   

    FileInfo fi=new FileInfo(filepath);
    System.Web.HttpContext.Current.Response.Clear();
    System.Web.HttpContext.Current.Response.ClearHeaders();
    System.Web.HttpContext.Current.Response.Buffer = false;
    System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
    System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(fi.FullName,System.Text.Encoding.UTF8));
    System.Web.HttpContext.Current.Response.AppendHeader("Content-Length",fi.Length.ToString());
    System.Web.HttpContext.Current.Response.WriteFile(fi.FullName);
    System.Web.HttpContext.Current.Response.Flush();
    fi.Delete();
    System.Web.HttpContext.Current.Response.End();
      

  2.   

    对了,我还想问下关于WEB导入再打印的并发性问题:
    我用来生成EXCEL的数据源是不断变化的,但个人可能要打印的内容也不一样,比如数据是分页 的,有的人只打印第1页的数据,有的打印第2也的数据.我的想法是把所有的生成的EXCEL都命名为一个相同的名字,如果是不同的名字的话,那生成的EXCEL文件太多了.
    所以我的问题是:如果某个人点了打印,但马上又有另一个人点了打印,那第一个人点打印生成的EXCEL文件会不会被第2个人的覆盖掉呢?????
    请大家帮我分析分析.
      

  3.   

    ExcelFileName+Guid.NewGuid().ToString()
    就不会有并发问题.文件生成后都被删除,不存在EXCEL文件多的问题.
      

  4.   

    具体的可参考一下
    http://www.cnblogs.com/elevenWolf/archive/2004/08/21/35324.html
      

  5.   

    可以用类似下面的代码到处到excel:
    Response.ContentType = "application/vnd.ms-excel";
                Response.Charset = "";
    this.EnableViewState = false;
    System.IO.StringWriter sw = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
    int nCur = dgShow.CurrentPageIndex;
    int nSize = dgShow.PageSize;

    dgShow.AllowPaging = false;
    BindData();

    dgShow.Columns[7].Visible =false;
    dgShow.RenderControl(hw);
    dgShow.Columns[7].Visible =true;

    //以下恢复分页
    dgShow.AllowPaging = true;
    dgShow.CurrentPageIndex = nCur;
    dgShow.PageSize = nSize;
    BindData();
    Response.Write(sw.ToString());
    Response.End();
      

  6.   

    注意其中最关键的是第一句Response.ContentType = "application/vnd.ms-excel";指定浏览器处理的类型为excel就不会出楼主碰到的问题。
      

  7.   

    第二个问题应该不会覆盖掉。
    因为web是并发的。
    但如果你的结果是cache等中暂存的而不是每次都调数据源的,那可能有问题。
    一般来讲可以这样做。
      

  8.   

    string templateFile = @"e:\CopperExcel.xls";
    string outputFile = @"e:\CopperExcel.xls";
    coppers = (new CopperBF()).GetCoppersWithConditions(moldIdFr, moldIdTo, workpieceIdFr,workpieceIdTo,copperIdFr,copperIdTo,makeDocTimeFr, makeDocTimeTo, copperState,followFactoryId,followManagerId, followCopperId, currentPage, 10000);CopperExcel cppExcel = new CopperExcel (templateFile,outputFile);
    int count = coppers.Count;
    cppExcel.EntityToExcel(coppers);
    System.IO.FileInfo fi=new System.IO.FileInfo (outputFile);
    System.Web.HttpContext.Current.Response.Clear();
    System.Web.HttpContext.Current.Response.ClearHeaders();
    System.Web.HttpContext.Current.Response.Buffer = false;
    System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
    System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(fi.FullName,System.Text.Encoding.UTF8));
    System.Web.HttpContext.Current.Response.AppendHeader("Content-Length",fi.Length.ToString());
    System.Web.HttpContext.Current.Response.WriteFile(fi.FullName);
    System.Web.HttpContext.Current.Response.Flush();
    fi.Delete();
    System.Web.HttpContext.Current.Response.End();
    System.GC.Collect();运行出现错误:值不能为空.\r\n参数名:obj.请问这是什么原因啊?????
    System.IO.FileInfo fi=new System.IO.FileInfo (outputFile);就是这一句出错.这个outputFile是我在程序里生成一个EXCEL文件,并保存的.但是我的另一个程序:直接用已经存在的文件路径做参数,就可以啊?为什么啊???
      

  9.   

    对了,我还想问下:web打印的进程处理啊
    我刚才测试了,每次提供下载后,在服务器端会产生很多进程啊,而客护端只有一个啊.
    这样累计下去的话,服务器的内存都会被占掉啊???
    请问怎么对服务器段的进程正确的处理,不要杀死其他客户端 的进程啊????