示例#1
0
        public void Export(Stream stream, IList <ResultSet> resultSets, ApiRowErrorsCollection errors)
        {
            if (resultSets == null)
            {
                throw new ArgumentNullException(nameof(resultSets));
            }

            var recordSet = new RecordSet();

            foreach (var resultSet in resultSets)
            {
                recordSet.AddResultSet(resultSet);
            }

            Export(stream, resultSets[0].Entity, recordSet, errors);
        }
示例#2
0
        private void _editInExcel_Click(object sender, EventArgs e)
        {
            GetAllResults();

            using (var form = new EditInExcelInstructionsForm())
            {
                if (form.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                }
            }

            ApiRowErrorsCollection errors = null;

            var original = new RecordSet();
            var editing  = original;

            foreach (var resultSet in _resultSets)
            {
                original.AddResultSet(resultSet);
            }

            while (true)
            {
                string fileName;

                for (int i = 0; ; i++)
                {
                    fileName = "Wastedge Export";
                    if (i > 0)
                    {
                        fileName += $" ({i})";
                    }
                    fileName += ".xlsx";

                    fileName = Path.Combine(Path.GetTempPath(), fileName);

                    if (!File.Exists(fileName))
                    {
                        break;
                    }
                }

                try
                {
                    using (var stream = File.Create(fileName))
                    {
                        new ExcelExporter().Export(stream, _entity, editing, errors);
                    }

                    try
                    {
                        Process.Start(fileName);
                    }
                    catch
                    {
                        // Ignore exceptions.
                    }

                    using (var form = new EditInExcelWaitForm())
                    {
                        if (form.ShowDialog(this) != DialogResult.OK)
                        {
                            return;
                        }
                    }

                    RecordSetChanges changes;
                    RecordSet        modified;

                    while (true)
                    {
                        try
                        {
                            using (var stream = File.OpenRead(fileName))
                            {
                                modified = new ExcelImporter().Import(stream, _entity);
                            }

                            changes = RecordSetChanges.Create(original, modified);
                            break;
                        }
                        catch (IOException)
                        {
                            var result = TaskDialogEx.Show(
                                this,
                                "The Excel file cannot be opened. Please close the Excel file before uploading your changes",
                                Text,
                                TaskDialogCommonButtons.OK | TaskDialogCommonButtons.Cancel,
                                TaskDialogIcon.Error
                                );
                            if (result == DialogResult.Cancel)
                            {
                                return;
                            }
                        }
                    }

                    using (var form = new EditInExcelUploadForm(_api, _entity, changes))
                    {
                        form.ShowDialog(this);

                        errors = form.Errors;
                    }

                    if (errors != null)
                    {
                        using (var form = new ValidationErrorsForm(errors))
                        {
                            if (form.ShowDialog(this) != DialogResult.OK)
                            {
                                return;
                            }

                            editing = modified;
                        }
                    }
                    else
                    {
                        ReloadResults();

                        return;
                    }
                }
                finally
                {
                    try
                    {
                        File.Delete(fileName);
                    }
                    catch
                    {
                        // Ignore.
                    }
                }
            }
        }