0

我已经在服务器上托管了我的应用程序。任务是将 Gridview 从 IIS 服务器上的 c# 应用程序导出到 ExcelSheet 到网络上的特定机器(不在用户的本地机器上)。这是我使用的代码

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
string renderedGridView = stringWrite.ToString();
File.WriteAllText(@"path of the server" + filename+ ".xls", renderedGridView);
Response.Write("<script>alert('File Successfully saved to server!')</script>");

当我在本地机器上运行这段代码时,它运行良好。它在指定位置创建 excel 文件。但是当我将相同的代码放在我的 IIS 服务器上并从服务器运行它时,文件没有得到出口到任何地方。

我使用的服务器路径是:

\\Machine IPaddress\machine name\Folder\SubFolder\filename.xls

我可以在运行中使用上述路径访问此位置。此路径在我的本地计算机上运行良好。当我在 IIS 服务器上托管我的应用程序时是否需要更改路径?如果是,那么应该更改什么?

我应该怎么做才能使它工作?请帮助。谢谢!

4

1 回答 1

0

试试这个代码

HttpResponse response = HttpContext.Current.Response;



            response.ContentType = "application/ms-excel";
   response.AddHeader("Content-Disposition", "inline;filename="+PathToServer +"ReportExport.xls");
   response.Write("<!DOCTYPE html PUBLIC " + Convert.ToChar(34) + "-//W3C//DTD XHTML 1.0 Transitional//EN" + Convert.ToChar(34) + " " + Convert.ToChar(34) +
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" + Convert.ToChar(34) + ">" + "\n" +
        "<html xmlns=" + Convert.ToChar(34) + "http://www.w3.org/1999/xhtml" + Convert.ToChar(34) + ">" + "\n" +
        "<head>"  + "\n"+
        "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>"  +"\n"+
        "</head><body>");


            using (StringWriter sw = new StringWriter())
                {


                sw.Write(StrHeader); // put strHeader as table string
                    using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                    {
                        GridView1.RenderControl(htw);
                        response.Write(sw.ToString());
                        response.Flush();
                        response.End(); 
                    }
                }
于 2013-10-09T12:52:52.070 回答