Basf_FCL/FCL/FineUIPro.Web/Evaluation/ExportDetails.aspx.cs

384 lines
16 KiB
C#
Raw Normal View History

2024-05-08 10:17:02 +08:00
using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Newtonsoft.Json;
namespace FineUIPro.Web.Evaluation
{
public partial class ExportDetails : PageBase
{
#region
/// <summary>
/// 加载页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetButtonPower();//权限设置
StartTime.Text = DateTime.Now.AddMonths(-1).ToString(StartTime.DateFormatString);
EndTime.Text = DateTime.Now.ToString(EndTime.DateFormatString);
ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
// 绑定表格
BindGrid();
}
}
/// <summary>
/// 绑定数据
/// </summary>
private void BindGrid()
{
string strSql = @"SELECT * from View_FC_SESRelatedData WHERE 1=1 ";
List<SqlParameter> listStr = new List<SqlParameter>();
if (!string.IsNullOrEmpty(this.txtFo.Text.Trim()))
{
strSql += " AND FO_NO LIKE @foNo";
listStr.Add(new SqlParameter("@foNo", "%" + this.txtFo.Text.Trim() + "%"));
}
SqlParameter[] parameter = listStr.ToArray();
DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
Grid1.RecordCount = tb.Rows.Count;
tb = GetFilteredTable(Grid1.FilteredData, tb);
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 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 btnPrint_Click(object sender, EventArgs e)
{
if (Grid1.SelectedRowIndexArray.Length == 0)
{
Alert.ShowInParent("Please select at least one record!");
return;
}
string Id = Grid1.SelectedRowID;
var fc = BLL.SESRelatedDataService.GetSESRelatedDataById(Id);
string sData = StartTime.Text;
string eData = EndTime.Text;
PageContext.RegisterStartupScript(Window2.GetShowReference(String.Format("../Report/ReportPrint.aspx?report=4&fcid=" + fc.FO_NO + "&sData=" + sData + "&eData=" + eData + "", "Print - ")));
}
#endregion
#region
/// <summary>
/// 导出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnExport_Click(object sender, EventArgs e)
{
DateTime startTime = DateTime.Parse(StartTime.Text + "-01");
DateTime endTime = DateTime.Parse(EndTime.Text + "-13");
int monthNum = (endTime.Year - startTime.Year) * 12 + (endTime.Month - startTime.Month) + 1;
if (monthNum < 0)
{
Alert.ShowInParent("The start date cannot be greater than the end date!");
return;
}
string rootPath = Server.MapPath("~/") + Const.ExcelUrl;
//模板文件
string TempletFileName = rootPath + "MonthScoreReport.xlsx";
//导出文件
string filePath = rootPath + DateTime.Now.ToString("yyyyMMddhhmmss") + "\\";
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
string ReportFileName = filePath + "out.xls";
FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read);
XSSFWorkbook hssfworkbook = new XSSFWorkbook(file);
var sesRelatedData = Funs.DB.View_FC_SESRelatedData.Where(x => x.FC_Status != "Closed").ToList();
List<SqlParameter> Param = new List<SqlParameter>();
Param.Add(new SqlParameter("@StartTime", startTime));
Param.Add(new SqlParameter("@EndTime", endTime));
SqlParameter[] ParList = Param.ToArray();
var sesDataTable = SQLHelper.GetDataTableRunProc("FN_YearToDateReport", ParList);
var sesDataScore = JsonConvert.DeserializeObject<List<YearToDateModel>>(JsonConvert.SerializeObject(sesDataTable));
XSSFSheet wsMonth = (XSSFSheet)hssfworkbook.GetSheet("Sheet1");
#region
// 字体样式
IFont font = hssfworkbook.CreateFont();
font.FontHeightInPoints = 11;
font.IsBold = false;
font.FontName = "Arial";
ICellStyle fontStyle = hssfworkbook.CreateCellStyle();
fontStyle.SetFont(font);
ICellStyle yearStyle = hssfworkbook.CreateCellStyle();
yearStyle.VerticalAlignment = VerticalAlignment.Center;
yearStyle.Alignment = HorizontalAlignment.Center;
yearStyle.SetFont(font);
//创建单元格样式
XSSFCellStyle backgroundstyle = (XSSFCellStyle)hssfworkbook.CreateCellStyle();
//填充模式
backgroundstyle.FillPattern = FillPattern.SolidForeground;
//创建颜色
XSSFColor xssfcolor = new XSSFColor();
//rbg值
byte[] rgb = { (byte)255, (byte)192, (byte)203 };
//写入rgb 粉色背景颜色定义
xssfcolor.SetRgb(rgb);
//设置颜色值
backgroundstyle.SetFillForegroundColor(xssfcolor);
backgroundstyle.SetFont(font);
#endregion
if (sesRelatedData.Count > 0)
{
var colIndex = 0;
var rowIndex = 1;
foreach (var item in sesRelatedData)
{
if (wsMonth.GetRow(rowIndex) == null) wsMonth.CreateRow(rowIndex);
#region
if (wsMonth.GetRow(rowIndex).GetCell(0) == null) wsMonth.GetRow(rowIndex).CreateCell(0);
wsMonth.GetRow(rowIndex).GetCell(0).SetCellValue(item.FO_NO);
wsMonth.GetRow(rowIndex).GetCell(0).CellStyle = fontStyle;
if (wsMonth.GetRow(rowIndex).GetCell(1) == null) wsMonth.GetRow(rowIndex).CreateCell(1);
wsMonth.GetRow(rowIndex).GetCell(1).SetCellValue(item.Discipline);
wsMonth.GetRow(rowIndex).GetCell(1).CellStyle = fontStyle;
if (wsMonth.GetRow(rowIndex).GetCell(2) == null) wsMonth.GetRow(rowIndex).CreateCell(2);
wsMonth.GetRow(rowIndex).GetCell(2).SetCellValue(item.Contractor);
wsMonth.GetRow(rowIndex).GetCell(2).CellStyle = fontStyle;
if (wsMonth.GetRow(rowIndex).GetCell(3) == null) wsMonth.GetRow(rowIndex).CreateCell(3);
wsMonth.GetRow(rowIndex).GetCell(3).SetCellValue(item.Contract_Admin);
wsMonth.GetRow(rowIndex).GetCell(3).CellStyle = fontStyle;
if (wsMonth.GetRow(rowIndex).GetCell(4) == null) wsMonth.GetRow(rowIndex).CreateCell(4);
wsMonth.GetRow(rowIndex).GetCell(4).SetCellValue(item.Main_Coordinator);
wsMonth.GetRow(rowIndex).GetCell(4).CellStyle = fontStyle;
var sesFoDataScore = sesDataScore.Where(p => p.FO_NO == item.FO_NO).ToList();
for (int j = 0; j < monthNum; j++)
{
var startData = startTime.AddMonths(j);
if (colIndex == 0)
{
if (wsMonth.GetRow(0) == null) wsMonth.CreateRow(0);
if (wsMonth.GetRow(0).GetCell(j + 5) == null) wsMonth.GetRow(0).CreateCell(j + 5);
wsMonth.GetRow(0).GetCell(j + 5).SetCellValue(startData.ToString("yyyy-MM"));
wsMonth.GetRow(0).GetCell(j + 5).CellStyle = yearStyle;
}
if (wsMonth.GetRow(rowIndex).GetCell(j + 5) == null) wsMonth.GetRow(rowIndex).CreateCell(j + 5);
if (sesFoDataScore.Count(p => p.dataMonth == startData.ToString("yyyy-MM")) > 0 && Funs.GetNewDecimal(sesFoDataScore.FirstOrDefault(p => p.dataMonth == startData.ToString("yyyy-MM")).SumScore) > 0)
{
var yearNums = float.Parse(sesFoDataScore.FirstOrDefault(p => p.dataMonth == startData.ToString("yyyy-MM")).SumScore).ToString("0.##");
wsMonth.GetRow(rowIndex).GetCell(j + 5).SetCellValue(double.Parse(yearNums));
//如果小于60分填充背景色为粉色
if (double.Parse(yearNums) > 0 && double.Parse(yearNums) < 60)
{
wsMonth.GetRow(rowIndex).GetCell(j + 5).CellStyle = backgroundstyle;
}
else
{
wsMonth.GetRow(rowIndex).GetCell(j + 5).CellStyle = fontStyle;
}
}
else
{
wsMonth.GetRow(rowIndex).GetCell(j + 5).SetCellValue("-");
wsMonth.GetRow(rowIndex).GetCell(j + 5).CellStyle = fontStyle;
}
}
#endregion
colIndex++;
rowIndex++;
}
}
wsMonth.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=Month_TotalScore_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.ExportDetailsMenuId);
if (buttonList.Count() > 0)
{
if (buttonList.Contains(BLL.Const.BtnOut))
{
this.btnPrint.Hidden = false;
}
}
}
#endregion
#region
/// <summary>
/// 行绑定后事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
{
DataRowView row = e.DataItem as DataRowView;
string id = row["ID"].ToString();
//for (int i = 0; i < this.Grid1.Rows.Count; i++)
//{
if (!string.IsNullOrEmpty(id))
{
var data = BLL.SESRelatedDataService.GetSESRelatedDataById(id);
if (data != null)
{
if (data.Expire_Date.HasValue)
{
if (data.Expire_Date < DateTime.Now)//过期
{
e.RowCssClass = "color2";
}
else if (data.Expire_Date.Value.AddMonths(-6) <= DateTime.Now && data.Expire_Date > DateTime.Now)//六个月后过期
{
e.RowCssClass = "color1";
}
}
//Remaining Budget/Contract Budget<10 显示红色
var checkedValue = BLL.SESReportService.getSumSSRActualCostByFo(data.FO_NO);
var remainmingBudget = data.Actual_Budget - checkedValue;
if (data.Actual_Budget > 0)
{
var remainmingBudgetRate = remainmingBudget / data.Actual_Budget * 100;
if (remainmingBudgetRate < 10)
{
e.CellCssClasses[30] = "colorRed";
}
}
//如果(合同过期日-今天)/(合同生效总天数)<10显示红色
if (data.Validate_Date.HasValue && data.Expire_Date.HasValue)
{
decimal a = (data.Expire_Date - DateTime.Now).Value.Days;
decimal b = (data.Expire_Date - data.Validate_Date).Value.Days;//合同生效总天数
if (b > 0)
{
decimal c = a / b * 100;
if (c < 10)
{
e.CellCssClasses[31] = "colorRed";
}
}
}
}
//}
}
}
#endregion
#region
/// <summary>
/// 查找
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSearch_Click(object sender, EventArgs e)
{
BindGrid();
}
#endregion
#region
// 定义实体
public class YearToDateModel
{
public string FO_NO { get; set; } = string.Empty;
public string Contract_Title { get; set; } = string.Empty;
public string Contractor { get; set; } = string.Empty;
public string Contract_Admin { get; set; } = string.Empty;
public string Main_Coordinator { get; set; } = string.Empty;
public string dataMonth { get; set; } = string.Empty;
public string SumScore { get; set; } = string.Empty;
}
#endregion
}
}