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

355 lines
17 KiB
C#

using BLL;
using Newtonsoft.Json.Linq;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
namespace FineUIPro.Web.Report
{
public partial class TimeSheetSummary : PageBase
{
#region
/// <summary>
/// 加载页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetButtonPower();//权限设置
BLL.DepartService.InitCTEDepartDropDownList(this.drpWorkCenter, false);
ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
// 绑定表格
BindGrid();
}
}
/// <summary>
/// 绑定数据
/// </summary>
private void BindGrid()
{
List<SqlParameter> listStr = new List<SqlParameter>();
if (!string.IsNullOrEmpty(this.txtMonth.Text.Trim()))
{
listStr.Add(new SqlParameter("@YearMonths", this.txtMonth.Text.Trim()));
}
if (this.drpWorkCenter.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpWorkCenter.SelectedItem.Text.Trim()) && this.drpWorkCenter.SelectedItem.Text != "CTE")
{
listStr.Add(new SqlParameter("@WorkCenter", this.drpWorkCenter.SelectedItem.Text.Trim()));
}
SqlParameter[] parameter = listStr.ToArray();
DataTable tb = SQLHelper.GetDataTableRunProc("Proc_TimeSheetSummary", parameter);
Grid1.RecordCount = tb.Rows.Count;
var table = this.GetPagedDataTable(Grid1, tb);
Grid1.DataSource = table;
Grid1.DataBind();
OutputSummaryData(table);//计算和
}
#endregion
#region
private void OutputSummaryData(DataTable tb)
{
float TargetHours = 0.0f;
float GrandHours = 0.0f;
float NormalWorkHours = 0.0f;
float OverTimeHours = 0.0f;
float TripHours = 0.0f;
float TrainingHours = 0.0f;
float LeaveHours = 0.0f;
float sickLeaveHours = 0.0f;
float OtherHours = 0.0f;
for (int i = 0; i < tb.Rows.Count; i++)
{
TargetHours += Convert.ToInt32(tb.Rows[i]["TargetHours"]);
GrandHours += Convert.ToInt32(tb.Rows[i]["GrandHours"]);
NormalWorkHours += Convert.ToInt32(tb.Rows[i]["NormalWorkHours"]);
OverTimeHours += Convert.ToInt32(tb.Rows[i]["OverTimeHours"]);
TripHours += Convert.ToInt32(tb.Rows[i]["TripHours"]);
TrainingHours += Convert.ToInt32(tb.Rows[i]["TrainingHours"]);
LeaveHours += Convert.ToInt32(tb.Rows[i]["LeaveHours"]);
sickLeaveHours += Convert.ToInt32(tb.Rows[i]["SickLeaveHours"]);
OtherHours += Convert.ToInt32(tb.Rows[i]["OtherHours"]);
}
JObject summary = new JObject();
//summary.Add("major", "全部合计");
summary.Add("TargetHours", TargetHours.ToString("F2"));
summary.Add("GrandHours", GrandHours.ToString("F2"));
summary.Add("NormalWorkHours", NormalWorkHours.ToString("F2"));
summary.Add("OverTimeHours", OverTimeHours.ToString("F2"));
summary.Add("TripHours", TripHours.ToString("F2"));
summary.Add("TrainingHours", TrainingHours.ToString("F2"));
summary.Add("LeaveHours", LeaveHours.ToString("F2"));
summary.Add("SickLeaveHours", sickLeaveHours.ToString("F2"));
summary.Add("OtherHours", OtherHours.ToString("F2"));
Grid1.SummaryData = summary;
}
#endregion
#region
/// <summary>
/// 下拉框选择事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
BindGrid();
}
protected void txtMonth_TextChanged(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 + "Time_Sheet_Summary.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 Time_Sheet_Summary
XSSFSheet reportModel = (XSSFSheet)hssfworkbook.GetSheet("Time_Sheet_Summary");
XSSFFont cs_content_Font = (XSSFFont)hssfworkbook.CreateFont(); //创建字体
cs_content_Font.FontName = "sans-serif";//字体
cs_content_Font.FontHeightInPoints = 10; //字体大小
List<SqlParameter> listStr = new List<SqlParameter>();
if (!string.IsNullOrEmpty(txtMonth.Text.Trim()))
{
listStr.Add(new SqlParameter("@YearMonths", txtMonth.Text.Trim()));
}
if (this.drpWorkCenter.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpWorkCenter.SelectedValue) && this.drpWorkCenter.SelectedItem.Text != "CTE")
{
listStr.Add(new SqlParameter("@WorkCenter", drpWorkCenter.SelectedItem.Text.Trim()));
}
SqlParameter[] parameter = listStr.ToArray();
DataTable table = SQLHelper.GetDataTableRunProc("Proc_TimeSheetSummary", parameter);
if (table.Rows.Count > 0)
{
var rowIndex = 1;
double targetHours = 0;
double grandHours = 0;
double normalWorkHours = 0;
double overTimeHours = 0;
double tripHours = 0;
double trainingHours = 0;
double leaveHours = 0;
double sickLeaveHours = 0;
double otherHours = 0;
for (int i = 0; i < table.Rows.Count; i++)
{
if (reportModel.GetRow(rowIndex) == null) reportModel.CreateRow(rowIndex);
#region
//Month
if (reportModel.GetRow(rowIndex).GetCell(0) == null) reportModel.GetRow(rowIndex).CreateCell(0);
reportModel.GetRow(rowIndex).GetCell(0).SetCellValue(table.Rows[i]["ManHoursDate"].ToString());
reportModel.GetRow(rowIndex).GetCell(0).CellStyle.SetFont(cs_content_Font);//将字体绑定到样式
//WorkCenter
if (reportModel.GetRow(rowIndex).GetCell(1) == null) reportModel.GetRow(rowIndex).CreateCell(1);
reportModel.GetRow(rowIndex).GetCell(1).SetCellValue(table.Rows[i]["WorkCenter"].ToString());
//Name
if (reportModel.GetRow(rowIndex).GetCell(2) == null) reportModel.GetRow(rowIndex).CreateCell(2);
reportModel.GetRow(rowIndex).GetCell(2).SetCellValue(table.Rows[i]["UserName"].ToString());
//Target Hours
if (reportModel.GetRow(rowIndex).GetCell(3) == null) reportModel.GetRow(rowIndex).CreateCell(3);
reportModel.GetRow(rowIndex).GetCell(3).SetCellValue(table.Rows[i]["TargetHours"] != null ? table.Rows[i]["TargetHours"].ToString() : "");
//Grand Hours
if (reportModel.GetRow(rowIndex).GetCell(4) == null) reportModel.GetRow(rowIndex).CreateCell(4);
reportModel.GetRow(rowIndex).GetCell(4).SetCellValue(Convert.ToDouble(table.Rows[i]["GrandHours"]));
//Normal Work Hours
if (reportModel.GetRow(rowIndex).GetCell(5) == null) reportModel.GetRow(rowIndex).CreateCell(5);
reportModel.GetRow(rowIndex).GetCell(5).SetCellValue(Convert.ToDouble(table.Rows[i]["NormalWorkHours"]));
//OverTime Hours
if (reportModel.GetRow(rowIndex).GetCell(6) == null) reportModel.GetRow(rowIndex).CreateCell(6);
reportModel.GetRow(rowIndex).GetCell(6).SetCellValue(Convert.ToDouble(table.Rows[i]["OverTimeHours"]));
//Trip Hours
if (reportModel.GetRow(rowIndex).GetCell(7) == null) reportModel.GetRow(rowIndex).CreateCell(7);
reportModel.GetRow(rowIndex).GetCell(7).SetCellValue(Convert.ToDouble(table.Rows[i]["TripHours"]));
//Training Hours
if (reportModel.GetRow(rowIndex).GetCell(8) == null) reportModel.GetRow(rowIndex).CreateCell(8);
reportModel.GetRow(rowIndex).GetCell(8).SetCellValue(Convert.ToDouble(table.Rows[i]["TrainingHours"]));
//Leave Hours
if (reportModel.GetRow(rowIndex).GetCell(9) == null) reportModel.GetRow(rowIndex).CreateCell(9);
reportModel.GetRow(rowIndex).GetCell(9).SetCellValue(Convert.ToDouble(table.Rows[i]["LeaveHours"]));
//Sick Leave Hours
if (reportModel.GetRow(rowIndex).GetCell(10) == null) reportModel.GetRow(rowIndex).CreateCell(10);
reportModel.GetRow(rowIndex).GetCell(10).SetCellValue(Convert.ToDouble(table.Rows[i]["SickLeaveHours"]));
//Other Hours
if (reportModel.GetRow(rowIndex).GetCell(11) == null) reportModel.GetRow(rowIndex).CreateCell(11);
reportModel.GetRow(rowIndex).GetCell(11).SetCellValue(Convert.ToDouble(table.Rows[i]["OtherHours"]));
#endregion
#region
if (!string.IsNullOrEmpty(table.Rows[i]["TargetHours"].ToString()))
{
targetHours += Convert.ToDouble(table.Rows[i]["TargetHours"].ToString());
}
if (!string.IsNullOrEmpty(table.Rows[i]["GrandHours"].ToString()))
{
grandHours += Convert.ToDouble(table.Rows[i]["GrandHours"].ToString());
}
if (!string.IsNullOrEmpty(table.Rows[i]["NormalWorkHours"].ToString()))
{
normalWorkHours += Convert.ToDouble(table.Rows[i]["NormalWorkHours"].ToString());
}
if (!string.IsNullOrEmpty(table.Rows[i]["OverTimeHours"].ToString()))
{
overTimeHours += Convert.ToDouble(table.Rows[i]["OverTimeHours"].ToString());
}
if (!string.IsNullOrEmpty(table.Rows[i]["TripHours"].ToString()))
{
tripHours += Convert.ToDouble(table.Rows[i]["TripHours"].ToString());
}
if (!string.IsNullOrEmpty(table.Rows[i]["TrainingHours"].ToString()))
{
trainingHours += Convert.ToDouble(table.Rows[i]["TrainingHours"].ToString());
}
if (!string.IsNullOrEmpty(table.Rows[i]["LeaveHours"].ToString()))
{
leaveHours += Convert.ToDouble(table.Rows[i]["LeaveHours"].ToString());
}
if (!string.IsNullOrEmpty(table.Rows[i]["SickLeaveHours"].ToString()))
{
sickLeaveHours += Convert.ToDouble(table.Rows[i]["SickLeaveHours"].ToString());
}
if (!string.IsNullOrEmpty(table.Rows[i]["OtherHours"].ToString()))
{
otherHours += Convert.ToDouble(table.Rows[i]["OtherHours"].ToString());
}
#endregion
rowIndex++;
}
#region
if (reportModel.GetRow(rowIndex) == null) reportModel.CreateRow(rowIndex);
if (reportModel.GetRow(rowIndex).GetCell(3) == null) reportModel.GetRow(rowIndex).CreateCell(3);
reportModel.GetRow(rowIndex).GetCell(3).SetCellValue(targetHours);
if (reportModel.GetRow(rowIndex).GetCell(4) == null) reportModel.GetRow(rowIndex).CreateCell(4);
reportModel.GetRow(rowIndex).GetCell(4).SetCellValue(grandHours);
if (reportModel.GetRow(rowIndex).GetCell(5) == null) reportModel.GetRow(rowIndex).CreateCell(5);
reportModel.GetRow(rowIndex).GetCell(5).SetCellValue(normalWorkHours);
if (reportModel.GetRow(rowIndex).GetCell(6) == null) reportModel.GetRow(rowIndex).CreateCell(6);
reportModel.GetRow(rowIndex).GetCell(6).SetCellValue(overTimeHours);
if (reportModel.GetRow(rowIndex).GetCell(7) == null) reportModel.GetRow(rowIndex).CreateCell(7);
reportModel.GetRow(rowIndex).GetCell(7).SetCellValue(tripHours);
if (reportModel.GetRow(rowIndex).GetCell(8) == null) reportModel.GetRow(rowIndex).CreateCell(8);
reportModel.GetRow(rowIndex).GetCell(8).SetCellValue(trainingHours);
if (reportModel.GetRow(rowIndex).GetCell(9) == null) reportModel.GetRow(rowIndex).CreateCell(9);
reportModel.GetRow(rowIndex).GetCell(9).SetCellValue(leaveHours);
if (reportModel.GetRow(rowIndex).GetCell(10) == null) reportModel.GetRow(rowIndex).CreateCell(10);
reportModel.GetRow(rowIndex).GetCell(10).SetCellValue(sickLeaveHours);
if (reportModel.GetRow(rowIndex).GetCell(11) == null) reportModel.GetRow(rowIndex).CreateCell(11);
reportModel.GetRow(rowIndex).GetCell(11).SetCellValue(otherHours);
#endregion
}
#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=P12_Time_Sheet_Summary_" + 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.TimeSheetSummaryMenuId);
if (buttonList.Count() > 0)
{
if (buttonList.Contains(BLL.Const.BtnOut))
{
this.btnExport.Hidden = false;
}
}
}
#endregion
}
}