我正在使用ASP.NET C#中的Respose.Write创建一个excel文件,我的用户可以将创建的文件保存在他们的系统上,但是我想将这个excel文件保存在我的服务器上,而无需用户了解它。这怎么可能?这就是我创建Excel文件的方式:
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" + "res" + ".xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
...
Response.Write("</table>");
Response.Flush();
Response.End();
此代码保存创建的excel文件客户端PC,但我想在服务器上保存创建的文件
答案 0 :(得分:2)
一般的想法是将Excel文件的内容写入缓冲区。然后a)将其保存到文件中,然后b)将其传输到客户端。您将需要在服务器上使用唯一的文件命名约定(例如,包括时间戳)。 IIS应用程序池还需要对要将文件保存到的文件夹具有写入权限。
StringBuilder excel = new StringBuilder();
excel.Append("<table>");
...
excel.Append("</table>");
// save to file
File.WriteAllText("C:\\blah.xls", excel.ToString());
// output to response
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" + "res" + ".xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
Response.Write(excel.ToString());
Response.End();