Tôi đang giao tiếp với tài liệu MS Excel
qua Powershell
. Có khả năng mỗi tài liệu excel có khoảng 1000 hàng dữ liệu.Có cách nào nhanh hơn để phân tích tài liệu excel bằng Powershell không?
Hiện tại, tập lệnh này có vẻ như đọc tệp Excel
và viết giá trị cho màn hình ở tốc độ 1 bản ghi sau mỗi 6 giây. Thoạt nhìn có vẻ rất chậm.
Đây là lần đầu tiên tôi đọc tệp Excel
với Powershell
, đây có phải là tiêu chuẩn không? Có cách nào nhanh hơn để tôi đọc và phân tích cú pháp dữ liệu Excel
không?
Đây là kết quả kịch bản (tỉa để có thể đọc)
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>
Đây là Powershell
kịch bản:
########################################################################################################
# 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)
Đó là rất thú vị, tôi sẽ cho nó một shot vào ngày mai và xem cách nó thực hiện. – ProfessionalAmateur
Điều này thật tuyệt. Insanely nhanh hơn. – ProfessionalAmateur