oleDbCommand访问Excel:
_oleCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$A2:A5]", _oleConn);
objValue = _oleCmdSelect.ExecuteScalar();
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { private static string _vsPath = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\work\0228\test\Solution2\ReadExcel07\Members.xlsx;Extended Properties=""Excel 12.0;HDR=YES;"""; static void Main(string[] args) { System.Data.OleDb.OleDbConnection vOleDbConnection = new System.Data.OleDb.OleDbConnection(_vsPath); vOleDbConnection.Open(); System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter=new System.Data.OleDb.OleDbDataAdapter(); oleDbDataAdapter.SelectCommand = new System.Data.OleDb.OleDbCommand("SELECT * FROM [Sheet1$A2:A5]",vOleDbConnection); System.Data.DataTable dataTable=new System.Data.DataTable(); oleDbDataAdapter.FillSchema(dataTable,System.Data.SchemaType.Source); oleDbDataAdapter.Fill(dataTable); oleDbDataAdapter.Dispose(); Console.WriteLine("Over"); Console.Read(); } } }
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { private static string _vsPath = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\work\0228\test\Solution2\ReadExcel07\Members.xlsx;Extended Properties=""Excel 12.0;HDR=YES;"""; static void Main(string[] args) { System.Data.OleDb.OleDbConnection vOleDbConnection = new System.Data.OleDb.OleDbConnection(_vsPath); vOleDbConnection.Open(); System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter = new System.Data.OleDb.OleDbDataAdapter(); oleDbDataAdapter.SelectCommand = new System.Data.OleDb.OleDbCommand("SELECT * FROM [Sheet1$]", vOleDbConnection); System.Data.DataTable dataTable = new System.Data.DataTable(); oleDbDataAdapter.FillSchema(dataTable, System.Data.SchemaType.Source); oleDbDataAdapter.Fill(dataTable); oleDbDataAdapter.Dispose(); System.Data.OleDb.OleDbCommand oleDbCommand = new System.Data.OleDb.OleDbCommand("INSERT INTO [Sheet1$] ([Full Name],[ID Number]) values ('F1','F2')", vOleDbConnection); oleDbCommand.ExecuteNonQuery(); Console.WriteLine("Over"); Console.Read(); } } }
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdataadapter.insertcommand.aspx