1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| public static String[] splitNextLigneCsv(StreamReader srFichier)
{
String[] ret = null;
List<String> listCells = new List<String>();
System.Text.StringBuilder ligne = new System.Text.StringBuilder(1024);
bool isOpen = false;
int n = srFichier.Read();
bool shouldContinue = true;
while ((n != -1) &&
(shouldContinue != false))
{
char c = (char)n;
if (c == '"')
{
if (ligne.Length == 0)
{
// C'est le premier guillement, on l'ignore
isOpen = true;
n = srFichier.Read();
}
else
{
// On lit le prochain caractere. Si c'est un ';'
// un retour chariot ou la fin du fichier, on l'ignore
n = srFichier.Read();
if (n != -1)
{
if (((char)n != ';') &&
((char)n != '\n') &&
((char)n != '\r'))
{
ligne.Append(c);
}
else
{
//Fin de la cellule
isOpen = false;
}
}
else
{
//Fin de la cellule et du fichier
listCells.Add(ligne.ToString());
ligne.Clear();
shouldContinue = false;
}
}
}
else if ((c == ';') ||
(c == '\r') ||
(c == '\n'))
{
if (isOpen == false)
{
//Fin de la cellule
if (c == ';')
{
listCells.Add(ligne.ToString());
ligne.Clear();
}
else if (listCells.Count > 0)
{
listCells.Add(ligne.ToString());
ligne.Clear();
shouldContinue = false;
}
}
else
{
ligne.Append(c);
}
n = srFichier.Read();
}
else
{
ligne.Append(c);
n = srFichier.Read();
}
}
if (listCells.Count > 0)
{
ret = listCells.ToArray();
}
return ret;
} |
Partager