gpt4 book ai didi

excel - 如何使用 Powershell 应用 Excel 表格样式

转载 作者:行者123 更新时间:2023-12-04 22:15:18 28 4
gpt4 key购买 nike

我正在考虑编写将我现有的 CSV 文件转换为 XLSX 文件的脚本,所以我一直在关注这篇文章
https://code.adonline.id.au/csv-to-xlsx-powershell/
它工作正常,但我只是想知道如何在转换为 XLSX 文件时将其格式化为表格并应用样式?
如果我能得到任何帮助或建议,我将不胜感激。

### Set input and output path
$inputCSV = "C:\AuditLogSearch\Modified Audit-Log-Records.csv"
$outputXLSX = "C:\AuditLogSearch\output1.xlsx"

### Create a new Excel Workbook with one empty sheet
$excel = New-Object -ComObject excel.application
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.worksheets.Item(1)

### Build the QueryTables.Add command
### QueryTables does the same as when clicking "Data » From Text" in Excel
$TxtConnector = ("TEXT;" + $inputCSV)
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)

### Set the delimiter (, or ;) according to your regional settings
$query.TextFileOtherDelimiter = $Excel.Application.International(5)

### Set the format to delimited and text for every column
### A trick to create an array of 2s is used with the preceding comma
$query.TextFileParseType = 1
$query.TextFileColumnDataTypes = ,2 * $worksheet.Cells.Columns.Count
$query.AdjustColumnWidth = 1

### Execute & delete the import query
$query.Refresh()
$query.Delete()

$Workbook.SaveAs($outputXLSX,51)
$excel.Quit()


最佳答案

假设您想试用 ImportExcel Module .

  • 先安装: Install-Module ImportExcel -Scope CurrentUser

  • 然后代码将如下所示:
    $params = @{
    AutoSize = $true
    TableName = 'exampleTable'
    TableStyle = 'Medium11' # => Here you can chosse the Style you like the most
    BoldTopRow = $true
    WorksheetName = 'YourWorkSheetName'
    PassThru = $true
    Path = 'path/to/excel.xlsx' # => Define where to save it here!
    }

    $xlsx = Import-Csv path/to/csv.csv | Export-Excel @params
    $ws = $xlsx.Workbook.Worksheets[$params.Worksheetname]
    $ws.View.ShowGridLines = $false # => This will hide the GridLines on your file
    Close-ExcelPackage $xlsx
    作者有 Youtube channel他曾经上传教程的地方,如果您想了解更多信息,还有互联网上的在线文档。

    关于excel - 如何使用 Powershell 应用 Excel 表格样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70191906/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com