Example #1
0
        protected void btn_Export_Click(object sender, EventArgs e)
        {
            txtJoinDate = tb_BeginDate.Text;
            txtOutDate  = tb_EndDate.Text;
            string currentPath = System.Web.HttpContext.Current.Server.MapPath("TravelFiles");

            if (txtJoinDate != "" || txtOutDate != "")
            {
                //string sql = "select User_Name,USER_ID from Base_UserInfo where (join_date<='" + txtOutDate + "' and (out_date is null or out_date='1900-01-01' or out_date>='" + txtJoinDate + "'))";
                //StringBuilder sb_sql = new StringBuilder(sql);
                //DataTable dt = DataFactory.SqlDataBase().GetPageList(sql, null, "User_Name", "asc", 1, 99999, ref count);
                DataTable dt           = dtExport;
                GenModel  GM           = new GenModel();
                string    fn           = txtJoinDate + "_" + txtOutDate + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "ATSReport.xls";
                string    SaveLocation = Server.MapPath("TravelFiles") + "\\" + fn;
                int       intResult    = GM.ExportExcel(dt, SaveLocation);
                //GM.rpExportExcel(ref rp_Item, fn, "application/ms-excel");
                if (intResult != -1)
                {
                    string   fileName = fn;                             //客户端保存的文件名
                    string   filePath = currentPath + "\\" + fileName;; //路径
                    FileInfo fileInfo = new FileInfo(filePath);
                    fileName = Path.GetFileName(filePath);
                    Response.Clear();
                    Response.ClearContent();
                    Response.ClearHeaders();
                    Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
                    Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                    Response.AddHeader("Content-Transfer-Encoding", "binary");
                    Response.ContentType     = "application/octet-stream";
                    Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                    Response.WriteFile(fileInfo.FullName);
                    Response.Flush();
                    Response.End();
                }
            }
        }