ASP.NET 匯出 TEXT檔案

StringBuilder SBErrMag = new StringBuilder();
 SBErrMag.Append(" ");
 SBErrMag.Append(Environment.NewLine);//換行

string fileName = row.Cells[1].Text + "異常明細" + ".txt";
                    if (Request.Browser.Browser == "IE")
                    {
                        fileName = Server.UrlPathEncode(fileName);
                    }
                    string strContentDisposition = String.Format("{0}; filename=\"{1}\"", "attachment", fileName);
                    Response.AddHeader("Content-Disposition", strContentDisposition);
                    Response.Charset = "";
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.ContentType = "application/vnd.text";

                    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                    Response.Write(SBErrMag.ToString());
                    Response.End();

檢查指定路徑是否存在
不存在則新建
 if (!Directory.Exists("C:\\"))
                { Directory.CreateDirectory("C:\\"+"Name"); }

張貼留言

0 留言