有更快的方法来解析一个使用Powershell的Excel文档吗? [英] Is there a faster way to parse an excel document with Powershell?

查看:122
本文介绍了有更快的方法来解析一个使用Powershell的Excel文档吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 Powershell MS Excel 文档连接。每个excel文档都有大约1000行数据的可能性。

I'm interfacing with an MS Excel document via Powershell. There is a possibility of each excel document of having around 1000 rows of data.

目前,此脚本似乎读取 Excel 文件,并以每秒1个记录的速度写入一个值.6秒。乍一看似乎非常慢。

Currently this script seems to read the Excel file and write a value to screen at a rate of 1 record every .6 seconds. At first glance that seems extremely slow.

这是我第一次阅读 Excel 文件与 Powershell ,这是规范吗?有没有更快的方法来阅读和解析 Excel 数据?

This is my first time reading an Excel file with Powershell, is this the norm? Is there a faster way for me to read and parse the Excel data?

这是脚本输出(修剪为了可读性)

Here is the script output (trimmed for readability)

PS P:\Powershell\ExcelInterfaceTest> .\WRIRMPTruckInterface.ps1 test.xlsx
3/20/2013 4:46:01 PM
---------------------------
2   078110
3   078108
4   078107
5   078109
<SNIP>
242   078338
243   078344
244   078347
245   078350
3/20/2013 4:48:33 PM
---------------------------
PS P:\Powershell\ExcelInterfaceTest>

这里是 Powershell 脚本: p>

Here is the Powershell script:

########################################################################################################
# This is a common function I am using which will release excel objects
########################################################################################################
function Release-Ref ($ref) {
    ([System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) -gt 0)
    [System.GC]::Collect()
    [System.GC]::WaitForPendingFinalizers()
}

########################################################################################################
# Variables
########################################################################################################

########################################################################################################
# Creating excel object
########################################################################################################
$objExcel = new-object -comobject excel.application 

# Set to false to not open the app on screen.
$objExcel.Visible = $False

########################################################################################################
# Directory location where we have our excel files
########################################################################################################
$ExcelFilesLocation = "C:/ShippingInterface/" + $args[0]

########################################################################################################
# Open our excel file
########################################################################################################
$UserWorkBook = $objExcel.Workbooks.Open($ExcelFilesLocation) 

########################################################################################################
# Here Item(1) refers to sheet 1 of of the workbook. If we want to access sheet 10, we have to modify the code to Item(10)
########################################################################################################
$UserWorksheet = $UserWorkBook.Worksheets.Item(2)

########################################################################################################
# This is counter which will help to iterrate trough the loop. This is simply a row counter
# I am starting row count as 2, because the first row in my case is header. So we dont need to read the header data
########################################################################################################
$intRow = 2

$a = Get-Date
write-host $a
write-host "---------------------------"

Do {

    # Reading the first column of the current row
    $TicketNumber = $UserWorksheet.Cells.Item($intRow, 1).Value()

    write-host $intRow " " $TicketNumber    

    $intRow++

} While ($UserWorksheet.Cells.Item($intRow,1).Value() -ne $null)

$a = Get-Date
write-host $a
write-host "---------------------------"

########################################################################################################
# Exiting the excel object
########################################################################################################
$objExcel.Quit()

########################################################################################################
#Release all the objects used above
########################################################################################################
$a = Release-Ref($UserWorksheet)
$a = Release-Ref($UserWorkBook) 
$a = Release-Ref($objExcel)


推荐答案

是静态的(不涉及公式,只是单元格中的数据),您可以将电子表格作为ODBC数据源访问,并对其执行SQL(或至少类似于SQL的)查询。请查看此参考以设置您的连接字符串(工作簿中的每个工作表将是此练习的表),并使用 System.Data 来查询它,就像常规数据库一样(Don Jones写了一个包装功能,这可能有所帮助)。

If the data is static (no formulas involved, just data in cells), you can access the spreadsheet as an ODBC data source and execute SQL (or at least SQL-like) queries against it. Have a look at this reference for setting up your connectionstring (each worksheet in a workbook will be a "table" for this exercise), and use System.Data to query it the same as you would a regular database (Don Jones wrote a wrapper function for this which may help).

这个应该比/启动Excel&单元格选择。

This should be faster than launching Excel & picking through cell by cell.

这篇关于有更快的方法来解析一个使用Powershell的Excel文档吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆