为什么PowerShell不想将我的文件保存为CSV

$Path = 'D:/ETL_Data/TwitchTVData.xlsx' $csvPath = 'D:/ETL_Data/TwitchTVData2.csv' # Open the Excel document and pull in the 'Sheet1' worksheet $Excel = New-Object -Com Excel.Application $Workbook = $Excel.Workbooks.Open($Path) $page = 'Sheet1' $ws = $Workbook.Worksheets | Where-Object {$_.Name -eq $page} $Excel.Visible = $true $Excel.DisplayAlerts = $false # Set variables for the worksheet cells, and for navigation $cells = $ws.Cells $row = 1 $col = 4 $formula = @" =NOW() "@ # Add the formula to the worksheet $range = $ws.UsedRange $rows = $range.Rows.Count for ($i=0; $i -ne $rows; $i++) { $cells.Item($row, $col) = $formula $row++ } $ws.Columns.Item("A:D").EntireColumn.AutoFit() | Out-Null $ws.Columns.Range("D1:D$rows").NumberFormat = "yyyy-MM-dd hh:mm" $Excel.ActiveWorkbook.SaveAs($csvPath) $Excel.Quit() [System.GC]::Collect() [System.GC]::WaitForPendingFinalizers() [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) 

https://www.experts-exchange.com/questions/27996530/How-to-convert-and-xlsx-spreadsheet-into-CSV.html#answer38780402-20

我试图跟随,但由于某种原因,我SaveAs()不起作用。 这给我一个错误

无法访问文件'D:// ETL_Data / E567DF00

我需要做些什么才能保存到CSV?

编辑:

没有fileformat参数6准确错误在评论中build议:

 Microsoft Excel不能访问文件“D:\ // ETL_Data / 8011FF00”。 有
几个可能的原因:
 o文件名或path不存在。
 o该文件正被另一个程序使用。
 o您尝试保存的工作簿与当前打开的名称相同
  工作簿。
在D:\ PS_Scripts \ twitchExcelAddSnapShot.ps1:32 char:1
 + $ Excel.ActiveWorkbook.SaveAs($ csvPath)
 + ~~~~~~~~~~~~~~~~~
     + CategoryInfo:OperationStopped:(:) [],COMException
     + FullyQualifiedErrorId:System.Runtime.InteropServices.COMException

与fileformat参数6确切的错误:

该文件无法访问。 尝试以下方法之一:
确保指定的文件夹存在。
确保包含文件的文件夹不是只读的。
确保文件名不包含以下任何字符:
   <>?  []:| 要么 *
确保文件/path名称不超过218个字符。
在D:\ PS_Scripts \ twitchExcelAddSnapShot.ps1:32 char:1
 + $ Excel.ActiveWorkbook.SaveAs($ csvPath,6)
 + ~~~~~~~~~~~~~~~~~~~
     + CategoryInfo:OperationStopped:(:) [],COMException
     + FullyQualifiedErrorId:System.Runtime.InteropServices.COMException

虽然PowerShell对于path分隔符相当宽容,但COM +服务器(如Excel.Application )可能不是。

$csvPathvariables值更改为使用\而不是/

 $csvPath = 'D:\ETL_Data\TwitchTVData2.csv' 

为了补充 Mathias R. Jessen对 背景资料 的有用答案 :

Excel的特定应用程序的行为似乎是问题的原因 ,与所使用的基础子系统或API无关。
(Excel的自动化API恰好是一个COM服务器。)
我不清楚为什么Excel这样做 – 它也是在交互式使用 ,虽然你可以争辩说,至less在编程使用它应该也允许。

提供一般性build议

  • Windows上 ,为了安全起见,使用\ ,特别是在处理应用程序级别的自动化API时,尽pipe在系统API级别/应该工作 – 请参阅下文。

  • 跨平台代码中 ,使用/ (但要注意上面的例外; [System.IO.Path]::DirectorySeparatorChar报告与平台相关的(主要)字符)。

尽pipe很less使用,但在API级别的Windows允许可互换地使用\/ (显然可以返回到DOS 2.0天 ),当引入对目录的支持时),这也反映在更高级的子系统中 ,如下示例演示。

 # PS: # Should output c:\windows\win.ini (Get-Item c:/windows/win.ini).FullName # .NET: # Should return the 1st child dir.'s path. # Note that the child directory names will be appended with "\", not "/" [System.IO.Directory]::GetDirectories('c:/windows/system32') | Select-Object -First 1 # COM: # Should return $true (New-Object -ComObject Scripting.FileSystemObject).FileExists('c:/windows/win.ini') # Windows API: # Should return a value such as 32. (Add-Type -PassThru WinApiHelper -MemberDefinition '[DllImport("kernel32.dll")] public static extern uint GetFileAttributes(string lpFileName);')::GetFileAttributes('c:/windows/win.ini') # cmd.exe # Note: quoting matters here, so that tokens with / aren't mistaken for options. # INCONSISTENT: # attrib: works cmd /c 'attrib "c:/windows/win.ini"' # dir: works with DIRECTORIES, but fails with FILES cmd /c 'dir /b "c:/windows/system32"' # OK cmd /c 'dir /b "c:/windows/win.ini"' # FAILS with 'File not found' cmd /c 'dir /b "c:/windows\win.ini"' # Using \ for the FILE component (only) works. 

下面是一个/来演示Excel问题最小例子

 # Create temporary dir. $null = mkdir c:\tmp -force $xl=New-Object -Com Excel.Application $wb = $xl.Workbooks.Add() # OK - with "\" $wb.SaveAs('c:\tmp\t1.xlsx') # FAILS - with "/": # "Microsoft Excel cannot access the file 'C:\//tmp/F5D39400'" $wb.SaveAs('c:/tmp/t2.xlsx') $xl.Quit() # Clean up temp. dir.