**正在更新主题...(以前:Powershell - 在 Excel 中插入分页符)
这是我的目标:
这是我到目前为止所拥有的:
$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $True
$wbpath = 'c:\path\to\excel.xls'
$wb = $Excel.Workbooks.Open($wbpath)
$ws = $wb.Sheets.Item(1)
$rows=$ws.UsedRange.Rows.Count
$rowcnt = $Excel.WorksheetFunction.CountIf($ws.Range("A1:A"+$rows), "<>")
$RowRange = $ws.Range("A1:A"+$rowcnt)
$RowRange = $ws.Range("A1:A"+$rowcnt)
$ColumnRange = #Set column range
##Set $Rowrange as VRagebreak
##Set $ColumnRange as HPageBreak
##Set Page Break view as default Page Layout view
$Workbook.Save()
$Workbook.Close()
$Excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)|Out-Null
[gc]::collect() | Out-Null
[gc]::WaitForPendingFinalizers() | Out-Null
提前致谢!
但是经过几个小时的搜索,我发现解决我的问题的更合适的语言是 VBScript,而不是 Powershell。 希望这不会引起我偏离主题的担忧,但我只想分享一下我如何使用 VBscript 实现我的目标。只是想帮助那些可能遇到同样问题的人。
就这样吧。下面的 VBScript 将:
将当前工作表视图设置为分页预览
WScript.echo“**格式化提取...--”&现在
'获取当前目录
设置参数= WScript.Arguments
CurDir = args.Item(0)
wscript.echo "Current Directory: " & CurDir
'Page break constants
const xlPageBreakManual = -4135
const xlPageBreakNone = -4142
const xlPageBreakPreview = &H2
const xlNormalView = &H1
'Initiate Excel variables
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(CurDir)
Set colFiles = objFolder.Files
For Each objFile in colFiles
If UCase(objFSO.GetExtensionName(objFile.name)) = "XLS" Then
AbsolutePathName = objFSO.GetAbsolutePathName(objFile)
ExtractName = objFSO.GetFileName(objFile)
'Wscript.Echo "File path and Name: " & AbsolutePathName
'Wscript.Echo "Extract Name: " & extractName
'Open Excel
Set xlApp = CreateObject("Excel.Application")
set xlBook = xlApp.WorkBooks.Open(AbsolutePathName)
set xlSht = xlApp.Worksheets.Item(1)
'Set print area
rows = xlSht.UsedRange.Rows.count
rowcnt = xlApp.WorksheetFunction.CountIf(xlSht.Range("A1:A"&rows), "<>")
if InStr(extractName,"DCIS") = 1 then
wscript.echo extractName & ": Setting print area..."
xlSht.PageSetup.PrintArea = "$A$1:$N$" & rowcnt
elseif InStr(extractName,"WCIS") = 1 then
wscript.echo extractName & ": Setting print area..."
xlSht.PageSetup.PrintArea = "$A$1:$O$" & rowcnt
end if
'Set current worksheet view to Page Break Preview
Wscript.Echo extractName & ": Setting default view to Page break view..."
xlApp.ActiveWindow.View = xlPageBreakPreview
'Save formatted excel
xlBook.Save
xlBook.Close False
xlApp.Quit
'Deallocate after use
Set xlSht = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
End If
Next
WScript.echo "**Finished formatting extracts. -- " & Now