| | | 1 | | using ExcelDataReader; |
| | | 2 | | using Microsoft.AspNetCore.Http; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Data; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.Linq; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | |
| | | 10 | | namespace SVETA.Api.Services.Implements.ImportingExporting |
| | | 11 | | { |
| | | 12 | | static class FileReader |
| | | 13 | | { |
| | | 14 | | public static DataTable ReadFromExcel(Stream stream, string contentType) |
| | 0 | 15 | | { |
| | 0 | 16 | | System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); |
| | 0 | 17 | | var readerConfig = new ExcelReaderConfiguration() |
| | 0 | 18 | | { |
| | 0 | 19 | | AutodetectSeparators = new char[] { ',', ';', '\t', '|', '#' }, |
| | 0 | 20 | | AnalyzeInitialCsvRows = 0 |
| | 0 | 21 | | }; |
| | 0 | 22 | | using (var reader = contentType switch |
| | 0 | 23 | | { |
| | 0 | 24 | | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => ExcelReaderFactory.CreateReader(s |
| | 0 | 25 | | "application/vnd.ms-excel" => ExcelReaderFactory.CreateCsvReader(stream, readerConfig), |
| | 0 | 26 | | "text/csv" => ExcelReaderFactory.CreateCsvReader(stream, readerConfig), |
| | 0 | 27 | | _ => throw new NotImplementedException($"File type {contentType} not supported"), |
| | 0 | 28 | | }) |
| | 0 | 29 | | { |
| | 0 | 30 | | var conf = new ExcelDataSetConfiguration |
| | 0 | 31 | | { |
| | 0 | 32 | | ConfigureDataTable = _ => new ExcelDataTableConfiguration |
| | 0 | 33 | | { |
| | 0 | 34 | | UseHeaderRow = true, |
| | 0 | 35 | | ReadHeaderRow = (rowReader) => |
| | 0 | 36 | | { |
| | 0 | 37 | | rowReader.Read(); // пропускаем описание шаблона |
| | 0 | 38 | | rowReader.Read(); // пропускаем русские колонки |
| | 0 | 39 | | } |
| | 0 | 40 | | } |
| | 0 | 41 | | }; |
| | 0 | 42 | | return reader.AsDataSet(conf).Tables[0]; |
| | | 43 | | } |
| | 0 | 44 | | } |
| | | 45 | | } |
| | | 46 | | } |