lixiaojun
2025-02-20 25dd33f23a5e3f8386e25d9298743288a1b4c9f6
WinFrmUI/Yw.WinFrmUI.Hydro.Core/06-visual/00-core/HydroVisualListCtrl.cs
@@ -1,4 +1,11 @@
using Yw.Model;
using DevExpress.Xpo.Helpers;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraPrinting;
using DevExpress.XtraPrintingLinks;
using System.Diagnostics;
using Yw.EPAnet;
using Yw.Model;
namespace Yw.WinFrmUI
{
@@ -11,7 +18,6 @@
            {
                if (page.Controls[0] is IHydroVisualList ctrl)
                {
                    ctrl.ShowFindPanel = false;
                    ctrl.HydroClickInfoEvent += (visual) =>
                    {
                        this.HydroClickInfoEvent?.Invoke(visual);
@@ -65,15 +71,41 @@
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData(Yw.Model.HydroModelInfo hydroInfo)
        public void SetBindingData
            (
                Yw.Model.HydroModelInfo hydroInfo,
                HydroChangeHelper changeHelper = null,
                HydroPropStatusHelper propStatusHelper = null
            )
        {
            this.SetBindingData(hydroInfo, null);
            this.SetBindingData(hydroInfo, allCalcuResultVisualDict: null, changeHelper, propStatusHelper);
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData(Yw.Model.HydroModelInfo hydroInfo, List<HydroCalcuResult> allCalcuResultList)
        public void SetBindingData
            (
                Yw.Model.HydroModelInfo hydroInfo,
                List<HydroCalcuVisualResult> allCalcuResultList,
                HydroChangeHelper changeHelper = null,
                HydroPropStatusHelper propStatusHelper = null
            )
        {
            var allCalcuResultVisualDict = allCalcuResultList?.ToDictionary(x => x.Code);
            SetBindingData(hydroInfo, allCalcuResultVisualDict, changeHelper, propStatusHelper);
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData
            (
                Yw.Model.HydroModelInfo hydroInfo,
                Dictionary<string, HydroCalcuVisualResult> allCalcuResultVisualDict,
                HydroChangeHelper changeHelper = null,
                HydroPropStatusHelper propStatusHelper = null
            )
        {
            if (hydroInfo == null)
            {
@@ -83,7 +115,7 @@
            {
                if (page.Controls[0] is IHydroVisualList ctrl)
                {
                    ctrl.SetBindingData(hydroInfo, allCalcuResultList);
                    ctrl.SetBindingData(hydroInfo, allCalcuResultVisualDict, changeHelper, propStatusHelper);
                    if (HidePageWhenListIsNull)
                    {
                        if (!ctrl.HasHydroList)
@@ -98,7 +130,12 @@
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData(List<HydroVisualViewModel> allVisualViewModelList)
        public void SetBindingData
            (
                List<HydroVisualViewModel> allVisualViewModelList,
                HydroChangeHelper changeHelper = null,
                HydroPropStatusHelper propStatusHelper = null
            )
        {
            if (allVisualViewModelList == null || allVisualViewModelList.Count < 1)
            {
@@ -108,7 +145,7 @@
            {
                if (page.Controls[0] is IHydroVisualList ctrl)
                {
                    ctrl.SetBindingData(allVisualViewModelList);
                    ctrl.SetBindingData(allVisualViewModelList, changeHelper, propStatusHelper);
                    if (HidePageWhenListIsNull)
                    {
                        if (!ctrl.HasHydroList)
@@ -199,7 +236,7 @@
        /// <summary>
        /// 更新计算属性
        /// </summary>
        public void UpdateCalcuProperty(List<HydroCalcuResult> allCalcuResultList)
        public void UpdateCalcuProperty(List<HydroCalcuVisualResult> allCalcuResultList)
        {
            if (allCalcuResultList == null || allCalcuResultList.Count < 1)
            {
@@ -218,15 +255,39 @@
        }
        /// <summary>
        /// 更新计算属性
        /// </summary>
        public void UpdateCalcuProperty(Dictionary<string, HydroCalcuVisualResult> allCalcuResultVisualDict)
        {
            if (allCalcuResultVisualDict == null || allCalcuResultVisualDict.Count < 1)
            {
                return;
            }
            foreach (var page in this.tabPane1.Pages)
            {
                if (page.PageVisible)
                {
                    if (page.Controls[0] is IHydroVisualList ctrl)
                    {
                        ctrl.UpdateCalcuProperty(allCalcuResultVisualDict);
                    }
                }
            }
        }
        /// <summary>
        /// 设置简单视图
        /// </summary>
        public void SetSimpleView()
        {
            foreach (var page in this.tabPane1.Pages)
            {
                if (page.Controls[0] is IHydroVisualList ctrl)
                if (page.PageVisible)
                {
                    ctrl.SetSimpleView();
                    if (page.Controls[0] is IHydroVisualList ctrl)
                    {
                        ctrl.SetSimpleView();
                    }
                }
            }
        }
@@ -238,9 +299,12 @@
        {
            foreach (var page in this.tabPane1.Pages)
            {
                if (page.Controls[0] is IHydroVisualList ctrl)
                if (page.PageVisible)
                {
                    ctrl.SetNormalView();
                    if (page.Controls[0] is IHydroVisualList ctrl)
                    {
                        ctrl.SetNormalView();
                    }
                }
            }
        }
@@ -252,27 +316,135 @@
        {
            foreach (var page in this.tabPane1.Pages)
            {
                if (page.Controls[0] is IHydroVisualList ctrl)
                if (page.PageVisible)
                {
                    ctrl.SetCalcuView();
                    if (page.Controls[0] is IHydroVisualList ctrl)
                    {
                        ctrl.SetCalcuView();
                    }
                }
            }
        }
        /// <summary>
        /// 设置批量设置视图
        /// 设置批量视图
        /// </summary>
        public void SetBulkSetView()
        public void SetBulkView()
        {
            foreach (var page in this.tabPane1.Pages)
            {
                if (page.Controls[0] is IHydroVisualList ctrl)
                if (page.PageVisible)
                {
                    ctrl.SetBulkSetView();
                    if (page.Controls[0] is IHydroVisualList ctrl)
                    {
                        ctrl.SetBulkView();
                    }
                }
            }
        }
        /// <summary>
        /// 设置结果视图
        /// </summary>
        public void SetResultView()
        {
            foreach (var page in this.tabPane1.Pages)
            {
                if (page.PageVisible)
                {
                    if (page.Controls[0] is IHydroVisualList ctrl)
                    {
                        ctrl.SetResultView();
                    }
                }
            }
        }
        //导出多gridview到Excel
        private void ExportMultipleGridViewsToExcel(List<GridView> gridViews, List<string> sheetNames, string filePath)
        {
            if (gridViews == null || gridViews.Count < 1)
            {
                return;
            }
            if (sheetNames == null || sheetNames.Count < 1)
            {
                return;
            }
            if (gridViews.Count != sheetNames.Count)
            {
                return;
            }
            WaitFormHelper.ShowWaitForm("正在导出,请稍后...");
            // 创建一个 PrintingSystem 对象
            PrintingSystem printingSystem = new PrintingSystem();
            // 创建一个 CompositeLink 对象,用于组合多个 GridView
            CompositeLink compositeLink = new CompositeLink(printingSystem);
            // 遍历每个 GridView,将其添加到 CompositeLink 中
            for (int i = 0; i < gridViews.Count; i++)
            {
                // 创建一个 PrintableComponentLink,用于导出 GridView
                PrintableComponentLink link = new PrintableComponentLink(printingSystem);
                link.Component = gridViews[i].GridControl;
                // 将 PrintableComponentLink 添加到 CompositeLink 中
                compositeLink.Links.Add(link);
            }
            //自定义sheet名称
            compositeLink.PrintingSystem.XlSheetCreated += (sender, e) =>
            {
                var sheetName = sheetNames[e.Index];
                e.SheetName = sheetName;
            };
            //compositeLink.Landscape = true;
            compositeLink.CreatePageForEachLink();
            // 设置 Excel 导出选项
            var options = new XlsxExportOptions
            {
                ExportMode = XlsxExportMode.SingleFilePageByPage, // 每个 GridView 导出到一个单独的工作表
                TextExportMode = TextExportMode.Value, // 导出文本模式
                SheetName = "Sheet" // 工作表名称前缀(如果不设置,默认使用 Sheet1, Sheet2 等)
            };
            // 导出到 Excel 文件
            using (FileStream stream = new FileStream(filePath, FileMode.Create))
            {
                compositeLink.ExportToXlsx(stream, options);
            }
            // 释放资源
            printingSystem.Dispose();
            WaitFormHelper.HideWaitForm();
            if (XtraMessageBox.Show("导出成功,是否打开文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                Process.Start(new ProcessStartInfo(filePath) { UseShellExecute = true });
            }
        }
        //导出Excel
        private void btnExportToXlsx_Click(object sender, EventArgs e)
        {
            var filePath = FileDialogHelper.SaveFile("导出Excel文件", "Excel文件|*.xlsx");
            if (string.IsNullOrEmpty(filePath))
            {
                return;
            }
            var gridViews = new List<GridView>();
            var sheetNames = new List<string>();
            foreach (var page in this.tabPane1.Pages)
            {
                if (page.PageVisible)
                {
                    if (page.Controls[0] is IHydroVisualList ctrl)
                    {
                        gridViews.Add(ctrl.GetGridView());
                        sheetNames.Add(page.Caption);
                    }
                }
            }
            ExportMultipleGridViewsToExcel(gridViews, sheetNames, filePath);
        }