ÐÂÊÖС°×ÓÃC# winform ¶ÁÈ¡Excel±íµÄʵÏÖ
Ò»¡¢½çÃ沿·Ö£º
Ê×ÏÈ£¬´ò¿ªvisual studioн¨ÏîÄ¿£»
È»ºóʹÓá°¹¤¾ßÏ䡱Ìí¼Ó¿Ø¼þ£º·Ö±ðÌí¼Óbutton£¬datagridview£¬textboxÈýÖÖ´°Ì壻
ËùµÃµ½µÄ½çÃæͼÈçͼËùʾ£º£¨¶à¼ÓÁ˼¸¸öÎı¾¿ò£©
¶þ¡¢´úÂ벿·Ö£º
1¡¢Ìí¼ÓÃüÃû¿Õ¼ä£º
using System.Data.OleDb; //ÃüÃû¿Õ¼ä
2¡¢Ìí¼ÓÀࣺ
//¸ù¾ÝexcleµÄ·¾¶°ÑµÚÒ»¸ösheelÖеÄÄÚÈÝ·ÅÈëdatatable public static DataTable ReadExcelToTable(string path)//excel´æ·ÅµÄ·¾¶ { try { //Á¬½Ó×Ö·û´® string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; // Office 07¼°ÒÔÉÏ°æ±¾ ²»ÄܳöÏÖ¶àÓàµÄ¿Õ¸ñ ¶øÇÒ·ÖºÅ×¢Òâ //string connstring = Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; //Office 07ÒÔÏ°汾 using (OleDbConnection conn = new OleDbConnection(connstring)) { conn.Open(); DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //µÃµ½ËùÓÐsheetµÄÃû×Ö string firstSheetName = sheetsName.Rows[0][2].ToString(); //µÃµ½µÚÒ»¸ösheetµÄÃû×Ö string sql = string.Format("SELECT * FROM [{0}]", firstSheetName); //²éѯ×Ö·û´® //string sql = string.Format("SELECT * FROM [{0}] WHERE [ÿÈÕÆÚ] is not null", firstSheetName); //²éѯ×Ö·û´® OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring); DataSet set = new DataSet(); ada.Fill(set); return set.Table【本文转自:韩国站群服务器 欢迎转载】s[0]; } } catch (Exception) { return null; } }
3¡¢ÔÚbuttonÖÐÌí¼Óµ÷ÓôúÂ룺
private void button1_Click(object sender, EventArgs e) { string folder_path = this.textBox1.Text.Trim(); dataGridView1.DataSource = null; //ÿ´Î´ò¿ªÇå¿ÕÄÚÈÝ DataTable dt = ReadExcelToTable(folder_path); dataGridView1.DataSource = dt; }
Íê±Ï£¡
µã»÷ÔËÐУ¬È»ºóÊäÈëExcelÎļþµÄ·¾¶£¬µã»÷button°´Å¥¡££¨ÕâÀïµÄ·¾¶£ºÂ·¾¶+ExcelÎļþÃû£©
µ½´ËÕâƪ¹ØÓÚÐÂÊÖС°×ÓÃC# winform ¶ÁÈ¡Excel±íµÄʵÏÖµÄÎÄÕ¾ͽéÉܵ½ÕâÁË,¸ü¶àÏà¹ØC# winform ¶ÁÈ¡ExcelÄÚÈÝÇëËÑË÷½Å±¾Ö®¼ÒÒÔÇ°µÄÎÄÕ»ò¼ÌÐøä¯ÀÀÏÂÃæµÄÏà¹ØÎÄÕÂÏ£Íû´ó¼ÒÒÔºó¶à¶àÖ§³Ö½Å±¾Ö®¼Ò£¡
【原创作者:http://www.1234xp.com/jap.html 转载请说明出处】