Beispiel #1
0
        /// <summary>
        /// Apertura del file excel in lettura / scrittura, viene passato anche il formato con il quale si identificheranno le informazioni
        /// sul file excel
        /// </summary>
        /// <param name="excelPath"></param>
        /// <param name="formatoExcel"></param>
        /// <param name="modalitaApertura"></param>
        /// <returns></returns>
        public bool OpenFileExcel(string excelPath, Constants.FormatFileExcel formatoExcel, Constants.ModalitaAperturaExcel modalitaApertura)
        {
            try
            {
                // validazione su formnato e path
                if (excelPath == String.Empty)
                {
                    throw new Exception(ExceptionMessages.EXCEL_EMPTYPATH);
                }

                // validazione sul formato in input
                if (formatoExcel == Constants.FormatFileExcel.NotDefined)
                {
                    throw new Exception(ExceptionMessages.EXCEL_FORMATNOTDEFINED);
                }

                if (modalitaApertura == Constants.ModalitaAperturaExcel.READ && !File.Exists(excelPath))
                {
                    throw new Exception(ExceptionMessages.EXCEL_SOURCENOTEXISTING);
                }
                else
                {
                    bool esistenza = false;

                    // ricreazione del file
                    ServiceLocator.GetUtilityFunctions.BuildFilePath(excelPath, out esistenza);

                    if (esistenza)
                    {
                        ConsoleService.ConsoleExcel.EsistenzaFileExcel_Message(excelPath);
                    }
                }

                // set licenza corrente
                ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;

                _excelName = ServiceLocator.GetUtilityFunctions.GetFileName(excelPath);

                FileStream currentFileExcel = new FileStream(excelPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                _openedExcel = new ExcelPackage(currentFileExcel);

                // decisione su cosa viene istanziato in base alla modalità su cui agire sul file excel
                if (modalitaApertura == Constants.ModalitaAperturaExcel.READ)
                {
                    _excelReaders = new ExcelReaders(ref _openedExcel, formatoExcel);
                }
                else if (modalitaApertura == Constants.ModalitaAperturaExcel.WRITE)
                {
                    _excelWriters = new ExcelWriters(ref _openedExcel, formatoExcel);
                }

                return(true);
            }
            catch (Exception e)
            {
                throw new Exception(String.Format(ExceptionMessages.EXCEL_PROBLEMAAPERTURAFILE, ServiceLocator.GetUtilityFunctions.GetFileName(excelPath), e.Message));
            }
        }
Beispiel #2
0
 /// <summary>
 /// Passaggio dello stream excel aperto con annesso anche il formato excel in scrittura corrente
 /// </summary>
 /// <param name="openedExcel"></param>
 /// <param name="formatoExcel"></param>
 public ExcelWriters(ref ExcelPackage openedExcel, Constants.FormatFileExcel formatoExcel)
 {
     _openedExcel  = openedExcel;
     _formatoExcel = formatoExcel;
 }