コード例 #1
0
    protected void Button3_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();// strWhere = "1=1";

        sb.Append("1=1");
        string browser = Request.Browser.Browser;

        if (!string.IsNullOrEmpty(Request["code"]))
        {
            sb.AppendFormat(" and CWB_Code like '%{0}%'", Request["code"].ToString().Trim());//;;//存 客户名称
        }
        if (!string.IsNullOrEmpty(Request["cname"]))
        {
            sb.AppendFormat(" and b.CC_Name like '%{0}%'", Request["cname"].ToString().Trim());//存 客户名称
        }
        if (!string.IsNullOrEmpty(Request["uname"]))
        {
            sb.AppendFormat(" and d.UserName like '%{0}%'", Request["uname"].ToString().Trim());//存 用户名称
        }
        if (!string.IsNullOrEmpty(Request["stime"]))
        {
            DateTime dtStime = Convert.ToDateTime("1980-01-01");
            DateTime.TryParse(Request["stime"].ToString().Trim(), out dtStime);
            sb.AppendFormat(" and CWB_CreateTime > '{0}'", dtStime.AddDays(-1));//保存操作开始时间
        }
        if (!string.IsNullOrEmpty(Request["etime"]))
        {
            DateTime dtEtime = Convert.ToDateTime("2250-01-01");
            DateTime.TryParse(Request["etime"].ToString().Trim(), out dtEtime);
            sb.AppendFormat(" and CWB_CreateTime < '{0}'", dtEtime.AddDays(1)); //保存操作结束时间
        }
        if (!string.IsNullOrEmpty(Request["Owner"]))                            //存 客户所属工程师
        {
            sb.AppendFormat(" and CWB_Description = '{0}'", Request["Owner"].ToString().Trim());
        }
        if (!string.IsNullOrEmpty(Request["sltType"]) && Request["sltType"].ToString().Trim() != "0")
        {
            sb.AppendFormat(" and CWB_Type = '{0}'", Convert.ToInt16(Request["sltType"].ToString().Trim()));
        }
        if (!string.IsNullOrEmpty(Request["status"]))
        {
            int intStatus = 0;
            int.TryParse(Request["status"].ToString().Trim(), out intStatus);
            int intStatus1 = 27;
            int.TryParse(ConfigurationManager.AppSettings["workbillStatusAll"].ToString(), out intStatus1);
            if (intStatus1 == intStatus)
            {
            }
            else
            {
                sb.AppendFormat(" and CWB_Status = '{0}'", intStatus);
            }
        }

        DataTable dt = new DataTable();

        HYTD.BLL.Call_WorkBillBLL bll = new HYTD.BLL.Call_WorkBillBLL();
        dt = bll.ImportExcelCustomerWorkBill(sb.ToString()).Tables[0];
        NPOI.SS.UserModel.IWorkbook book1 = new ImportExcel().ImportCustomerWorkBill(dt, "客户服务记录");
        string strFile  = string.Empty;
        string filename = string.Empty;
        string strPath  = HttpContext.Current.Server.MapPath("/" + System.Configuration.ConfigurationManager.AppSettings["CustomerFiles"].ToString().Replace("\\", "/") + "/")
                          + "\\" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2, '0') + "\\";

        if (dt.Rows.Count > 0)
        {
            strFile  = dt.Rows[0]["客户名称"].ToString();
            filename = strPath + dt.Rows[0]["客户名称"].ToString() + ".xls";
            if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(filename)))
            {
                System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filename));
            }
            try
            {
                System.IO.FileStream file1 = new System.IO.FileStream(filename, System.IO.FileMode.Create);
                book1.Write(file1);

                file1.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        Response.Clear();
        Response.BufferOutput    = false;
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        if (browser.ToLower() != "firefox")
        {
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFile) + ".xls");
        }
        else
        {
            Response.AddHeader("Content-Disposition", "attachment;filename=" + strFile + ".xls");
        }
        Response.ContentType = "application/ms-excel";
        book1.Write(Response.OutputStream);
        book1.Dispose();
    }