Exemple #1
0
        /// <summary>
        /// 处理文本文件
        /// </summary>
        /// <param name="fPath"></param>
        private IList <ImportMobileInfo> processTXT(string fPath)
        {
            //读文件
            StreamReader fileStream = new StreamReader(fPath, Encoding.Default);
            string       s          = fileStream.ReadToEnd();

            fileStream.Close();

            IList <ImportMobileInfo> mobiles = new List <ImportMobileInfo>();

            s = s.Replace("\t", "~@~").Replace(" ", "~@~").Replace("\r\n", "~||~");
            s = Regex.Replace(s, @"(~@~)+", "~@~");
            string[] separatorRow = { "~||~" };
            string[] separatorCol = { "~@~" };
            string[] arrRow       = s.Split(separatorRow, StringSplitOptions.RemoveEmptyEntries);

            foreach (string row in arrRow)
            {
                string[] arrCol = row.Split(separatorCol, StringSplitOptions.RemoveEmptyEntries);

                if (arrCol.Length < 1)
                {
                    continue;
                }

                try
                {
                    ImportMobileInfo mobileInfo = new ImportMobileInfo();
                    mobileInfo.Mobile = arrCol[0].ToString();

                    if (arrCol.Length > 1)
                    {
                        mobileInfo.CompanyName = arrCol[1].ToString();
                    }

                    if (arrCol.Length > 2)
                    {
                        mobileInfo.FullName = arrCol[2].ToString();
                    }

                    mobiles.Add(mobileInfo);
                }
                catch { }
            }

            return(mobiles);
        }
Exemple #2
0
        /// <summary>
        /// 获取EXCEL表里的数据
        /// </summary>
        /// <param name="fPath"></param>
        /// <param name="tableName"></param>
        /// <param name="mobiles"></param>
        /// <returns></returns>
        private IList <ImportMobileInfo> GetExcelTableData(string fPath, string tableName, IList <ImportMobileInfo> mobiles)
        {
            string          strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fPath + ";" + "Extended Properties=Excel 8.0;";
            OleDbConnection conn    = new OleDbConnection(strConn);

            conn.Open();
            string           strExcel  = "";
            OleDbDataAdapter myCommand = null;
            DataSet          ds        = null;

            strExcel  = "select * from [" + tableName + "]";
            myCommand = new OleDbDataAdapter(strExcel, strConn);
            ds        = new DataSet();
            myCommand.Fill(ds, "table1");
            myCommand.Dispose();
            conn.Close();
            conn.Dispose();

            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];

                //第一个工作表时取列名
                if (mobiles.Count == 0)
                {
                    ImportMobileInfo mobileInfo = new ImportMobileInfo();

                    if (dt.Columns.Count > 0)
                    {
                        mobileInfo.Mobile = dt.Columns[0].ColumnName;
                    }

                    if (dt.Columns.Count > 1)
                    {
                        mobileInfo.CompanyName = dt.Columns[1].ColumnName;
                    }

                    if (dt.Columns.Count > 2)
                    {
                        mobileInfo.FullName = dt.Columns[2].ColumnName;
                    }

                    mobiles.Add(mobileInfo);
                }

                foreach (DataRow dr in dt.Rows)
                {
                    ImportMobileInfo mobileInfo = new ImportMobileInfo();

                    if (dr.ItemArray.Length > 0)
                    {
                        mobileInfo.Mobile = dr[0].ToString();
                    }

                    if (dr.ItemArray.Length > 1)
                    {
                        mobileInfo.CompanyName = dr[1].ToString();
                    }

                    if (dr.ItemArray.Length > 2)
                    {
                        mobileInfo.FullName = dr[2].ToString();
                    }

                    if (!(string.IsNullOrEmpty(mobileInfo.Mobile) && string.IsNullOrEmpty(mobileInfo.CompanyName) && string.IsNullOrEmpty(mobileInfo.FullName)))
                    {
                        mobiles.Add(mobileInfo);
                    }
                }
            }

            return(mobiles);
        }