如何使用 Powershell 应用 Excel Table 样式
How to apply Excel Table Style using Powershell
我正在考虑编写将我现有的 CSV 文件转换为 XLSX 文件的脚本,所以我一直在关注这个 post
https://code.adonline.id.au/csv-to-xlsx-powershell/
它工作正常,但我只是想知道如何在转换为 XLSX 文件时格式化为 table 并应用样式?
如果能得到任何帮助或建议,我将不胜感激。
### 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 用于上传教程的地方,如果您想了解更多信息,也可以通过互联网找到在线文档。
我正在考虑编写将我现有的 CSV 文件转换为 XLSX 文件的脚本,所以我一直在关注这个 post https://code.adonline.id.au/csv-to-xlsx-powershell/
它工作正常,但我只是想知道如何在转换为 XLSX 文件时格式化为 table 并应用样式?
如果能得到任何帮助或建议,我将不胜感激。
### 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 用于上传教程的地方,如果您想了解更多信息,也可以通过互联网找到在线文档。