【问题标题】:How to resize Excel column width using ImportExcel如何使用 ImportExcel 调整 Excel 列宽
【发布时间】:2022-01-11 03:52:08
【问题描述】:

我下载了 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"
}

【问题讨论】:

    标签: excel powershell format


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 2022-10-05
      • 2018-02-02
      • 1970-01-01
      • 2021-08-25
      • 2013-06-23
      • 2019-03-01
      相关资源
      最近更新 更多