private static DataTable OpenTextFixedFile(string filePath, PriceFormat?priceFormat, PriceEncode encode, DataTableMarking dataTableMarking) { return(OpenPriceTable(filePath, dataTableMarking, priceFormat, encode)); }
public static List <DataTable> OpenPriceFile(string filePath, PriceFormat?priceFormat, PriceEncode encode, string delimiter, DataTableMarking dataTableMarking) { try { if (!priceFormat.HasValue) { throw new Exception($"Неизвестный формат {priceFormat}"); } var tables = new List <DataTable>(); if ((priceFormat.Value == PriceFormat.DBF) || (priceFormat.Value == PriceFormat.NativeDbf)) { var encoding = encode == PriceEncode.Cp1251 ? Encoding.GetEncoding(1251) : Encoding.GetEncoding(866); var beliveInCodePageByte = encode == PriceEncode.CoNo; tables.Add(Dbf.Load(filePath, encoding, beliveInCodePageByte)); return(tables); } if ((priceFormat.Value == PriceFormat.XLS) || (priceFormat.Value == PriceFormat.NativeXls)) { return(OpenXlsFile(filePath)); } if ((priceFormat.Value == PriceFormat.DelimDOS) || (priceFormat.Value == PriceFormat.DelimWIN) || (priceFormat.Value == PriceFormat.NativeDelim)) { tables.Add(OpenTextDelimiterFile(filePath, priceFormat, delimiter, encode)); return(tables); } if ((priceFormat.Value == PriceFormat.FixedDOS) || (priceFormat.Value == PriceFormat.FixedWIN) || (priceFormat.Value == PriceFormat.NativeFixed)) { tables.Add(OpenTextFixedFile(filePath, priceFormat, encode, dataTableMarking)); return(tables); } if (priceFormat.Value == PriceFormat.UniversalFormalizer || priceFormat.Value == PriceFormat.FarmaimpeksOKPFormalizer) { return(new List <DataTable>()); } throw new Exception($"Неизвестный формат {priceFormat}"); } catch (Exception ex) { var error = $"Ошибка при открытии файла\nЛокальный путь:{filePath}\nФормат:{priceFormat}\nРазделитель:{delimiter}"; _logger.Error(error, ex); return(null); } }
public OpenPriceFileThread(string filePath, PriceFormat?priceFormat, PriceEncode encode, string delimiter, DataTableMarking dataTableMarking) { _filePath = filePath; _priceFormat = priceFormat; _delimiter = delimiter; _dataTableMarking = dataTableMarking; _handle = new Thread(ThreadProc); _formWait = new frmWait(); _priceEncode = encode; }
public static List <DataTable> AsyncOpenPriceFile(string filePath, PriceFormat?priceFormat, PriceEncode encode, string delimiter, DataTableMarking dataTableMarking) { var openPriceThread = new OpenPriceFileThread(filePath, priceFormat, encode, delimiter, dataTableMarking); openPriceThread.Start(); return(openPriceThread.WaitStop()); }
public static DataTable OpenPriceTable(string filePath, DataTableMarking dataTableMarking, PriceFormat?priceFormat, PriceEncode encode) { DataTable dataTablePrice = null; if (dataTableMarking.Rows.Count > 1) { var fields = new ArrayList(); for (var i = 0; i < dataTableMarking.Rows.Count; i++) { var row = dataTableMarking.Rows[i]; var fieldName = row["MNameField"].ToString(); var beginPosition = Convert.ToInt32(row["MBeginField"]); var endPosition = Convert.ToInt32(row["MEndField"]); fields.Add(new TxtFieldDef(fieldName, beginPosition, endPosition)); } fields.Sort(new TxtFieldDef()); var schemaFile = Path.GetDirectoryName(filePath) + Path.DirectorySeparatorChar + "Schema.ini"; using (var w = new StreamWriter(schemaFile, false, Encoding.GetEncoding(1251))) { w.WriteLine("[" + Path.GetFileName(filePath) + "]"); w.WriteLine(((priceFormat == PriceFormat.FixedWIN) || (priceFormat == PriceFormat.NativeFixed && encode == PriceEncode.Cp1251)) ? "CharacterSet=ANSI" : "CharacterSet=OEM"); w.WriteLine("Format=FixedLength"); w.WriteLine("ColNameHeader=False"); w.WriteLine("MaxScanRows=300"); if (fields.Count > 0) { int j = 1; TxtFieldDef prevTFD, currTFD = (TxtFieldDef)fields[0]; if (1 == currTFD.posBegin) { w.WriteLine(String.Format("Col{0}={1} Text Width {2}", j, currTFD.fieldName, currTFD.posEnd)); j++; } else { w.WriteLine(String.Format("Col{0}={1} Text Width {2}", j, "x", currTFD.posBegin - 1)); j++; w.WriteLine(String.Format("Col{0}={1} Text Width {2}", j, currTFD.fieldName, currTFD.posEnd - currTFD.posBegin + 1)); j++; } for (var i = 1; i <= fields.Count - 1; i++) { prevTFD = (TxtFieldDef)fields[i - 1]; currTFD = (TxtFieldDef)fields[i]; if (currTFD.posBegin == prevTFD.posEnd + 1) { w.WriteLine(String.Format("Col{0}={1} Text Width {2}", j, currTFD.fieldName, currTFD.posEnd - currTFD.posBegin + 1)); j++; } else { w.WriteLine(String.Format("Col{0}={1} Text Width {2}", j, "x", currTFD.posBegin - prevTFD.posEnd - 1)); j++; w.WriteLine(String.Format("Col{0}={1} Text Width {2}", j, currTFD.fieldName, currTFD.posEnd - currTFD.posBegin + 1)); j++; } } } else { w.WriteLine(String.Format("Col{0}=x1 Text Width {1}", 1, 255)); } } var connectionString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Text\"", Path.GetDirectoryName(filePath)); using (var connection = new OleDbConnection(connectionString)) { connection.Open(); var dataAdapter = new OleDbDataAdapter(String.Format("select * from {0}", Path.GetFileName(filePath).Replace(".", "#")), connection); dataTablePrice = new DataTable(); dataAdapter.SyncFill(dataTablePrice); if (dataTablePrice.Rows.Count == 0) { throw new Exception("При открытии файла таблица с полями прайс-листа оказалась пуста. Предположительно неверный формат или файл поврежден"); } } } return(dataTablePrice); }