2017-04-08 59 views
4
$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为什么PowerShell不要想我的文件保存为CSV

我试图遵循,但出于某种原因,我的SaveAs()不起作用。它给了我一个错误

无法访问该文件“d:// ETL_Data/E567DF00

我有什么做的就是这在保存到CSV?

编辑:

确切错误而不FILEFORMAT参数6作为意见建议:

 
Microsoft Excel cannot access the file 'D:\//ETL_Data/8011FF00'. There are 
several possible reasons: 
o The file name or path does not exist. 
o The file is being used by another program. 
o The workbook you are trying to save has the same name as a currently open 
    workbook. 
At D:\PS_Scripts\twitchExcelAddSnapShot.ps1:32 char:1 
+ $Excel.ActiveWorkbook.SaveAs($csvPath) 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : OperationStopped: (:) [], COMException 
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException 

精确误差FILEFORMAT参数6

 
The file could not be accessed. Try one of the following: 
o Make sure the specified folder exists. 
o Make sure the folder that contains the file is not read-only. 
o Make sure the file name does not contain any of the following characters: 
    < > ? [ ] : | or * 
o Make sure the file/path name doesn't contain more than 218 characters. 
At D:\PS_Scripts\twitchExcelAddSnapShot.ps1:32 char:1 
+ $Excel.ActiveWorkbook.SaveAs($csvPath,6) 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : OperationStopped: (:) [], COMException 
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException 

回答

4

虽然PowerShell是相当宽容的时候它涉及到路径分隔符,COM +服务器(如Excel.Application)可能不是。

更改$csvPath变量值使用\而不是/

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

补充Mathias R. Jessen's helpful answer背景资料

似乎Excel的应用程序特定的行为是你问题的原因,无关t o所使用的基础子系统或API。
(Excel的自动化API恰好是一个COM服务器。)
我为什么Excel的行为这种方式目前还不清楚 - 它也这样做,在交互使用,虽然你可能会说,至少在纲领性使用也应该允许/

为了提供广义建议

  • 的Windows,为了安全起见,使用\,尤其是应用级自动化的API打交道时,虽然在系统API的级别/应该工作以及 - 见下文。

  • 跨平台的代码,使用/(但注意上面的例外; [System.IO.Path]::DirectorySeparatorChar报告平台的适当的(初级)字符)。

虽然很少使用,视窗在API级别允许可互换使用的\/(这显然goes back to the DOS 2.0 days),引入了当用于目录支持),并且是也反映在更高级别的子系统,如以下示例所示。

# 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. 
相关问题