Ejemplo n.º 1
0
 public void SetColonne(int nNumColonne, CColonneCSV colonne)
 {
     if (colonne != null)
     {
         m_tableColonnes[nNumColonne] = colonne;
     }
     else
     {
         m_tableColonnes.Remove(nNumColonne);
     }
 }
Ejemplo n.º 2
0
        public CResultAErreur Serialize(C2iSerializer serializer)
        {
            int            nVersion = GetNumVersion();
            CResultAErreur result   = serializer.TraiteVersion(ref nVersion);

            if (!result)
            {
                return(result);
            }

            string strTmp = m_strSeparateur + "";

            serializer.TraiteString(ref strTmp);
            if (strTmp.Length > 0)
            {
                m_strSeparateur = strTmp[0];
            }

            serializer.TraiteString(ref m_strIndicateurTexte);

            serializer.TraiteBool(ref m_bNomChampsSurPremiereLigne);

            strTmp = m_strSeparateurDecimales + "";
            serializer.TraiteString(ref strTmp);
            if (strTmp.Length > 0)
            {
                m_strSeparateurDecimales = strTmp[0];
            }

            int nNbTypes = m_tableColonnes.Count;

            serializer.TraiteInt(ref nNbTypes);

            //serializer.TraiteObject(ref m_mappage, new object[] { });
            switch (serializer.Mode)
            {
            case ModeSerialisation.Ecriture:
                foreach (DictionaryEntry entry in m_tableColonnes)
                {
                    int         nNum = (int)entry.Key;
                    CColonneCSV col  = (CColonneCSV)entry.Value;
                    serializer.TraiteInt(ref nNum);
                    result = col.Serialize(serializer);
                    if (!result)
                    {
                        return(result);
                    }
                }

                break;

            case ModeSerialisation.Lecture:
                m_tableColonnes.Clear();
                for (int nEntry = 0; nEntry < nNbTypes; nEntry++)
                {
                    int         nNum = 0;
                    CColonneCSV col  = new CColonneCSV();
                    serializer.TraiteInt(ref nNum);
                    result = col.Serialize(serializer);
                    if (!result)
                    {
                        return(result);
                    }
                    m_tableColonnes[nNum] = col;
                }
                break;
            }

            if (nVersion > 0)
            {
                result = m_mappage.Serialize(serializer);
            }
            if (nVersion > 1)
            {
                serializer.TraiteBool(ref m_bValeurNulleSiErreur);
            }
            if (nVersion >= 3)
            {
                int nTmp = (int)m_encoding;
                serializer.TraiteInt(ref nTmp);
                m_encoding = (EEncoding)nTmp;
            }


            if (!result)
            {
                return(result);
            }
            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Importe un fichier. Le data du result contient un datatable
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        public CResultAErreur LectureFichier(string strFichier)
        {
            CResultAErreur result = CResultAErreur.True;

            StreamReader reader = new StreamReader(strFichier, new CEncoding(m_encoding).GetEncoding());

            DataTable table = new DataTable();

            try
            {
                //string strLigne = reader.ReadLine();
                string   strLigne = GetCSVLine(reader);
                string[] strDatas = strLigne.Split(m_strSeparateur);
                int      nCol     = 1;
                int      nLigne   = 1;

                //Creation des colonnes
                Dictionary <DataColumn, CColonneCSV> mappageCols = new Dictionary <DataColumn, CColonneCSV>();
                foreach (string strData in strDatas)
                {
                    //Nom Table
                    string      strNom = "Column " + nCol.ToString();
                    CColonneCSV colCSV = (CColonneCSV)m_tableColonnes[nCol - 1];
                    if (NomChampsSurPremiereLigne)
                    {
                        strNom = strData;
                    }
                    if (colCSV != null && colCSV.Nom != "")
                    {
                        strNom = colCSV.Nom;
                    }

                    int nIndex = 0;
                    while (table.Columns[strNom] != null)
                    {
                        nIndex++;
                        strNom = strNom + "_" + nIndex.ToString();
                    }

                    Type tp = typeof(string);
                    if (colCSV != null && colCSV.DataType != null)
                    {
                        tp = colCSV.DataType;
                    }
                    DataColumn col = new DataColumn(strNom, tp);
                    mappageCols.Add(col, colCSV);
                    table.Columns.Add(col);
                    col.AllowDBNull = true;
                    nCol++;
                }
                if (NomChampsSurPremiereLigne)
                {
                    //strLigne = reader.ReadLine();
                    strLigne = GetCSVLine(reader);
                    nLigne++;
                }

                //Lecture Table
                while (strLigne != null)
                {
                    if (strLigne.Trim().Length > 0)
                    {
                        strDatas = GetDatas(strLigne);
                        nCol     = 0;
                        DataRow row = table.NewRow();
                        foreach (string strData in strDatas)
                        {
                            if (nCol < table.Columns.Count)
                            {
                                DataColumn  col    = table.Columns[nCol];
                                CColonneCSV colCSV = mappageCols[col];
                                try
                                {
                                    if (colCSV != null)
                                    {
                                        if (colCSV.HasNullMapping)
                                        {
                                            if (colCSV.NullCaseSensitive)
                                            {
                                                if (strData == colCSV.NullMapping)
                                                {
                                                    row[col] = DBNull.Value;
                                                    nCol++;
                                                    continue;
                                                }
                                            }
                                            else if (strData.ToUpper() == colCSV.NullMapping.ToUpper())
                                            {
                                                row[col] = DBNull.Value;
                                                nCol++;
                                                continue;
                                            }
                                        }
                                    }

                                    if (col.DataType == typeof(string))
                                    {
                                        row[col] = strData;
                                    }

                                    else if (col.DataType == typeof(double) || col.DataType == typeof(int) ||
                                             col.DataType == typeof(float))
                                    {
                                        double fVal = CUtilDouble.DoubleFromString(strData);
                                        if (col.DataType == typeof(int))
                                        {
                                            row[col] = (int)fVal;
                                        }
                                        if (col.DataType == typeof(float))
                                        {
                                            row[col] = (float)fVal;
                                        }
                                        else
                                        {
                                            row[col] = fVal;
                                        }
                                    }
                                    else if (col.DataType == typeof(DateTime))
                                    {
                                        DateTime dt = CUtilDate.DateFromString(strData);
                                        row[col] = dt;
                                    }
                                    else if (col.DataType == typeof(bool))
                                    {
                                        string valBool = strData.ToUpper();
                                        bool   val     = (valBool == "1" || valBool == "O" || valBool == "OUI" || valBool == "Y" || valBool == "YES" || valBool == "TRUE");
                                        row[col] = val;
                                    }
                                    else
                                    {
                                        object val = Convert.ChangeType(strData, col.DataType);
                                        row[col] = val;
                                    }
                                }
                                catch
                                {
                                    if (ValeurNullSiErreur)
                                    {
                                        row[col] = DBNull.Value;
                                    }
                                    else
                                    {
                                        result.EmpileErreur(I.T("Conversion error : line @1 column @2 : the value cannot be converted into @3|30003"
                                                                , nLigne.ToString(), col.ColumnName, DynamicClassAttribute.GetNomConvivial(col.DataType)));
                                        return(result);
                                    }
                                }
                            }
                            nCol++;
                        }
                        table.Rows.Add(row);
                    }
                    //strLigne = reader.ReadLine();
                    strLigne = GetCSVLine(reader);
                    nLigne++;
                }
                result.Data = table;
                return(result);
            }
            catch (Exception e)
            {
                result.EmpileErreur(new CErreurException(e));
                return(result);
            }
            finally
            {
                try
                {
                    reader.Close();
                }
                catch { }
            }
        }