예제 #1
0
        public static IExcelService GetExcelService(EXCELVersion excel, string strPath, IFileService fileService)
        {
            switch (excel)
            {
            case EXCELVersion.EXCEL2003:
                return(new Excel2003Service(strPath, fileService));

            case EXCELVersion.EXCEL2007:
                return(new Excel2007Service(strPath, fileService));
            }
            return(null);
        }
예제 #2
0
        /// <summary>
        /// 将项目文件发布为EXCEL文档
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        public bool ExportProjectToExcel(ProjectModel project, string strFilePath, IFileService fileService)
        {
            if (project == null)
            {
                return(false);
            }
            try
            {
                EXCELVersion version = EXCELVersion.EXCEL2003;
                strFilePath += "\\" + project.Name + ".xls";
                IExcelService excelService          = ExcelServiceManager.GetExcelService(version, strFilePath, fileService);
                List <string> sheetNames            = new List <string>();
                const int     Loop_Amount_Per_Sheet = 8;
                GenerateExcelSummarySheet(project, ref excelService, out sheetNames);
                ControllerManager controllerManager = new ControllerManager();
                controllerManager.InitializeAllControllerOperation(null);

                //生成“项目摘要”信息


                foreach (var controller in project.Controllers)
                {
                    IControllerOperation controllerOperation = controllerManager.GetController(controller.Type);

                    int loopSheetAmount = Convert.ToInt32(Math.Ceiling((float)controller.Loops.Count / Loop_Amount_Per_Sheet));
                    int loopStartIndex  = 0;//记录每个Sheet页内初始索引号
                    for (int i = 0; i < loopSheetAmount; i++)
                    {
                        string           loopName     = controller.Name + "-回路分组" + (i + 1).ToString();
                        int              loopEndIndex = (i + 1) * Loop_Amount_Per_Sheet;
                        List <LoopModel> lstLoops     = new List <LoopModel>();
                        for (int j = loopStartIndex; j < loopEndIndex; j++)
                        {
                            //loopStartIndex++;
                            if (j >= controller.Loops.Count) //超过最大回路数
                            {
                                break;
                            }
                            lstLoops.Add(controller.Loops[j]);
                        }
                        controllerOperation.ExportLoopDataToExcel(ref excelService, lstLoops, loopName);
                        lstLoops.Clear();
                    }
                    if (controller.StandardConfig.Count > 0)
                    {
                        string strSheetName = controller.Name + "-标准组态";
                        controllerOperation.ExportStandardLinkageDataToExcel(ref excelService, controller.StandardConfig, strSheetName);
                    }
                    if (controller.MixedConfig.Count > 0)
                    {
                        string strSheetName = controller.Name + "-混合组态";
                        controllerOperation.ExportMixedLinkageDataToExcel(ref excelService, controller.MixedConfig, strSheetName);
                    }
                    if (controller.GeneralConfig.Count > 0)
                    {
                        string strSheetName = controller.Name + "-通用组态";
                        controllerOperation.ExportGeneralLinkageDataToExcel(ref excelService, controller.GeneralConfig, strSheetName);
                    }
                    if (controller.ControlBoard.Count > 0)
                    {
                        string strSheetName = controller.Name + "-网络手动盘";
                        controllerOperation.ExportManualControlBoardDataToExcel(ref excelService, controller.ControlBoard, strSheetName);
                    }
                }
                excelService.SaveToFile();

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }