close

using System.IO;

 


 //報表輸出功能(1.直接存檔(網址列需加入AutoSave=1) 2.直接開啟檔案)
    public static void ExportToExcel(System.Web.UI.Control control, string ExcelFileName)
    {
        //设置输出文件类型为excel文件。
        System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-TW", true);
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        control.RenderControl(oHtmlTextWriter);


        //自動寫入server 存檔
        string _AutoSave = System.Web.HttpContext.Current.Request.QueryString["AutoSave"];


        if (_AutoSave == "1")
        {
            SaveToExcel(oStringWriter, ExcelFileName);
        }
        else//開啟檔案
        {
            HttpContext context = HttpContext.Current;
            context.Response.Clear();
            context.Response.Buffer = true;
            context.Response.Charset = "utf-8";
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + ExcelFileName + ".xls");
            context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
            context.Response.ContentType = "application/ms-excel";
            string charset = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>";
            context.Response.Write(charset + oStringWriter.ToString().Replace("<br/>", "<br style='mso-data-placement:same-cell;'/> "));
            context.Response.End();
        }
    }
    public static void SaveToExcel(System.IO.StringWriter oStringWriter, string filename)
    {
        string CoId = System.Web.HttpContext.Current.Request.QueryString["CoId"];
        string DpId = System.Web.HttpContext.Current.Request.QueryString["DpId"];
        string charset = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>";
        string HtmlInfo = charset + oStringWriter.ToString().Trim();
        string DocFileName = filename + ".xls";
        string FilePathName = (System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\Output\\" + CoId + "\\" + DpId + "\\").Replace("/", "\\");
        if (!Directory.Exists(FilePathName)) create_physicalFolder(FilePathName);
        FilePathName = FilePathName + "\\" + DocFileName;
        FileStream Fs = new FileStream(FilePathName, FileMode.Create);
        BinaryWriter BWriter = new BinaryWriter(Fs, System.Text.Encoding.GetEncoding("UTF-8"));
        BWriter.Write(HtmlInfo);
        BWriter.Close();
        Fs.Close();
    }
    public static void create_physicalFolder(string _Loc)
    {


        string P_Web = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
        int i = P_Web.Length;
        int j = _Loc.IndexOf("\\", i - 1);
        string L = _Loc.Substring(j);
        string[] split = L.Split(new Char[] { '\\' });
        string p = P_Web;
        foreach (string s in split)
        {
            p += s + @"\";
            if (p.EndsWith(@"\\")) p = p.Substring(0, p.Length - 1);
            if (!Directory.Exists(p))
            {
                Directory.CreateDirectory(p);
            }
        }
    }

arrow
arrow
    全站熱搜

    鴨鴨仔 發表在 痞客邦 留言(1) 人氣()