Basf_EProject/EProject/FineUIPro.Web/Report/ProjectCostReport.aspx.cs

342 lines
16 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 ProjectCostReport : PageBase
{
#region
/// <summary>
/// 加载页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetButtonPower();//权限设置
this.drpBU.DataTextField = "DepartName";
this.drpBU.DataValueField = "DepartId";
this.drpBU.DataSource = BLL.DepartService.GetDepartList();
this.drpBU.DataBind();
Funs.FineUIPleaseSelect(this.drpBU);
BLL.ConstService.InitConstValueDropDownList(this.drpJobType, BLL.Const.ProjectPlanner_JobType, true);
this.txtMonth.Text = string.Format("{0:yyyy-MM}", DateTime.Now);
ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
// 绑定表格
BindGrid();
}
}
/// <summary>
/// 绑定数据
/// </summary>
private void BindGrid()
{
string strSql = @"SELECT costReport.CostReportId,
costReport.EProjectId,
costReport.Monthly,
costReport.OrginalBudget,
costReport.ChangedBudget,
costReport.ActualCost,
(costReport.CommittedPRPO + costReport.CommittedSSRs) AS Commitment,
costReport.CostToComplete,
(CASE WHEN costReport.OrginalBudget > 0
then CONVERT(nvarchar(10),CONVERT(DECIMAL(18,2),(CONVERT(DECIMAL(18,2),costReport.CommittedPRPO + costReport.CommittedSSRs + costReport.ActualCost)/CONVERT(DECIMAL(18,2),costReport.OrginalBudget)*100)))+'%'
else '0.00%' end) as CostCompeted,
eProject.ProjectControl_JobNo,
eProject.ProjectControl_BUCode,
eProject.ProjectControl_JobType,
eProject.ProjectControl_JobTitle,
eProject.ProjectControl_ProjectManager,
eProject.ProjectControl_ConstManager,
eProject.ProjectControl_JobStatus,
CONVERT(NVARCHAR(10),eProject.PM_MA_ProjectApproval,23) as PM_MA_ProjectApproval"
+ @" FROM dbo.Editor_CostReport AS costReport"
+ @" LEFT JOIN dbo.Editor_EProject AS eProject ON eProject.EProjectId = costReport.EProjectId"
+ @" WHERE costReport.Monthly = @monthly ";
List<SqlParameter> listStr = new List<SqlParameter>();
listStr.Add(new SqlParameter("@monthly",this.txtMonth.Text.Trim()));
if (!string.IsNullOrEmpty(this.txtJobNo.Text.Trim()))
{
strSql += " AND eProject.ProjectControl_JobNo LIKE @JobNO ";
listStr.Add(new SqlParameter("@JobNO", this.txtJobNo.Text.Trim() + "%"));
}
if (this.drpBU.SelectedValue != BLL.Const._Null&&!string.IsNullOrEmpty(this.drpBU.SelectedValue))
{
strSql += " AND eProject.ProjectControl_BUCode=@buCode ";
listStr.Add(new SqlParameter("@buCode", this.drpBU.SelectedItem.Text));
}
if (this.drpJobType.SelectedValue != BLL.Const._Null&&!string.IsNullOrEmpty(this.drpJobType.SelectedValue))
{
strSql += " AND eProject.ProjectControl_JobType=@jobType ";
listStr.Add(new SqlParameter("@jobType", this.drpJobType.SelectedItem.Text));
}
strSql += " ORDER BY eProject.ProjectControl_JobNo ";
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 + "Project_Cost_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 Project_Cost_Report
XSSFSheet cost = (XSSFSheet)hssfworkbook.GetSheet("Project_Cost_Report");
XSSFFont cs_content_Font = (XSSFFont)hssfworkbook.CreateFont(); //创建字体
cs_content_Font.FontName = "sans-serif";//字体
cs_content_Font.FontHeightInPoints = 10; //字体大小
IDataFormat dataformat = hssfworkbook.CreateDataFormat();
ICellStyle styleQfw = hssfworkbook.CreateCellStyle();
styleQfw.VerticalAlignment = VerticalAlignment.Center;
styleQfw.Alignment = HorizontalAlignment.Right;
styleQfw.DataFormat = dataformat.GetFormat("#,##0.00");
styleQfw.SetFont(cs_content_Font);
ICellStyle styleDate = hssfworkbook.CreateCellStyle();
styleDate.DataFormat = dataformat.GetFormat("yyyy/m/d");
var costReport = (from x in Funs.DB.View_Report_ProjectCostReport orderby x.ProjectControl_JobNo descending select x).ToList();
if (!string.IsNullOrEmpty(this.txtMonth.Text.Trim()))
{
costReport = costReport.Where(x => x.Monthly == this.txtMonth.Text.Trim()).ToList();
}
if (this.drpJobType.SelectedValue!=BLL.Const._Null&&!string.IsNullOrEmpty(this.drpJobType.SelectedValue))
{
costReport = costReport.Where(x => x.ProjectControl_JobType == this.drpJobType.SelectedItem.Text.Trim()).ToList();
}
if (this.drpBU.SelectedValue!=BLL.Const._Null&&!string.IsNullOrEmpty(this.drpBU.SelectedValue))
{
costReport = costReport.Where(x => x.ProjectControl_BUCode == this.drpBU.SelectedItem.Text.Trim()).ToList();
}
if (!string.IsNullOrEmpty(txtJobNo.Text.Trim()))
{
costReport = costReport.Where(x => x.ProjectControl_JobNo.Contains(this.txtJobNo.Text.Trim())).ToList();
}
if (costReport.Count > 0)
{
if (cost.GetRow(1).GetCell(11) == null) cost.GetRow(1).CreateCell(11);
cost.GetRow(1).GetCell(11).SetCellValue(this.txtMonth.Text.Trim());
var rowIndex = 3;
foreach (var itemOver in costReport)
{
if (cost.GetRow(rowIndex) == null) cost.CreateRow(rowIndex);
#region
//BU.
if (cost.GetRow(rowIndex).GetCell(0) == null) cost.GetRow(rowIndex).CreateCell(0);
cost.GetRow(rowIndex).GetCell(0).SetCellValue(itemOver.ProjectControl_BUCode);
cost.GetRow(rowIndex).GetCell(0).CellStyle.SetFont(cs_content_Font);//将字体绑定到样式
//Job No
if (cost.GetRow(rowIndex).GetCell(1) == null) cost.GetRow(rowIndex).CreateCell(1);
cost.GetRow(rowIndex).GetCell(1).SetCellValue(itemOver.ProjectControl_JobNo);
//Type
if (cost.GetRow(rowIndex).GetCell(2) == null) cost.GetRow(rowIndex).CreateCell(2);
cost.GetRow(rowIndex).GetCell(2).SetCellValue(itemOver.ProjectControl_JobType);
//Title
if (cost.GetRow(rowIndex).GetCell(3) == null) cost.GetRow(rowIndex).CreateCell(3);
cost.GetRow(rowIndex).GetCell(3).SetCellValue(itemOver.ProjectControl_JobTitle);
//Budget
if (cost.GetRow(rowIndex).GetCell(4) == null) cost.GetRow(rowIndex).CreateCell(4);
cost.GetRow(rowIndex).GetCell(4).SetCellValue((double)itemOver.OrginalBudget);
cost.GetRow(rowIndex).GetCell(4).CellStyle = styleQfw;
//Actual
if (cost.GetRow(rowIndex).GetCell(5) == null) cost.GetRow(rowIndex).CreateCell(5);
cost.GetRow(rowIndex).GetCell(5).SetCellValue((double)itemOver.ActualCost);
cost.GetRow(rowIndex).GetCell(5).CellStyle = styleQfw;
//Commitment
if (cost.GetRow(rowIndex).GetCell(6) == null) cost.GetRow(rowIndex).CreateCell(6);
cost.GetRow(rowIndex).GetCell(6).SetCellValue(itemOver.Committeds != null ? (double)itemOver.Committeds.Value : 0);
cost.GetRow(rowIndex).GetCell(6).CellStyle = styleQfw;
//Cost Competed(%)
if (cost.GetRow(rowIndex).GetCell(7) == null) cost.GetRow(rowIndex).CreateCell(7);
cost.GetRow(rowIndex).GetCell(7).SetCellValue(itemOver.CostCompetedRate.ToString());
//PM
if (cost.GetRow(rowIndex).GetCell(8) == null) cost.GetRow(rowIndex).CreateCell(8);
cost.GetRow(rowIndex).GetCell(8).SetCellValue(itemOver.ProjectControl_ProjectManager);
//CM
if (cost.GetRow(rowIndex).GetCell(9) == null) cost.GetRow(rowIndex).CreateCell(9);
cost.GetRow(rowIndex).GetCell(9).SetCellValue(itemOver.ProjectControl_ConstManager);
//Status
if (cost.GetRow(rowIndex).GetCell(10) == null) cost.GetRow(rowIndex).CreateCell(10);
cost.GetRow(rowIndex).GetCell(10).SetCellValue(itemOver.ProjectControl_JobStatus);
//Approval Date
if (cost.GetRow(rowIndex).GetCell(11) == null) cost.GetRow(rowIndex).CreateCell(11);
if (!string.IsNullOrEmpty(itemOver.PM_MA_ProjectApproval))
{
cost.GetRow(rowIndex).GetCell(11).SetCellValue((DateTime)Convert.ToDateTime(itemOver.PM_MA_ProjectApproval));
cost.GetRow(rowIndex).GetCell(11).CellStyle = styleDate;
}
#endregion
rowIndex++;
}
}
#endregion
cost.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=P03_Project_Cost_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();
}
/// <summary>
/// 根据sql获取数据
/// </summary>
/// <param name="strSql"></param>
/// <param name="tableName"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static DataTable GetDataTableNameRunText(string strSql, string tableName = "", params SqlParameter[] parameters)
{
DataTable dataTable = string.IsNullOrEmpty(tableName) ? new DataTable() : new DataTable(tableName);
using (SqlConnection Connection = new SqlConnection(Funs.ConnString))
{
try
{
Connection.Open();
SqlCommand command = new SqlCommand(strSql, Connection);
command.CommandType = CommandType.Text;
if (parameters != null)
{
command.Parameters.AddRange(parameters);
}
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(dataTable);
}
finally
{
Connection.Close();
}
}
return dataTable;
}
#endregion
#region
/// <summary>
/// 菜单按钮权限
/// </summary>
private void GetButtonPower()
{
var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.UserId, BLL.Const.ProjectCostReportMenuId);
if (buttonList.Count() > 0)
{
if (buttonList.Contains(BLL.Const.BtnOut))
{
this.btnExport.Hidden = false;
}
}
}
#endregion
}
}