using System; using System.Data; using System.Data.OleDb; using System.Windows.Forms; private void button2_Click(object sender, EventArgs e) { try { string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=D:\\723\\文档\\客户.xlsx;Extended Properties=Excel 8.0"; //string strCon = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = D:\\0723\\文档\\客户.xlsx;Extended Properties=Excel 8.0"; OleDbConnection myConn = new OleDbConnection(strCon); string strCom = "SELECT * FROM [Sheet1$] "; myConn.Open(); OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn); DataSet myDataSet = new DataSet(); myCommand.Fill(myDataSet, "[Sheet1$]"); myConn.Close(); for (int i = 0; i < myDataSet.Tables[0].Rows.Count; i++) { if (!string.IsNullOrEmpty(myDataSet.Tables[0].Rows[i]["CODE"].ToString())) { string code = ""; string name = ""; code = "0,'" + myDataSet.Tables[0].Rows[i]["CODE"] + "',"; name = "'" + myDataSet.Tables[0].Rows[i]["NAME"] + "') "; } } string ss = ""; } catch (Exception ex) { } }
需要读取Excel:
读取出的结果集: