日期:2014-05-17  浏览次数:20505 次

生成excel之后如何保存到服务器指定目录中?
小弟目前初学,碰到个问题请大家帮忙看下。 下面那个方法是把数据以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();

------解决方案--------------------
save 通过路径来保存!
------解决方案--------------------
HttpContext.Current.Response.Write(sw);
改成
System.IO;File.WriteAllText(Server.MapPath("~/xx/x.xls"),sw)