/// <summary> /// Charge le fichier qui est dans Filename /// Si Filename n'est pas renseigné une Exception est déclenchée /// </summary> /// <param name="NbLines"> /// Nombre de ligne du fichier à charger, si c'est -1 on charge tout /// </param> /// <param name="NbCols"> /// Nombre de colonnes dans le fichier a charger (ne monte pas les lignes en erreur) /// Si -1, on ne gere pas le nombre de colonne /// </param> public void WrapFile(int NbLines, int NbCols) { if (_autoDetect) detectSeparators(); FileInfo fi = new FileInfo(_filename); //if (NbLines == -1) CleanCRLF(); string delimStr = TextIdentifier + ColumnSeparator + TextIdentifier; char[] delimiter = { (char)255 }; string[] aLine; string line; int firstLineColCount = 0; try { using (StreamReader sr = new StreamReader(_filename, Encoding)) { int LigneEnCours = 0; while ((line = sr.ReadLine()) != null) { line = line.Replace(string.Format("{0}", (char)0x1A), ""); var row = new Row(); if ((NbLines == -1) ? true : (LigneEnCours < NbLines)) { if (!string.IsNullOrWhiteSpace(line)) { line = line.Replace(delimStr, delimiter[0].ToString()); aLine = line.Split(delimiter); if (((NbCols > 0) && (aLine.Length == NbCols)) || (NbCols == -1)) { var cellIndex = 0; for (int i = 0; i < aLine.Length; ++i) { var cell = new Cell { Index = cellIndex++ }; if ((i == 0) && (TextIdentifier.Length > 0)) aLine[i] = aLine[i].Substring(TextIdentifier.Length); if ((i == (aLine.Length - 1)) && (TextIdentifier.Length > 0)) { if (aLine[i] == TextIdentifier) aLine[i] = ""; else aLine[i] = aLine[i].Substring(0, aLine[i].Length - 1); } if (!aLine[i].StartsWith(string.Format("{0}", (char)0x1A))) { cell.Value = aLine[i].Replace((char)0x1A, (char)0x20); row.Cells.Add(cell); } } if (row.Cells.Count > 0) Table.Rows.Add(row); if (firstLineColCount == 0) firstLineColCount = row.Cells.Count; else { if (firstLineColCount != row.Cells.Count) { CSVError err = new CSVError(); err.numLine = LigneEnCours + 1; err.errorText = string.Format("{0} colonne{1}", row.Cells.Count, (row.Cells.Count > 1) ? "s" : ""); Errors.Add(err); row.HasError = true; } } } } } else break; LigneEnCours++; } sr.Close(); } } catch (Exception ex) { throw new CSVException(ex.Message); } }
/// <summary> /// Charge le fichier qui est dans Filename /// Si Filename n'est pas renseigné une Exception est déclenchée /// </summary> /// <param name="NbLines"> /// Nombre de ligne du fichier à charger, si c'est -1 on charge tout /// </param> /// <param name="NbCols"> /// Nombre de colonnes dans le fichier a charger (ne monte pas les lignes en erreur) /// Si -1, on ne gere pas le nombre de colonne /// </param> public void WrapFile(int NbLines, int NbCols) { if (FAutoDetect) detectSeparators(); FileInfo fi = new FileInfo(FFilename); //if (NbLines == -1) CleanCRLF(); string delimStr = FTextIdentifier + FColumnSeparator + FTextIdentifier; char[] delimiter = { (char)255 }; string[] aLine; string line; ArrayList cols; int firstLineColCount = 0; try { using (StreamReader sr = new StreamReader(FFilename, FEncoding)) { int LigneEnCours = 0; while ((line = sr.ReadLine()) != null) { line = line.Replace(string.Format("{0}", (char)0x1A), ""); //stringLines.Add(line); if ((NbLines == -1) ? true : (LigneEnCours < NbLines)) { if (!String.IsNullOrWhiteSpace(line)) { line = line.Replace(delimStr, delimiter[0].ToString()); aLine = line.Split(delimiter); if (((NbCols > 0) && (aLine.Length == NbCols)) || (NbCols == -1)) { cols = new ArrayList(); for (int i = 0; i < aLine.Length; ++i) { if ((i == 0) && (FTextIdentifier.Length > 0)) aLine[i] = aLine[i].Substring(FTextIdentifier.Length); if ((i == (aLine.Length - 1)) && (FTextIdentifier.Length > 0)) { if (aLine[i] == FTextIdentifier) aLine[i] = ""; else aLine[i] = aLine[i].Substring(0, aLine[i].Length - 1); } if (!aLine[i].StartsWith(string.Format("{0}", (char)0x1A))) cols.Add(aLine[i].Replace((char)0x1A, (char)0x20)); } if (cols.Count > 0) filelines.Add(cols); if (firstLineColCount == 0) firstLineColCount = cols.Count; else { if (firstLineColCount != cols.Count) { CSVError err = new CSVError(); err.numLine = LigneEnCours + 1; err.errorText = string.Format("{0} colonne{1}", cols.Count, (cols.Count > 1) ? "s" : ""); FErrors.Add(err); } } } } } else break; LigneEnCours++; } sr.Close(); } } catch (Exception ex) { throw new CSVException(ex.Message); } }