private void ReportProgress(object sender, SpreadsheetCollectionProgressModel e)
        {
            IsProcessCancellable = e.IsCancellable;
            State = e.State;

            if (e.steps.Count > 0)
            {
                var newStep = e.steps[^ 1];
Ejemplo n.º 2
0
        public void MergeDTfromFiles(IProgress <SpreadsheetCollectionProgressModel> progress, CancellationToken cancellationToken)
        {
            SpreadsheetCollectionProgressModel report = new SpreadsheetCollectionProgressModel(ProcessState.IsRunning);
            List <FileInfo> files = StringToFileInfo(Options.Files);
            string          dir   = files[0].DirectoryName;

            List <string> sheets = GenerateSheets();

            List <DataTable> tables     = new List <DataTable>();
            DataTable        errorTable = new DataTable();

            foreach (FileInfo file in files)
            {
                report.steps.Add($"Loading {file.Name}...");
                progress.Report(report);

                var package = SpreadsheetUtilities.GetDTfromExcel(file, Convert.ToInt32(Options.StartRow), sheets, Options.Passwords);
                tables.AddRange(package.data);
                errorTable.Merge(package.errors);
                cancellationToken.ThrowIfCancellationRequested();
            }

            //Combine all tables into first table in list
            for (int i = 1; i < tables.Count; i++)
            {
                report.steps.Add($"Merging {tables[i].TableName}...");
                progress.Report(report);
                tables[0].Merge(tables[i], false, MissingSchemaAction.Add);
                tables[i] = null;
                cancellationToken.ThrowIfCancellationRequested();
            }

            report.IsCancellable = false;
            report.steps.Add("Sending to Excel...");
            progress.Report(report);

            try
            {
                cancellationToken.ThrowIfCancellationRequested();
                SpreadsheetUtilities.DTtoExcel(tables[0], dir, errorTable);
            }
            catch (ArgumentOutOfRangeException e)
            {
                throw new ArgumentOutOfRangeException("Sheet(s) not found in any files!", e);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            finally
            {
                report.State = ProcessState.Completed;
                report.steps.Add("Process Completed!");
                progress.Report(report);
            }
        }