C#读取EXCEL 并输出到文本框

 #region 加载数据到DataGraidView
        private void button1_Click(object sender, EventArgs e)
        {
            string excelPath = textBox1.Text.Trim();
            //string excelPath = "测试.xlsx"; //文件路径
            string fileExt = Path.GetExtension(excelPath);//获得文件扩展名
            string conn = "";
            if (fileExt == ".xls")
            {
                conn = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + excelPath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
            }
            else
            {
                conn = "Provider = Microsoft.ACE.OLEDB.12.0 ; Data Source =" + excelPath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'";
            }

            OleDbConnection con = new OleDbConnection(conn);
            DataTable dt = new DataTable();
            //OleDbDataAdapter da = new OleDbDataAdapter("select * from [Admin$]", con); //查询表名文件Admin的数据表
            OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con); //查询表名文件Admin的数据表

            con.Open();
            da.Fill(dt); //填充到dt
            con.Close();
            dataGridView1.DataSource = dt;

            //foreach (DataRow dr in dt.Rows)
            //{
            //    MessageBox.Show(dr["管理员编号"].ToString());
            //}
            MessageBox.Show("读取完成!");
        }
        #endregion

        #region 遍历dataGridview 输出到文本框 
        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
               string A1=  dataGridView1.Rows[i].Cells["A1"].Value.ToString();
               string A2= dataGridView1.Rows[i].Cells["A2"].Value.ToString();            
                textBox2.Text += "fwgcs" + ".Add(\""+A1+ "\");//" + A2+ "\r\n";
            }
            MessageBox.Show("添加到集合");
        }
        #endregion

 

      #region 提取生日
        private void button2_Click(object sender, EventArgs e)
        {
            textBox2.Text = "";
            //遍历gridvie;
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {

                string s = dataGridView1.Rows[i].Cells["身份证"].Value.ToString();
                //int x = int.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
                // s = s.Substring(0, s.Length - 1);
                if (0 < s.Length && s.Length < 18)
                {
                    MessageBox.Show(i.ToString() + "]" + s);
                }
                if (s.Length >= 18)
                {
                    s = s.Substring(6, 8).Insert(4, "-").Insert(7, "-");

                }

                textBox2.Text += s + "\r\n";
            }
        }
        #endregion

 

        #region 发送post请求
        public static string Post(string str)
        {

            string result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:8563/BaseInfo/PersonInformation/IndexAdd");
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";

            byte[] data = Encoding.UTF8.GetBytes(str);//把字符串转换为字节

            req.ContentLength = data.Length; //请求长度

            using (Stream reqStream = req.GetRequestStream()) //获取
            {
                reqStream.Write(data, 0, data.Length);//向当前流中写入字节
                reqStream.Close(); //关闭当前流
            }

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); //响应结果
            Stream stream = resp.GetResponseStream();
            //获取响应内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //发送
            for (int i=0;i<list.Count;i++)
            {
               textBox2.Text+= Post(list[i])+"\r\n";
            }
        }
        #endregion

 

        /// <summary>  
        /// 18位身份证号码验证  
        /// </summary>  
        private bool CheckIDCard18(string idNumber)
        {
            long n = 0;
            if (long.TryParse(idNumber.Remove(17), out n) == false
                || n < Math.Pow(10, 16) || long.TryParse(idNumber.Replace('x', '0').Replace('X', '0'), out n) == false)
            {
                return false;//数字验证  
            }
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.IndexOf(idNumber.Remove(2)) == -1)
            {
                return false;//省份验证  
            }
            string birth = idNumber.Substring(6, 8).Insert(6, "-").Insert(4, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
            {
                return false;//生日验证  
            }
            string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
            string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
            char[] Ai = idNumber.Remove(17).ToCharArray();
            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
            }
            int y = -1;
            Math.DivRem(sum, 11, out y);
            if (arrVarifyCode[y] != idNumber.Substring(17, 1).ToLower())
            {
                return false;//校验码验证  
            }
            return true;//符合GB11643-1999标准  
        }

 

posted @ 2018-03-07 15:51  enych  阅读(855)  评论(0编辑  收藏  举报