随笔 - 24, 文章 - 0, 评论 - 1, 引用 - 0
数据加载中……

Using ADO to get data from Excel to Dataset in Monad

 

$excel = "$pwd\桌面\book1.xls" 

# Make the Connection : s
$ConnString 
= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=$excel;Extended Properties=Excel 8.0;" 
$Conn 
= new-object System.Data.OleDb.OleDbConnection($connString) 

# Open the Connection 
$conn.open() 

Get the available Tables 
$Tables 
= $Conn.GetOleDbSchemaTable([System.Data.OleDb.OleDbSchemaGuid]::tables,$null) 

# make a 
Command to get all data of the first table (Sheet) 
$cmd 
= new-object System.Data.OleDb.OleDbCommand("Select * from [$($tables.rows[0].TABLE_NAME)]",$Conn) 

# Load a DataAdapter 
with the Select Command
$da 
= new-object System.Data.OleDb.OleDbDataAdapter($cmd) 

# Fill a DataSet from the Data Adapter 
$ds 
= new-object System.Data.dataset 
$da.fill($ds) 

get the Tables 
$ds.tables | out
-null 

get all Row's where User is Mow1 
$ds.tables[0].select("user = 'Mow1'"

# Save this Dataset 
as XML Document: 
$sw 
= new-object system.io.streamwriter("$pwd\桌面\Puters.xml"
$ds.WriteXml($sw) 
$sw.close() 

# Load XML document into 
new dataset :   
$sr 
=  new-object system.io.streamreader("$pwd\桌面\Puters.xml"
$ds.ReadXml($sr)
$sr.close() 

$ds.tables 

 

摘自:mow001的blog

posted on 2009-04-14 10:32 nicktang 阅读(125) 评论(0)  编辑 收藏 引用 所属分类: Powershell

只有注册用户登录后才能发表评论。