0

我已经从 asp.net 表创建了 excel 文件,现在可以下载它。

现在我想将这个 excel 文件保存在服务器上以便能够使用它。我尝试使用 string-writer 保存文件夹,但没有成功。这是我为从 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文件的功能。如何将其保存到服务器?

4

2 回答 2

0

尝试使用这个

    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();
于 2013-12-05T07:41:41.423 回答
0

如果我正确理解了您的请求,您希望使用您的网络应用程序将文件保存在您的网络服务器上。

如果您在您的网络服务器上使用 IIS 来运行您的 c# 应用程序,那么您需要授予您的文件夹应用程序池权限,以便将 excel 文件/文件夹写入其中。

应用程序池必须与 web 应用程序使用的相同。

Update:

首先,您需要授予保存文件/文件夹的文件夹的权限。您需要授予的权限是您的 Web 应用程序正在使用的应用程序池。

之后在 IIS 中右键单击您的站点并添加一个虚拟目录。您可以在此处定义文件夹的别名。您的代码会将其识别为项目中的实际文件夹。你需要在这里选择实际的物理路径。

于 2013-12-05T07:30:57.673 回答