简体   繁体   English

使用Powershell从Excel查询数据的网站

[英]Query a website with data from Excel using Powershell

I want to use a powershell script to get data from an excel sheet to query an online database 我想使用Powershell脚本从Excel工作表中获取数据以查询在线数据库

I used this code to open the spreadsheet from powershell 我使用此代码从Powershell打开电子表格

$xl = New-Object -COM "Excel.Application"
$xl.Visible = $true
$wb = $xl.Workbooks.Open("C:\Documents\Book4.xlsx")
$ws = $wb.Sheets.Item(1)

But I can't really work on the spreadsheet ie reference cells. 但是我不能真正处理电子表格,即参考单元。 What I need is to select contents of individual cells, store in a variable and use to populate a query for an online database. 我需要选择单个单元格的内容,将其存储在变量中,然后用于填充在线数据库的查询。

I used this code to access the website url 我使用此代码访问网站网址

$IE=new-object -com internetexplorer.application
$IE.navigate2("http://www.google.com")
$IE.visible=$true

But I've not been able to fill the form or query the database. 但是我无法填写表格或查询数据库。

Please how can I do this using powershell? 请我如何使用Powershell做到这一点? Thanks 谢谢

Use the $ws.Columns 使用$ ws.Columns

Example (get the text in column2, row 2): 示例(在第2列第2行中获取文本):

$query = $ws.Columns[2].Rows[2].Text

Then you could use and navigate to the google using the query string, example: 然后,您可以使用查询字符串使用并导航到Google,例如:

$IE.navigate2(("http://www.google.com?q={0}" -f $query))

You could also use the Range to fetch the cells (if multiple) and select it's text. 您也可以使用Range来获取单元格(如果有多个单元格)并选择其文本。

Example (get cells between A2-A99, and select the text): 示例(获取A2-A99之间的单元格,然后选择文本):

$queries = $ws.Range("A2","A99")|select Text

Then you will have an array with the queries in mind if you want to iterate over them. 然后,如果要遍历查询,将有一个考虑了查询的数组。

Thank you so much guys. 十分感谢大家。

This code works well for google although you might have to do some formatting to the query when the browser launches 尽管您可能需要在浏览器启动时对查询进行某种格式设置,但是此代码对于Google来说效果很好

#To open spreadsheet
$xl = New-Object -COM "Excel.Application" 
$xl.Visible = $true
$wb = $xl.Workbooks.Open("C:\Users\Documents\Book4.xlsx")
$ws = $wb.Sheets.Item("Team Sheet") #Incase of multiple sheets
$queries = $ws.Range("D9")|select Text #specify cell use ("D9", "D99") for range 
#$queries 

#To open urls
$IE=new-object -com internetexplorer.application
$IE.navigate2(("http://www.google.com?q={0}" -f $queries))
$IE.visible=$true

Just in case anyone needs it 万一有人需要

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM