using BLL; using MiniExcelLibs; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace FineUIPro.Web.HJGL.WeldingReport { public partial class PipePrefabricationSummary : PageBase { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { UnitWorkService.InitUnitWorkDropDownList(drpUnitWork, CurrUser.LoginProjectId, false); DateTime today = DateTime.Today; int daysSinceMonday = ((int)today.DayOfWeek + 6) % 7; dpReportDate.SelectedDate = today; dpStartDate.SelectedDate = today.AddDays(-daysSinceMonday); dpEndDate.SelectedDate = today.AddDays(6 - daysSinceMonday); BindGrid(); } } protected void btnQuery_Click(object sender, EventArgs e) { BindGrid(); } protected void btnExport_Click(object sender, EventArgs e) { DateTime reportDate; DateTime startDate; DateTime endDate; if (!TryGetFilterDates(out reportDate, out startDate, out endDate)) { return; } string templatePath = Path.Combine(Funs.RootPath, "File", "Excel", "DataOut", "管道预制汇总模板.xlsx"); if (!File.Exists(templatePath)) { ShowNotify("未找到管道预制汇总导出模板!", MessageBoxIcon.Warning); return; } List rows = GetSummary(reportDate, startDate, endDate); var value = CreateExportValue(rows, reportDate); string fileName = string.Format( "管道预制汇总_{0:yyyyMMdd}_{1:yyyyMMdd}-{2:yyyyMMdd}.xlsx", reportDate, startDate, endDate); string filePath = Path.Combine(Funs.RootPath, "File", "Excel", "Temp", Guid.NewGuid().ToString("N") + ".xlsx"); Directory.CreateDirectory(Path.GetDirectoryName(filePath)); MiniExcel.SaveAsByTemplate(filePath, templatePath, value); DownloadExcel(filePath, fileName); } private void BindGrid() { DateTime reportDate; DateTime startDate; DateTime endDate; if (!TryGetFilterDates(out reportDate, out startDate, out endDate)) { return; } List rows = GetSummary(reportDate, startDate, endDate); Grid1.RecordCount = rows.Count; Grid1.DataSource = rows; Grid1.DataBind(); BindSummary(rows); } private List GetSummary( DateTime reportDate, DateTime startDate, DateTime endDate) { string[] unitWorkIds = drpUnitWork.SelectedValueArray .Where(x => !string.IsNullOrWhiteSpace(x) && x != Const._Null) .ToArray(); return PipePrefabricationSummaryService.GetSummary( CurrUser.LoginProjectId, unitWorkIds, startDate, endDate, reportDate); } private bool TryGetFilterDates( out DateTime reportDate, out DateTime startDate, out DateTime endDate) { reportDate = DateTime.MinValue; startDate = DateTime.MinValue; endDate = DateTime.MinValue; if (!dpReportDate.SelectedDate.HasValue) { ShowNotify("请选择报表日期!", MessageBoxIcon.Warning); return false; } if (!dpStartDate.SelectedDate.HasValue || !dpEndDate.SelectedDate.HasValue) { ShowNotify("请选择完整的日期范围!", MessageBoxIcon.Warning); return false; } reportDate = dpReportDate.SelectedDate.Value.Date; startDate = dpStartDate.SelectedDate.Value.Date; endDate = dpEndDate.SelectedDate.Value.Date; if (startDate > endDate) { ShowNotify("开始日期不能晚于结束日期!", MessageBoxIcon.Warning); return false; } return true; } private void BindSummary(ICollection rows) { decimal totalDrawing = rows.Sum(x => x.TotalDrawingQuantity); decimal completed = rows.Sum(x => x.CompletedQuantity); JObject summary = new JObject { { "UnitCode", string.Empty }, { "UnitWorkCode", "Total" }, { "TotalDrawingQuantity", PipePrefabricationSummaryItem.FormatQuantity(totalDrawing) }, { "IsoDrawingQuantity", PipePrefabricationSummaryItem.FormatQuantity(rows.Sum(x => x.IsoDrawingQuantity)) }, { "WorkFrontQuantity", PipePrefabricationSummaryItem.FormatQuantity(rows.Sum(x => x.WorkFrontQuantity)) }, { "CompletedQuantity", PipePrefabricationSummaryItem.FormatQuantity(completed) }, { "QuantityCompleteRate", GetCompleteRate(completed, totalDrawing) }, { "DailyQuantity", PipePrefabricationSummaryItem.FormatQuantity(rows.Sum(x => x.DailyQuantity)) }, { "PreviousDayCumulativeQuantity", PipePrefabricationSummaryItem.FormatQuantity(rows.Sum(x => x.PreviousDayCumulativeQuantity)) }, { "WeeklyQuantity", PipePrefabricationSummaryItem.FormatQuantity(rows.Sum(x => x.WeeklyQuantity)) }, { "LastWeekCumulativeQuantity", PipePrefabricationSummaryItem.FormatQuantity(rows.Sum(x => x.LastWeekCumulativeQuantity)) }, { "Note", string.Empty } }; Grid1.SummaryData = summary; } private static string GetCompleteRate(decimal completed, decimal totalDrawing) { return totalDrawing <= 0M ? "0.00%" : (completed * 100M / totalDrawing).ToString("0.00") + "%"; } private static Dictionary CreateExportValue( ICollection rows, DateTime reportDate) { decimal totalDrawing = rows.Sum(x => x.TotalDrawingQuantity); decimal completed = rows.Sum(x => x.CompletedQuantity); // 只向模板上半部分的数据占位符传值;下方“陕西化建人力统计表”保持模板原样空白。 return new Dictionary { { "ReportDate", reportDate.ToString("yyyy-MM-dd") }, { "Data", rows }, // MiniExcel 1.31.3 的普通对象不支持模板中的嵌套属性,合计值使用扁平占位符。 { "TotalUnitCode", string.Empty }, { "TotalUnitWorkCode", "Total" }, { "TotalDrawingQuantity", totalDrawing }, { "TotalIsoDrawingQuantity", rows.Sum(x => x.IsoDrawingQuantity) }, { "TotalWorkFrontQuantity", rows.Sum(x => x.WorkFrontQuantity) }, { "TotalCompletedQuantity", completed }, { "TotalQuantityCompleteRate", GetCompleteRate(completed, totalDrawing) }, { "TotalDailyQuantity", rows.Sum(x => x.DailyQuantity) }, { "TotalPreviousDayCumulativeQuantity", rows.Sum(x => x.PreviousDayCumulativeQuantity) }, { "TotalWeeklyQuantity", rows.Sum(x => x.WeeklyQuantity) }, { "TotalLastWeekCumulativeQuantity", rows.Sum(x => x.LastWeekCumulativeQuantity) }, { "TotalNote", string.Empty } }; } private void DownloadExcel(string filePath, string fileName) { FileInfo file = new FileInfo(filePath); Response.Clear(); Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.AddHeader("Content-Length", file.Length.ToString()); Response.TransmitFile(filePath, 0, file.Length); Response.Flush(); File.Delete(filePath); Response.End(); } } }