如何使用 ImportExcel 调整 Excel 列宽
How to resize Excel column width using ImportExcel
我下载了 Excel 报告,但我需要使用 PowerShell 将某些列的大小调整为不同的宽度,所以我只是想知道如何实现。任何帮助或建议将不胜感激。
例如,我想修改用户、日期和时间、项目列的宽度为 30,Activity 列的宽度为 50,其他一些列的宽度为 30,依此类推...
function Modify-Columns {
$params = @{
AutoSize = $true
# TableName = 'exampleTable'
TableStyle = 'Light9' # => Here you can chosse the Style you like the most
BoldTopRow = $true
WorksheetName = "Audit Log"
PassThru = $true
Path = "C:\AuditLogSearch$((Get-Date).AddDays(-7).ToString('yyyy-MM-dd')) _ $(Get-Date -Format "yyyy-MM-dd") Audit-Log-Records.xlsx" # => Define where to save it here!
}
Write-Host "Start Modifiying Column and Row using AD DisplayName and Excel Files"
$modifiedFile = Import-Csv "C:\AuditLogSearch\Modified-Audit-Log-Records.csv"
$actionReference = Import-Csv "C:\AuditLogSearch\Reference\eDiscovery_Activities_List.csv"
$result = foreach ($u in $modifiedFile) {
$u.User = (Get-AzureADUser -ObjectId $u.User).DisplayName
New-Object PsObject -Property @{
User = $u.User
# "Search Criteria" = $u."Search Criteria"
"Result Status" = $u."Result Status"
"Date & Time" = $u."Date & Time"
"Search Conditions" = $u."Search Conditions"
"SharePoint Sites" = $u."SharePoint Sites"
"Exchange Public Folders" = $u."Exchange Public Folders"
"Exchange Mailboxes" = $u."Exchange Mailboxes"
"Case Name" = $u."Case Name"
"Search Criteria" = $u."Search Criteria"
"Item" = $u."Item"
"Activity" = if (($actionReference | where-object { $_.Name -eq $u."Activity" }).Value -ne $null) { ($actionReference | where-object { $_.Name -eq $u."Activity" }).Value }
else { $u."Activity" }
} | Select-object -Property User, "Date & Time", "Case Name", "Item", "Activity" , "SharePoint Sites", "Exchange Mailboxes", "Exchange Public Folders" , "Search Criteria", "Result Status"
}
$xlsx = $result | Export-Excel @params
$ws = $xlsx.Workbook.Worksheets[$params.Worksheetname]
$ws.View.ShowGridLines = $false # => This will hide the GridLines on your file
Close-ExcelPackage $xlsx
Write-Host "Finished Modifiying Column and Row using AD DisplayName and Excel Files"
}
如您所见,-AutoSize
不是很精确,它往往会留下比需要更多的宽度。如果您需要将硬编码值设置为您可以使用的列之一:
$xlsx.Workbook.Worksheets['SheetName'].Column(n).width = newVal
注意:此方法需要使用-PassThru
。
这是一个最小的可重现示例:
$result = [pscustomobject]@{
Col1 = 'Test'
Col2 = 'Test'
Col3 = [datetime]::Now
}
$params = @{
AutoSize = $true
TableStyle = 'Light9'
BoldTopRow = $true
WorksheetName = "Audit Log"
PassThru = $true
Path = './test.xlsx'
}
$xlsx = $result | Export-Excel @params
$ws = $xlsx.Workbook.Worksheets[$params.WorksheetName]
# This is how you can get the number of Columns, Index starts from 1 not 0.
$ws.Dimension.Columns # => 3
$ws.Column(1).Width # => Col1 current Width is 9.140625
$ws.Column(1).Width = 5 # => Updated to 5
$ws.View.ShowGridLines = $false # => Hide the GridLines
Close-ExcelPackage $xlsx
我下载了 Excel 报告,但我需要使用 PowerShell 将某些列的大小调整为不同的宽度,所以我只是想知道如何实现。任何帮助或建议将不胜感激。
例如,我想修改用户、日期和时间、项目列的宽度为 30,Activity 列的宽度为 50,其他一些列的宽度为 30,依此类推...
function Modify-Columns {
$params = @{
AutoSize = $true
# TableName = 'exampleTable'
TableStyle = 'Light9' # => Here you can chosse the Style you like the most
BoldTopRow = $true
WorksheetName = "Audit Log"
PassThru = $true
Path = "C:\AuditLogSearch$((Get-Date).AddDays(-7).ToString('yyyy-MM-dd')) _ $(Get-Date -Format "yyyy-MM-dd") Audit-Log-Records.xlsx" # => Define where to save it here!
}
Write-Host "Start Modifiying Column and Row using AD DisplayName and Excel Files"
$modifiedFile = Import-Csv "C:\AuditLogSearch\Modified-Audit-Log-Records.csv"
$actionReference = Import-Csv "C:\AuditLogSearch\Reference\eDiscovery_Activities_List.csv"
$result = foreach ($u in $modifiedFile) {
$u.User = (Get-AzureADUser -ObjectId $u.User).DisplayName
New-Object PsObject -Property @{
User = $u.User
# "Search Criteria" = $u."Search Criteria"
"Result Status" = $u."Result Status"
"Date & Time" = $u."Date & Time"
"Search Conditions" = $u."Search Conditions"
"SharePoint Sites" = $u."SharePoint Sites"
"Exchange Public Folders" = $u."Exchange Public Folders"
"Exchange Mailboxes" = $u."Exchange Mailboxes"
"Case Name" = $u."Case Name"
"Search Criteria" = $u."Search Criteria"
"Item" = $u."Item"
"Activity" = if (($actionReference | where-object { $_.Name -eq $u."Activity" }).Value -ne $null) { ($actionReference | where-object { $_.Name -eq $u."Activity" }).Value }
else { $u."Activity" }
} | Select-object -Property User, "Date & Time", "Case Name", "Item", "Activity" , "SharePoint Sites", "Exchange Mailboxes", "Exchange Public Folders" , "Search Criteria", "Result Status"
}
$xlsx = $result | Export-Excel @params
$ws = $xlsx.Workbook.Worksheets[$params.Worksheetname]
$ws.View.ShowGridLines = $false # => This will hide the GridLines on your file
Close-ExcelPackage $xlsx
Write-Host "Finished Modifiying Column and Row using AD DisplayName and Excel Files"
}
如您所见,-AutoSize
不是很精确,它往往会留下比需要更多的宽度。如果您需要将硬编码值设置为您可以使用的列之一:
$xlsx.Workbook.Worksheets['SheetName'].Column(n).width = newVal
注意:此方法需要使用-PassThru
。
这是一个最小的可重现示例:
$result = [pscustomobject]@{
Col1 = 'Test'
Col2 = 'Test'
Col3 = [datetime]::Now
}
$params = @{
AutoSize = $true
TableStyle = 'Light9'
BoldTopRow = $true
WorksheetName = "Audit Log"
PassThru = $true
Path = './test.xlsx'
}
$xlsx = $result | Export-Excel @params
$ws = $xlsx.Workbook.Worksheets[$params.WorksheetName]
# This is how you can get the number of Columns, Index starts from 1 not 0.
$ws.Dimension.Columns # => 3
$ws.Column(1).Width # => Col1 current Width is 9.140625
$ws.Column(1).Width = 5 # => Updated to 5
$ws.View.ShowGridLines = $false # => Hide the GridLines
Close-ExcelPackage $xlsx