将 TXT 转换为 CSV

Convert TXTto CSV

我正在做一个项目,我需要获取一个文本文件并将其设为 excel 文件。到目前为止,我想出的是这个。

cls

Remove-Item -path D:\Users\zabin\OneDrive\Desktop\ITS3410\WEEK8\MainWarehouse.csv

Add-Content -path D:\Users\zabin\OneDrive\Desktop\ITS3410\WEEK8\MainWarehouse.csv -Value '"Part_Number","Cost","Price"'

$csvPath = 'D:\Users\zabin\OneDrive\Desktop\ITS3410\WEEK8\MainWarehouse.csv'


#region Excel Test
If (test-path HKLM:SOFTWARE\Classes\Word.Application) {
    Write-host "Microsoft Excel installed"
} else {
    Write-host "Microsoft Excel not installed"
}
#endregion

#region Patterns
$mainpattern1 = '(?<Partnumber>\d*\s*\w*,)(?<Cost>\d*.\d*),(?<Price>\d*.\d*)'
$mainpattern2 = '(?<Part_number>\d*-\d*-\d*),(?<Cost>\d*.\d*),(?<Price>\d*.\d*)'

#endregion

get-Content 'D:\Users\zabin\OneDrive\Desktop\ITS3410\WEEK8\MainWarehouse.csv' | #grabs the content
    Select-String -Pattern $mainpattern1, $mainpattern2 | #selects the patterns
    Foreach-Object {
        $Part_Number, $Cost, $Price = $_.Matches[0].Groups['Part_number', 'Cost','Price']



        [PSCustomObject] @{
     
     part_number = $Part_Number
     Cost = $Cost
     Price = $Price

   
    }
    $objResults | Export-Csv -Path $csvPath -NoTypeInformation -Append
    
}

这里有一些示例数据

00001143 SP,136.41,227.35
00001223 SP,48.66,81.10
00001236 SP,149.72,249.53
0001-0003-00,100.95,168.25
00015172 W,85.32,142.20

我正在创建文件并且 header 值是正确的,但我不确定如何获取要导入的值。

继续我的评论...使用资源和一个简单的例子。

Find-Module -Name '*excel*' | Format-Table -AutoSize
# Results
<#
Version     Name            Repository Description                
-------     ----            ---------- -----------                
7.1.1       ImportExcel     PSGallery  PowerShell module to import/export Excel spreadsheets, without Excel....                  
0.1.12      PSWriteExcel    PSGallery  Little project to create Excel files without Microsoft Excel being installed.             
1.0.2       PSExcel         PSGallery  Work with Excel without installing Excel        
...    
0.6.9       ExcelPSLib      PSGallery  Allow simple creation and manipulation of XLSX file                  
2.1         Read-ExcelFile  PSGallery  PowerShell module to import Excel spreadsheets, without Excel....    
...
#>

MSExcel 将在本机读取格式正确的 CSV。因此,要转换为真正的 XLS 文件,请使用 PowerShell、MSOffice COM 打开包含 CSV 文件的 MSExcel,然后将其另存为 XLS 格式。

$FileName = "$env:temp\Report"

Get-Process | 
Export-Csv -UseCulture -Path "$FileName.csv" -NoTypeInformation -Encoding UTF8

$excel         = New-Object -ComObject Excel.Application 
$excel.Visible = $true
$excel.Workbooks.Open("$FileName.csv").SaveAs("$FileName.xlsx",51)
$excel.Quit()

explorer.exe "/Select,$FileName.xlsx"

您的用例当然如前所述:

Import-Csv -Path 'D:\temp\book1.txt' -header Title, Author

然后如上所述使用 COM。

在我提出这个问题后,我最终解决了这个问题我在这段代码中有很多缺陷

Add-Content -path D:\Users\zabin\OneDrive\Desktop\ITS3410\WEEK8\MainWarehouse.csv -Value '"Part_Number","Cost","Price"'

$csvPath = 'D:\Users\zabin\OneDrive\Desktop\ITS3410\WEEK8\MainWarehouse.csv'


#region Excel Test
If (test-path HKLM:SOFTWARE\Classes\Excel.Application) {#these next few lines will check if excel is installed on the system
    Write-host "Microsoft Excel installed"
} else {
    Write-host "Microsoft Excel not installed"
}
#endregion

#region Patterns
$mainpattern1 = '(?<Part_number>\d*\s*\w*),(?<Cost>\d*.\d*),(?<Price>\d*.\d*)'#These two line will use REGEX to help seperate the data
$mainpattern2 = '(?<Part_number>\d*-\d*-\d*),(?<Cost>\d*.\d*),(?<Price>\d*.\d*)'

#endregion

get-Content 'D:\Users\zabin\OneDrive\Desktop\ITS3410\WEEK8\Main.rtf' | #grabs the content
    Select-String -Pattern $mainpattern2, $mainpattern1 | #selects the patterns
   Foreach-Object {
        $Part_number, $Cost, $Price = $_.Matches[0].Groups['Part_number', 'Cost','Price']     #Gets the groups of a call to select-string



  $results = [PSCustomObject] @{#the list here is what i use to seperate the data onto the CSV file
     part_number = $Part_Number
     Cost = $Cost
     Price = $Price

   
    }
    
$results | Export-Csv -Path $csvPath -NoTypeInformation -Append #moves the results to the CSV file
    
}