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中的所有栏位名称:
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 }

浙公网安备 33010602011771号