小弟目前初学,碰到个问题请大家帮忙看下。 下面那个方法是把数据以ms-execl的格式,以Response输出到浏览器(客户端),
怎么才能保存到服务器指定的目录中呢
      public static void DataSetToExcel(DataSet ds)
        {
            DataTable dee = ds.Tables[0];
            dee.DataSet.DataSetName = ds.ToString();
            HttpContext.Current.Response.Clear();
            System.IO.StringWriter sw = new System.IO.StringWriter();            //写数据,省略
            ...
            ...
            ...            sw.Close();
            string ls_FileName = "attachment; filename=";
            ls_FileName += FileHelper.GetRandom();
            ls_FileName += ".xls";            HttpContext.Current.Response.AddHeader("Content-Disposition", ls_FileName);            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");            HttpContext.Current.Response.Write(sw);            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();

解决方案 »

  1.   

     HttpContext.Current.Response.Write(sw);
    改成
    System.IO;File.WriteAllText(Server.MapPath("~/xx/x.xls"),sw)
      

  2.   

    Server.MapPath("~/xx/" + "这里你可以随你改 啊" + ".xls")
      

  3.   

    是我问题没描述清楚,我的意思是这样传入的路径中的文件名是固定的,比如我写的
    File.WriteAllText(HttpRuntime.AppDomainAppPath + "\\Temp\\123.xls", sw.ToString());
    其中123.xls这个文件是每执行一次就会往里面写一次数据。如果多个用户同时操作那么不可能每次操作都能保存一次的吧 而是会覆盖原来的。这个问题我自己再想办法解决,晚点我再结贴了,还是谢谢你的帮助。
      

  4.   

    问题已经解决了,我的方法是在写入数据之前先将这个文件123.xls COPY一份另存,再写入这个复件。这样就OK啦!