大家好,又见面了,我是你们的朋友全栈君。
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
转载于:https://www.cnblogs.com/hongjiumu/archive/2013/03/02/2940303.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/144206.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...