PowerShell中将CSV文件转换为EXCEL文件 2021-06-13 Website News 暂无评论 1700 次阅读 一直在研究PowerShell,针对CSV文件转换成EXCEL文件(XLSX)一直没找到比较合适方法,查询了许多资料后,整理出来一下这个脚本,目前测试下来速度还不错。 ```csharp #Define locations and delimiter $csv = "D:\Test\20210505_042018.csv" #Location of the source file $xlsx = "D:\Test\20210505_042018.xlsx" #Desired location of output $delimiter = "," #Specify the delimiter used in the file # 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) $worksheet.Cells.NumberFormat = "@" # Build the QueryTables.Add command and reformat the data $TxtConnector = ("TEXT;" + $csv) $Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1")) $query = $worksheet.QueryTables.item($Connector.name) $query.TextFileOtherDelimiter = $delimiter $query.TextFileParseType = 1 $query.TextFilePlatform = 65001 $query.TextFileColumnDataTypes = ,2 * $worksheet.Cells.Columns.Count $query.AdjustColumnWidth = 1 # Execute & delete the import query $query.Refresh() $query.Delete() # Save & close the Workbook as XLSX. $Workbook.SaveAs($xlsx,51) $excel.Quit() ``` 标签: PowerShell, excel, csv转excel, CSV to Excel, CSV, XLSX, 转换 本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
评论已关闭