示例#1
0
        public CResultAErreur InitEntetes()
        {
            CResultAErreur result = CResultAErreur.True;

            if (m_fichierExcelOleDb == null)
            {
                result.EmpileErreur("");
                return(result);
            }

            string strExcelCol  = m_expCol.Match(m_strPremiereCelluleHG).ToString();
            string strExcelLine = m_expLine.Match(m_strPremiereCelluleHG).ToString();

            string strTitreColonne = "";

            // Récupèration des colonnes et construit le dictionnaire NomDeColonne / EnteteExcel
            // Util pour les fonctions GetFirstLine et GetNextLine
            do
            {
                strTitreColonne = string.Empty;
                object cellValue = m_fichierExcelOleDb.GetValue(strExcelCol, strExcelLine);
                if (cellValue != null)
                {
                    strTitreColonne = cellValue.ToString();
                }

                if (strTitreColonne != string.Empty)
                {
                    m_dicColHeaderColName.Add(strTitreColonne, strExcelCol);
                }
                strExcelCol = CUtilExcel.GetNextColumnHeader(strExcelCol);
            } while (strTitreColonne != string.Empty);

            return(result);
        }
示例#2
0
        /// <summary>
        /// Recherche la référence de la première cellule trouvée contenant la valeur indiquée
        /// Se limite dans la recherhce aux nMaxLine premières lignes et nMaxCol premières colonnes
        /// </summary>
        /// <param name="strValeurAttendue"></param>
        /// <returns></returns>
        public string GetFirstCellForValue(string strValeurAttendue, int nMaxCol, int nMaxLine)
        {
            if (m_typeFichier == ETypeFichierExcel.OleDb)
            {
                for (int l = 1; l < nMaxLine; l++)
                {
                    // Commence en A1
                    string cColonne = "A";

                    for (int c = 0; c < nMaxCol; c++)
                    {
                        string strCell = cColonne + l.ToString();
                        object valeur  = m_fichierExcelOleDb.GetValue(strCell);

                        if (valeur != null && valeur.ToString() == strValeurAttendue)
                        {
                            return(strCell);
                        }

                        cColonne = CUtilExcel.GetNextColumnHeader(cColonne);
                    }
                }
            }

            return("");
        }
示例#3
0
        //---------------------------------------------------------------------------
        public DataTable GetTable(bool bNomChampsSurPremiereLigne)
        {
            DataTable dt = new DataTable("EXCEL_TABLE");

            if (m_typeFichier == ETypeFichierExcel.OleDb && m_fichierExcelOleDb != null)
            {
                m_fichierExcelOleDb.Headers = bNomChampsSurPremiereLigne;
                return(m_fichierExcelOleDb.GetTable());
            }

            // Construction des colonnes
            object[] entetes = GetFirstLine();
            if (bNomChampsSurPremiereLigne)
            {
                foreach (object titreCol in entetes)
                {
                    dt.Columns.Add(titreCol.ToString());
                }
            }
            else
            {
                string titre = "A";
                foreach (object titreCol in entetes)
                {
                    dt.Columns.Add(titre);
                    titre = CUtilExcel.GetNextColumnHeader(titre);
                }
            }

            // Remplissage des lignes

            object[] datas = GetFirstLine();
            if (bNomChampsSurPremiereLigne)
            {
                datas = GetNextLine();
            }

            int index = 0;

            while (datas != null && datas.Length > 0)
            {
                index = 0;
                DataRow firstRow = dt.Rows.Add();
                foreach (DataColumn colonne in dt.Columns)
                {
                    try
                    {
                        object data = datas[index++];
                        if (data != null)
                        {
                            firstRow[colonne] = data;
                        }
                        else
                        {
                            firstRow[colonne] = "";
                        }
                    }
                    catch
                    {
                        firstRow[colonne] = "";
                    }
                }

                datas = GetNextLine();
            }


            return(dt);
        }