229 lines
		
	
	
		
			9.2 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			229 lines
		
	
	
		
			9.2 KiB
		
	
	
	
		
			C#
		
	
	
	
using BLL;
 | 
						|
using NPOI.XSSF.UserModel;
 | 
						|
using NPOI.SS.UserModel;
 | 
						|
using System;
 | 
						|
using System.Collections.Generic;
 | 
						|
using System.Data;
 | 
						|
using System.Data.SqlClient;
 | 
						|
using System.IO;
 | 
						|
using System.Linq;
 | 
						|
using System.Web.UI.WebControls;
 | 
						|
 | 
						|
namespace FineUIPro.Web.Report
 | 
						|
{
 | 
						|
    public partial class HTDReport : PageBase
 | 
						|
    {
 | 
						|
        #region 加载
 | 
						|
        /// <summary>
 | 
						|
        /// 加载页面
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="sender"></param>
 | 
						|
        /// <param name="e"></param>
 | 
						|
        protected void Page_Load(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            if (!IsPostBack)
 | 
						|
            {
 | 
						|
                GetButtonPower();//权限设置
 | 
						|
                ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
 | 
						|
                // 绑定表格
 | 
						|
                BindGrid();
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 绑定数据
 | 
						|
        /// </summary>
 | 
						|
        private void BindGrid()
 | 
						|
        {
 | 
						|
            string strSql = @"SELECT htd.HTDID,htd.Discipline,htd.ReceiveDate,htd.[Status],pro.ProjectControl_JobNo,con.ContractorName 
 | 
						|
                              FROM dbo.Editor_CM_HTD htd
 | 
						|
                              LEFT JOIN dbo.Editor_EProject pro ON pro.EProjectId = htd.EProjectId
 | 
						|
                              LEFT JOIN dbo.Base_Contractor con ON con.ContractorId = htd.ContractorId                      
 | 
						|
                              WHERE 1=1 ";
 | 
						|
            List<SqlParameter> listStr = new List<SqlParameter>();
 | 
						|
            if (!string.IsNullOrEmpty(this.txtJobNo.Text.Trim()))
 | 
						|
            {
 | 
						|
                strSql += " AND pro.ProjectControl_JobNo LIKE @JobNO ";
 | 
						|
                listStr.Add(new SqlParameter("@JobNO", this.txtJobNo.Text.Trim() + "%"));
 | 
						|
            }
 | 
						|
 | 
						|
            strSql += " ORDER BY pro.ProjectControl_JobNo DESC";
 | 
						|
            SqlParameter[] parameter = listStr.ToArray();
 | 
						|
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
 | 
						|
            // 2.获取当前分页数据
 | 
						|
            //var table = this.GetPagedDataTable(Grid1, tb1);
 | 
						|
            Grid1.RecordCount = tb.Rows.Count;
 | 
						|
            var table = this.GetPagedDataTable(Grid1, tb);
 | 
						|
            Grid1.DataSource = table;
 | 
						|
            Grid1.DataBind();
 | 
						|
        }
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region 查询
 | 
						|
        /// <summary>
 | 
						|
        /// 下拉框选择事件
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="sender"></param>
 | 
						|
        /// <param name="e"></param>
 | 
						|
        protected void btnSearch_Click(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            BindGrid();
 | 
						|
        }
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region 分页、排序
 | 
						|
        /// <summary>
 | 
						|
        /// 分页
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="sender"></param>
 | 
						|
        /// <param name="e"></param>
 | 
						|
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
 | 
						|
        {
 | 
						|
            Grid1.PageIndex = e.NewPageIndex;
 | 
						|
            BindGrid();
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 分页显示条数下拉框
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="sender"></param>
 | 
						|
        /// <param name="e"></param>
 | 
						|
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
 | 
						|
            BindGrid();
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 排序
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="sender"></param>
 | 
						|
        /// <param name="e"></param>
 | 
						|
        protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
 | 
						|
        {
 | 
						|
            Grid1.SortDirection = e.SortDirection;
 | 
						|
            Grid1.SortField = e.SortField;
 | 
						|
            BindGrid();
 | 
						|
        }
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region 导出
 | 
						|
        /// <summary>
 | 
						|
        /// 导出按钮
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="sender"></param>
 | 
						|
        /// <param name="e"></param>
 | 
						|
        protected void btnExport_Click(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            string rootPath = Server.MapPath("~/") + Const.ExcelUrl;
 | 
						|
            //模板文件
 | 
						|
            string TempletFileName = rootPath + "HTD_Report.xlsx";
 | 
						|
            //导出文件
 | 
						|
            string filePath = rootPath + DateTime.Now.ToString("yyyyMMddhhmmss") + "\\";
 | 
						|
            if (!Directory.Exists(filePath))
 | 
						|
            {
 | 
						|
                Directory.CreateDirectory(filePath);
 | 
						|
            }
 | 
						|
            string ReportFileName = filePath + "out.xlsx";
 | 
						|
 | 
						|
            FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read);
 | 
						|
            XSSFWorkbook hssfworkbook = new XSSFWorkbook(file);
 | 
						|
 | 
						|
            #region FCR_Report
 | 
						|
            XSSFSheet reportModel = (XSSFSheet)hssfworkbook.GetSheet("HTD_Report");
 | 
						|
 | 
						|
            XSSFFont cs_content_Font = (XSSFFont)hssfworkbook.CreateFont(); //创建字体
 | 
						|
            cs_content_Font.FontName = "sans-serif";//字体
 | 
						|
            cs_content_Font.FontHeightInPoints = 10; //字体大小
 | 
						|
 | 
						|
            IDataFormat dataformat = hssfworkbook.CreateDataFormat();
 | 
						|
            ICellStyle styleDate = hssfworkbook.CreateCellStyle();
 | 
						|
            styleDate.DataFormat = dataformat.GetFormat("yyyy/m/d");
 | 
						|
            styleDate.SetFont(cs_content_Font);
 | 
						|
 | 
						|
            var list = (from x in Funs.DB.Editor_CM_HTD
 | 
						|
                        join y in Funs.DB.Editor_EProject on x.EProjectId equals y.EProjectId
 | 
						|
                        join z in Funs.DB.Base_Contractor on x.ContractorId equals z.ContractorId
 | 
						|
                        orderby y.ProjectControl_JobNo descending
 | 
						|
                        select new
 | 
						|
                        { x.HTDID, y.ProjectControl_JobNo, z.ContractorName, x.Discipline, x.ReceiveDate, x.Status }).ToList();
 | 
						|
            if (!string.IsNullOrEmpty(this.txtJobNo.Text.Trim()))
 | 
						|
            {
 | 
						|
                list = list.Where(x => x.ProjectControl_JobNo.Contains(this.txtJobNo.Text.Trim())).ToList();
 | 
						|
            }
 | 
						|
            if (list.Count > 0)
 | 
						|
            {
 | 
						|
                var rowIndex = 1;
 | 
						|
                foreach (var itemOver in list)
 | 
						|
                {
 | 
						|
                    if (reportModel.GetRow(rowIndex) == null) reportModel.CreateRow(rowIndex);
 | 
						|
 | 
						|
                    #region 列赋值
 | 
						|
                    //Job No.
 | 
						|
                    if (reportModel.GetRow(rowIndex).GetCell(0) == null) reportModel.GetRow(rowIndex).CreateCell(0);
 | 
						|
                    reportModel.GetRow(rowIndex).GetCell(0).SetCellValue(itemOver.ProjectControl_JobNo);
 | 
						|
                    reportModel.GetRow(rowIndex).GetCell(0).CellStyle.SetFont(cs_content_Font);//将字体绑定到样式
 | 
						|
                    //Contractor
 | 
						|
                    if (reportModel.GetRow(rowIndex).GetCell(1) == null) reportModel.GetRow(rowIndex).CreateCell(1);
 | 
						|
                    reportModel.GetRow(rowIndex).GetCell(1).SetCellValue(itemOver.ContractorName);
 | 
						|
                    // Discipline
 | 
						|
                    if (reportModel.GetRow(rowIndex).GetCell(2) == null) reportModel.GetRow(rowIndex).CreateCell(2);
 | 
						|
                    reportModel.GetRow(rowIndex).GetCell(2).SetCellValue(itemOver.Discipline);
 | 
						|
                    // Receive Date 
 | 
						|
                    if (reportModel.GetRow(rowIndex).GetCell(3) == null) reportModel.GetRow(rowIndex).CreateCell(3);
 | 
						|
                    if (itemOver.ReceiveDate.HasValue)
 | 
						|
                    {
 | 
						|
                        reportModel.GetRow(rowIndex).GetCell(3).SetCellValue((DateTime)itemOver.ReceiveDate.Value);
 | 
						|
                    }
 | 
						|
                    reportModel.GetRow(rowIndex).GetCell(3).CellStyle = styleDate;
 | 
						|
 
 | 
						|
                    // Status
 | 
						|
                    if (reportModel.GetRow(rowIndex).GetCell(4) == null) reportModel.GetRow(rowIndex).CreateCell(4);
 | 
						|
                    reportModel.GetRow(rowIndex).GetCell(4).SetCellValue(itemOver.Status);
 | 
						|
                    
 | 
						|
                    #endregion
 | 
						|
                    rowIndex++;
 | 
						|
                }
 | 
						|
            }
 | 
						|
            #endregion
 | 
						|
            reportModel.ForceFormulaRecalculation = true;
 | 
						|
 | 
						|
            using (FileStream filess = File.OpenWrite(ReportFileName))
 | 
						|
            {
 | 
						|
                hssfworkbook.Write(filess);
 | 
						|
            }
 | 
						|
            FileInfo filet = new FileInfo(ReportFileName);
 | 
						|
            Response.Clear();
 | 
						|
            Response.Charset = "GB2312";
 | 
						|
            Response.ContentEncoding = System.Text.Encoding.UTF8;
 | 
						|
            // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
 | 
						|
            Response.AddHeader("Content-Disposition", "attachment; filename=HTD_Report_" + Server.UrlEncode(DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx"));
 | 
						|
            // 添加头信息,指定文件大小,让浏览器能够显示下载进度
 | 
						|
            Response.AddHeader("Content-Length", filet.Length.ToString());
 | 
						|
            // 指定返回的是一个不能被客户端读取的流,必须被下载
 | 
						|
            Response.ContentType = "application/ms-excel";
 | 
						|
            // 把文件流发送到客户端
 | 
						|
            Response.WriteFile(filet.FullName);
 | 
						|
            // 停止页面的执行
 | 
						|
            Response.End();
 | 
						|
        }
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region 权限设置
 | 
						|
        /// <summary>
 | 
						|
        /// 菜单按钮权限
 | 
						|
        /// </summary>
 | 
						|
        private void GetButtonPower()
 | 
						|
        {
 | 
						|
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.UserId, BLL.Const.CMHTDMenuId);
 | 
						|
            if (buttonList.Count() > 0)
 | 
						|
            {
 | 
						|
                if (buttonList.Contains(BLL.Const.BtnOut))
 | 
						|
                {
 | 
						|
                    this.btnExport.Hidden = false;
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
        #endregion
 | 
						|
    }
 | 
						|
} |