我有一个excel工作簿,有10个工作表。我想找出使用Power-Shell的每个excel工作表使用的内存。我试图加载Com对象并检查属性但没有运气!
$application = New-Object -ComObject excel.application
$wb=$application.workbooks.open("C:\Model_Q.xlsx")
$wb.sheets |gm
知道该怎么做吗?
答案 0 :(得分:0)
这可能不是您正在寻找的答案,因为它不在PowerShell中,但我在Excel中找到了一个非常简单的使用VBA的解决方案,并且看到您现在没有答案,它&# 39;值得一试! (1)在Excel中打开工作表并按Alt + F11,右键单击工作簿,然后单击插入>模块。 (2)粘贴到以下VBA脚本中:
Sub WorksheetSizes()
Dim xWs As Worksheet
Dim Rng As Range
Dim xOutWs As Worksheet
Dim xOutFile As String
Dim xOutName As String
xOutName = "SheetSizesinBits"
xOutFile = ThisWorkbook.Path & "\TempWb.xls"
On Error Resume Next
Application.DisplayAlerts = False
Err = 0
Set xOutWs = Application.Worksheets(xOutName)
If Err = 0 Then
xOutWs.Delete
Err = 0
End If
With Application.ActiveWorkbook.Worksheets.Add(Before:=Application.Worksheets(1))
.Name = xOutName
.Range("A1").Resize(1, 2).Value = Array("Worksheet Name", "Size")
End With
Set xOutWs = Application.Worksheets(xOutName)
Application.ScreenUpdating = False
xIndex = 1
For Each xWs In Application.ActiveWorkbook.Worksheets
If xWs.Name <> xOutName Then
xWs.Copy
Application.ActiveWorkbook.SaveAs xOutFile
Application.ActiveWorkbook.Close SaveChanges:=False
Set Rng = xOutWs.Range("A1").Offset(xIndex, 0)
Rng.Resize(1, 2).Value = Array(xWs.Name, VBA.FileLen(xOutFile))
Kill xOutFile
xIndex = xIndex + 1
End If
Next
Application.ScreenUpdating = True
Application.Application.DisplayAlerts = True
End Sub
(3)将工作簿另存为启用宏的工作簿。 (4)关闭开发人员屏幕并按Alt + F8。 (5)选择我们刚制作的宏并运行它。 这将创建一个新的工作表,并以位为单位构建每个工作表大小的表。
祝你好运!