我正在考慮撰寫將現有 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()
uj5u.com熱心網友回復:
假設您想試用該ImportExcel模塊。
- 先安裝:
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 頻道,他曾經在那里上傳教程,如果您想了解更多資訊,還可以通過 Internet 找到在線檔案和教程。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371424.html
標籤:擅长 电源外壳 powershell-2.0
