[英]save file as excel is folder using string writer
我已经从asp.net表创建了excel文件,现在可以下载它了。
现在,我想将此excel文件保存在服务器上以便能够使用它。 我尝试使用字符串编写器保存文件文件夹,但未成功。 这是我编写的从asp.net生成文件的代码。
tbl_loctbl.Controls.Clear();
LoadDetails();
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=ProfessorWise.xls");
Response.ContentType = "application/ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);
tbl_loctbl.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();
根据此代码,将生成文件并将其下载到用户pc。 Loaddetails()
是将数据加载到asp.net表,然后生成excel文件的函数。 如何将其保存到服务器?
如果我正确理解了您的要求,则希望使用Web应用程序将文件保存在Web服务器上。
如果要在Web服务器上使用IIS来运行c#应用程序,则需要向文件夹applicationpool授予要将excel文件/文件夹写入其中的权限。
applicationpool必须与Web应用程序使用的相同。
Update:
首先,您需要授予保存文件/文件夹的文件夹权限。 您需要授予的权限是您的Web应用程序正在使用的applicationpool。
之后,在IIS中,右键单击您的站点并添加一个虚拟目录。 在这里,您可以定义文件夹的别名。 您的代码会将其识别为项目中的实际文件夹。 您需要在此处选择实际的物理路径。
尝试使用这个
string path = Server.MapPath("Print_Files"); // folder path
Random rnd = new Random();
int month = rnd.Next(1, 13); // creates a number between 1 and 12
int dice = rnd.Next(1, 7); // creates a number between 1 and 6
int card = rnd.Next(9); // creates a number between 0 and 51
string file_name = "filename" +month + dice + card + ".pdf"; // to prevent duplicate
FileStream file = new FileStream(path + "/" + file_name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
file.Write(bytes, 0, bytes.Length);
file.Dispose();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.