Пример #1
0
        public void ReadData(DocumentsStorage documentStorage)
        {
            using (var stream = excelFileInfo.OpenRead())
            {
                var configuration = new ExcelReaderConfiguration()
                {
                    FallbackEncoding = Encoding.Default
                };
                using (var reader = ExcelReaderFactory.CreateOpenXmlReader(stream, configuration))
                {
                    try
                    {
                        do
                        {
                            int rowCount   = reader.RowCount;
                            int workingRow = 0;

                            // + Work with header +
                            int attributePositionStart = 5;
                            int fieldCount             = reader.FieldCount - attributePositionStart;
                            documentStorage.Init(fieldCount);

                            reader.Read();
                            for (int attributeNumber = 0; attributeNumber < fieldCount; attributeNumber++)
                            {
                                documentStorage.SetAttributeName(attributeNumber, reader.GetString(attributeNumber + attributePositionStart));
                            }
                            workingRow++;

                            reader.Read(); reader.Read();
                            for (int attributeNumber = 0; attributeNumber < fieldCount; attributeNumber++)
                            {
                                documentStorage.SetAttributeIdentifier(attributeNumber, reader.GetString(attributeNumber + attributePositionStart));
                            }
                            // - Work with header -

                            int documentCount = 1;
                            while (reader.Read())
                            {
                                workingRow++;

                                string documentIdentifier = null;
                                ReadColumn(reader, 0, out object textFromReader, out Type textFromReaderType);
                                if (textFromReader != null && !string.IsNullOrWhiteSpace(textFromReader.ToString()))
                                {
                                    documentIdentifier = textFromReader.ToString();
                                }
                                Document document = documentStorage.CreateDocument(documentIdentifier);
                                ReadTextFieldFromExcelReader(reader, document, 1, DocumentsStorage.FilesType.Text);
                                ReadTextFieldFromExcelReader(reader, document, 2, DocumentsStorage.FilesType.ScanCopy);
                                ReadTextFieldFromExcelReader(reader, document, 3, DocumentsStorage.FilesType.TextPdf);
                                ReadTextFieldFromExcelReader(reader, document, 4,
                                                             DocumentsStorage.FilesType.Attachments);

                                bool shouldToBeAddedInDocumentStorage = false;

                                for (int attributeNumber = 0; attributeNumber < fieldCount; attributeNumber++)
                                {
                                    object value;
                                    Type   valueType;
                                    string attributeId = documentStorage.GetAttributeIdentifier(attributeNumber);
                                    int    colPosition = attributeNumber + attributePositionStart;
                                    ReadColumn(reader, colPosition, out value, out valueType);
                                    if (value != null)
                                    {
                                        document.SetAttributeValue(attributeId,
                                                                   new DocumentAttributeValue(value, valueType));
                                        shouldToBeAddedInDocumentStorage = true;
                                    }
                                }

                                try
                                {
                                    if (shouldToBeAddedInDocumentStorage)
                                    {
                                        documentStorage.AddDocument(document);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Exception sendException =
                                        new Exception($"Произошла ошибка при добавлении документа. Документ '{document.Identifier}' не был добавлен для обработки. Продолжаем обработку.", ex);
                                    ExceptionOccured?.BeginInvoke(sendException, null, null);
                                    continue;
                                }
                            }
                        } while (reader.NextResult());
                    }
                    catch (Exception ex)
                    {
                        Exception sendException = new Exception("Произошла ошибка при чтении файда шаблона.", ex);
                        ExceptionOccured?.BeginInvoke(sendException, null, null);
                    }
                }
            }
        }