C#用oledb讀取Excel數據

string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + v_strExcelPath + ";Extended Properties='Excel 12.0;IMEX=1'";

            OleDbDataAdapter myDataAdapter = null;
            OleDbConnection conn = new OleDbConnection(strConn);
            OleDbCommand myOleDbCommand = new OleDbCommand("SELECT * FROM [" + v_strSheetName + "$]", conn);

            try
            {
                conn.Open();
                myDataAdapter = new OleDbDataAdapter(myOleDbCommand);
                myDataAdapter.Fill(dt);
                conn.Dispose();
            }


讀取Excel中的全部欄位名稱:ide

 1 ArrayList lv_arrList = new ArrayList();
 2             string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + v_strExcelPath + ";Extended Properties='Excel 12.0;IMEX=1'";
 3             OleDbConnection conn = new OleDbConnection(strConn);
 4             try
 5             {               
 6                 conn.Open();
 7                 DataTable sheetname = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
 8                 conn.Close();
 9                 conn.Dispose();
10                 foreach (DataRow dr in sheetname.Rows)
11                 {
12                     lv_arrList.Add(dr[2].ToString().Replace("$", "").Replace("'", ""));
13                 }
14             }
相關文章
相關標籤/搜索