[英]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.