我獲得以下錯誤:轉換XLSX到CSV沒有使用Excel
Cannot index into a null array. At C:\tmp\Folder\excel\output\net45\test.ps1:14 char:1 + $Data = $Reader.AsDataSet().Tables[0].Rows + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : NullArray
# Zero based index. The second row has index 1.
$StartRow = 2
# Input File
$InputFileName = "C:\tmp\Folder\excel\output\net20\test.xlsx"
# Output File
$OutputFileName = "C:\tmp\Folder\excel\output\net20\SomeFile.csv"
# Path to Excel.dll is saved (downloaded from http://exceldatareader.codeplex.com/)
$DllPath = "C:\tmp\Folder\excel\output\net45\Excel.4.5.dll"
[void]([Reflection.Assembly]::LoadFrom($DllPath))
$Stream = New-Object IO.FileStream($InputFileName, "Open", "Read")
$Reader = [Excel.ExcelReaderFactory]::CreateBinaryReader($Stream)
$Data = $Reader.AsDataSet().Tables[0].Rows
# Read the column names. Order should be preserved
$Columns = $Data[$StartRow].ItemArray
# Sort the remaining data into an object using the specified columns
$Data[$($StartRow + 1)..$($Data.Count - 1)] | % {
# Create an object
$Output = New-Object Object
# Read each column
for ($i = 0; $i -lt $Columns.Count; $i++) {
$Output | Add-Member NoteProperty $Columns[$i] $_.ItemArray[$i]
}
# Leave it in the output pipeline
$Output
} | Export-CSV $OutputFileName -NoType
你有什麼試過這個嗎?通常預期StackOverflow中的問題顯示出實際代碼方面的一些努力。所以,也許可以自己嘗試一些編程,然後詢問有關您遇到問題的零件的具體問題。考慮擱置幾分鐘來看看[關於提問的一些指導方針](http://stackoverflow.com/help/how-to-ask)。 –
我認爲加載DLL文件不同於在你的代碼中使用:$ E = New-Object -ComObject Excel.Application。由於Excel.Application打開Excel。 –
爲什麼「不使用excel」很重要? –