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
| StreamReader MonFichier = new StreamReader(CheminFichierACharger, Encoding.Default);
String LigneEnCours = "";
do // On passe l'entete du fichier (mis en commentaire par des // en tete de ligne)
{
LigneEnCours = MonFichier.ReadLine();
} while (LigneEnCours.Substring(0, 2) == "//");
//ici la ligne contient la première ligne de paramètre qui commence par #
string[] tabCSV = LigneEnCours.Split(';'); // on recupère un tableau de string avec chaque contenu entre ;
int NbChamps = tabCSV.GetUpperBound(0) ; // recherche nombre de colonnes dans le fichier
string[] tabVoie = new string[NbChamps];
string[] tabNom = new string[NbChamps];
string[] tabUnite = new string[NbChamps];
for (i = 0; i < NbChamps; i++) //Init des tableaux de paramètre avant lecture du fichier CSV
{
tabVoie[i] = "";
tabNom[i] = "";
tabUnite[i] = "";
}
while (LigneEnCours.Substring(0, 1) == "#") // On passe les paramètres (Ils commencent par "#")
{
if (LigneEnCours.Substring(0, 5) == "#Voie") tabVoie = LigneEnCours.Split(';'); // on recupère un tableau de string avec chaque contenu entre ;
if (LigneEnCours.Substring(0, 4) == "#Nom") tabNom = LigneEnCours.Split(';'); // on recupère un tableau de string avec chaque contenu entre ;
if (LigneEnCours.Substring(0, 6) == "#Unite") tabUnite = LigneEnCours.Split(';'); // on recupère un tableau de string avec chaque contenu entre ;
LigneEnCours = MonFichier.ReadLine(); // leture de la ligne suivante
}
//ici on a la ligne avec les min max
double TailleFichier = MonFichier.BaseStream.Length; //quelle est la taille du fichier ?
List<BinaryWriter> ListWriter = new List<BinaryWriter>(); //création d'une liste de binarywriter pour créer autant de fichiers qu'il y a de champ data dans le CSV
for (i=1 ; i<=NbChamps ; i++) //création des binarywriter
{
string fileName = CheminRepertoireTemporaire+ "\\" + i.ToString() + "-" + tabNom[i].ToString() + ".par";
BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)) ;
ListWriter.Add(writer);
}
while (MonFichier.BaseStream.Position < TailleFichier) // remplissage des binaryWriter, tant que je ne suis pas à la fin du fichier
{
string ligne = MonFichier.ReadLine(); // lecture d'une ligne
tabCSV = ligne.Split(';'); // récupération du tableau de string avec chaque contenu entre ;
string[] FormatsDates = new string[8] { "dd/MM/yyyy", "d/M/yyyy", "dd/MM/yyyy HH:mm:ss.fff", "d/M/yyyy HH:mm:ss.fff", "dd/MM/yyyy HH:mm:ss", "d/M/yyyy HH:mm:ss", "dd/MM/yyyy HH:mm", "d/M/yyyy HH:mm" };//formats possible de date
double date = DateTime.ParseExact(tabCSV[0], FormatsDates, null, System.Globalization.DateTimeStyles.None).ToOADate();
double LaValeur = 0;
for (i = 0; i < NbChamps; i++)
{
try
{
LaValeur = Convert.ToDouble(tabCSV[i+1], CultureInfo.InvariantCulture);
}
catch
{
break;
}
ListWriter[i].Write(date);
ListWriter[i].Write(LaValeur);
}
}
MonFichier.Close(); // On ferme le streamreader
for (i = 0; i < NbChamps; i++) // destruction des binarywriter
{
ListWriter[i].Close(); // fermeture
ListWriter[i].Dispose(); // libération des ressources
}
ListWriter.Clear(); |
Partager