如何使用ImportExcel调整Excel列宽

3

我下载了 Excel 报告,但需要使用 PowerShell 调整一些列的宽度大小。因此,我想知道如何实现。感谢任何帮助或建议。

例如,我想将用户、日期和时间、项目列调整为宽度大小为 30,活动列调整为宽度大小为 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"
}
1个回答

3

可能您已经看到了,-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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接