public static bool FillOptions(CParametreLectureCSV parametre, string strFichierExemple)
        {
            CFormOptionsCSV1 form = new CFormOptionsCSV1();

            form.m_parametre         = parametre;
            form.m_strFichierExemple = strFichierExemple;

            DialogResult result = DialogResult.Retry;

            while (result == DialogResult.Retry)
            {
                result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    result = CFormOptionsCSV2.FillOptions(parametre, strFichierExemple);
                }
            }
            form.Dispose();
            return(result == DialogResult.OK);
        }
Exemple #2
0
        public static IParametreLectureFichier CreateParametreLectureFichier(ref string strFichier)
        {
            int nEtape = 0;
            IParametreLectureFichier parametre   = null;
            ETypeFichier             typeFichier = ETypeFichier.CSV;

            while (true)
            {
                switch (nEtape)
                {
                case 0:    //Sélection du fichier
                    OpenFileDialog dlg = new OpenFileDialog();
                    dlg.Filter = I.T("Text file(txt, csv)|*.txt;*.csv|Excel File|*.xls;*.xlsx|All files|*.*|20003");
                    if (dlg.ShowDialog() == DialogResult.Cancel)
                    {
                        return(null);
                    }
                    strFichier = dlg.FileName;
                    string strExtension = strFichier.Substring(strFichier.Length - 3, 3);
                    if (strExtension.ToUpper() == "XLS")
                    {
                        typeFichier = ETypeFichier.Excel;
                        parametre   = new CParametreLectureExcel();
                    }
                    else
                    {
                        typeFichier = ETypeFichier.CSV;
                        parametre   = new CParametreLectureCSV();
                    }
                    nEtape++;
                    break;

                case 1:    //Paramétrage de la lecture CSV
                    if (typeFichier == ETypeFichier.Excel)
                    {
                        if (!CFormOptionsImportExcel1.FillOptions((CParametreLectureExcel)parametre, strFichier))
                        {
                            nEtape--;
                        }
                        else
                        {
                            nEtape++;
                        }
                    }
                    else
                    {
                        if (!CFormOptionsCSV1.FillOptions((CParametreLectureCSV)parametre, strFichier))
                        {
                            nEtape--;
                        }
                        else
                        {
                            nEtape++;
                        }
                    }
                    break;

                case 2:
                    return(parametre);
                }
            }
            return(null);
        }
        public static DialogResult FillOptions(CParametreLectureCSV parametre, string strFichierExemple)
        {
            CFormOptionsCSV2 form = new CFormOptionsCSV2();

            form.m_parametre = parametre;
            string strTexteExemple = "";

            if (strFichierExemple != "")
            {
                StreamReader reader = null;
                try
                {
                    reader = new StreamReader(strFichierExemple, new CEncoding(parametre.Encodage).GetEncoding());
                    int nLigne = 0;
                    //string strLigne = reader.ReadLine();
                    string strLigne = parametre.GetCSVLine(reader);
                    form.m_tableExemple = new DataTable();
                    form.m_nomsCol      = new Dictionary <int, string>();
                    //CREATION DES COLONNES
                    if (parametre.NomChampsSurPremiereLigne)
                    {
                        string[] strChamps = strLigne.Split(parametre.Separateur);
                        int      nChamp    = 0;
                        foreach (string strChamp in strChamps)
                        {
                            string strVal = strChamp;
                            if (strVal.Trim() == "")
                            {
                                strVal = form.GetNomColonneDefaut(nChamp);
                            }
                            nChamp++;
                            int    nCpt        = 0;
                            string strChampTmp = strVal;
                            while (form.m_tableExemple.Columns.Contains(strChampTmp))
                            {
                                strChampTmp = strChamp + nCpt.ToString();
                                nCpt++;
                            }
                            form.m_tableExemple.Columns.Add(strChampTmp, typeof(string));
                            form.m_nomsCol.Add(nChamp, strChampTmp);
                        }
                        //strLigne = reader.ReadLine();
                        strLigne = parametre.GetCSVLine(reader);
                    }
                    else
                    {
                        string[] strCols = parametre.GetDatas(strLigne);
                        for (int nCol = 0; nCol < strCols.Length; nCol++)
                        {
                            string strChamp = "";
                            if (parametre.Mappage != null &&
                                parametre.Mappage.StringsA != null &&
                                parametre.Mappage.StringsA.Count >= strCols.Length)
                            {
                                strChamp = parametre.Mappage.StringsA[nCol];
                            }
                            else
                            {
                                form.GetNomColonneDefaut(nCol);
                            }

                            int    nCpt        = 0;
                            string strChampTmp = strChamp;
                            while (form.m_tableExemple.Columns.Contains(strChampTmp))
                            {
                                strChampTmp = strChamp + nCpt.ToString();
                                nCpt++;
                            }
                            form.m_tableExemple.Columns.Add(strChampTmp, typeof(string));
                            form.m_nomsCol.Add(nCol, strChampTmp);
                        }
                    }

                    //LECTURE DU FICHIER
                    while (strLigne != null && nLigne++ < 100)
                    {
                        string[] strDatas = parametre.GetDatas(strLigne);
                        int      nCol     = 0;
                        DataRow  row      = form.m_tableExemple.NewRow();
                        foreach (string strData in strDatas)
                        {
                            if (nCol < form.m_tableExemple.Columns.Count)
                            {
                                row[nCol] = strData;
                            }
                            nCol++;
                        }
                        form.m_tableExemple.Rows.Add(row);
                        //strLigne = reader.ReadLine();
                        strLigne = parametre.GetCSVLine(reader);
                    }


                    //CREATION DU LISTVIEW
                    form.m_wndListeExemple.Columns.Clear();
                    foreach (DataColumn col in form.m_tableExemple.Columns)
                    {
                        ColumnHeader header = new ColumnHeader();
                        header.Text = col.ColumnName;
                        form.m_wndListeExemple.Columns.Add(header);
                    }
                    foreach (DataRow row in form.m_tableExemple.Rows)
                    {
                        ListViewItem item = new ListViewItem(row[0].ToString());
                        for (int n = 1; n < form.m_tableExemple.Columns.Count; n++)
                        {
                            item.SubItems.Add(row[n].ToString());
                        }
                        form.m_wndListeExemple.Items.Add(item);
                    }


                    //CREATION DES COLONNES CSV DANS FICHIER PARAMETRAGE
                    for (int nCol = 0; nCol < form.m_tableExemple.Columns.Count; nCol++)
                    {
                        CParametreLectureCSV.CColonneCSV col = form.m_parametre.GetColonne(nCol);
                        if (col == null || col.Nom != form.m_tableExemple.Columns[nCol].ColumnName)
                        {
                            col          = new CParametreLectureCSV.CColonneCSV();
                            col.Nom      = form.m_tableExemple.Columns[nCol].ColumnName;
                            col.DataType = typeof(string);
                            form.m_parametre.SetColonne(nCol, col);
                        }
                    }


                    reader.Close();

                    form.m_strTexteExemple = strTexteExemple;
                    DialogResult result = form.ShowDialog();
                    form.Dispose();
                    return(result);
                }
                catch
                {
                }
                finally
                {
                    try
                    {
                        reader.Close();
                    }

                    catch
                    {
                    }
                }
            }
            return(DialogResult.Abort);
        }