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標準 
        }
相關文章
相關標籤/搜索