# Converting text file data to excel

**URL:** <https://forums.powershell.org/t/converting-text-file-data-to-excel/15807>\
**Category:** PowerShell Help\
**Created:** [March 2, 2021, 9:09am UTC](https://forums.powershell.org/t/converting-text-file-data-to-excel/15807 "2021-03-02T09:09:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rony1](https://avatars.discourse-cdn.com/v4/letter/r/d07c76/32.png) [@rony1](https://forums.powershell.org/u/rony1)\
**Post date:** [March 2, 2021, 9:09am UTC](https://forums.powershell.org/t/converting-text-file-data-to-excel/15807/1 "2021-03-02T09:09:19Z")

</div>

Hi,

I want to convert a text file data to excel, below is the content of the text file:

ModuleName FailedCount SucceededCount TotalCount

BESS 0 80 80

IG 0 14 14

the spaces between the words in the the text file are not fixed, so I how do I convert this data to excel? Below is the PowerShell script i tried, but it is not giving correct results

```auto
#Constants
$SourcePath = "C:\Users\rnoy\Parse-html\inputfile.txt"
$DestinationPath = "C:\Users\rnoy\Parse-html\outputfile"
$deleimter= "`t"; #You can replace it by any other delimeter ';' or '/'#You can leave this part as it is
$SourceTxt = Get-Content $SourcePath
$xlsxFile = $DestinationPath + ".xlsx"
if (Test-Path ($xlsxFile))
{
Remove-Item $xlsxFile
}
#$SourceTxt >> $txtFile
[threading.thread]::CurrentThread.CurrentCulture = 'en-US'
$xl=New-Object -ComObject "Excel.Application"
$wb=$xl.Workbooks.Add()
$ws=$wb.ActiveSheet
$xl.Visible=$True
$cells=$ws.Cells
$Content = Get-Content $SourcePath
$numOfRows = $Content.Length
$numOfColumns = $Content[0].split($deleimter).Count
for ($i=0; $i -lt $numOfRows ;$i++)
{
$rowData = $Content[$i].split($deleimter)
for ($j=0; $j -lt $numOfColumns; $j++)
{
$cellData = $rowData[$j]
$cells.item($i+1,$j+1) = $cellData
}
}
#Apply some Format for Excel header
$xl.Cells.EntireColumn.AutoFit()
$xl.Cells.EntireColumn.AutoFilter()
$xl.Rows.Item(1).Font.Bold = $True
$xl.Rows.Item(1).Interior.ColorIndex = 44
#Save Sheet
$wb.SaveAs($xlsxFile)
$wb.Close()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($wb)
$xl.Quit()
#Clean up
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 2, 2021, 9:25am UTC](https://forums.powershell.org/t/converting-text-file-data-to-excel/15807/2 "2021-03-02T09:25:39Z")

</div>

When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to help you making their work twice or more.

> <https://stackoverflow.com/questions/66436555/converting-text-file-data-to-excel-using-powershell>

Thanks in advance

And please format your code as code. here you can learn how that works:

> [@Guide to Posting Code - Redux](https://forums.powershell.org/t/guide-to-posting-code-redux/15797):
>
> Discourse uses [Markdown](https://www.markdownguide.org/getting-started/) for text formatting. There is a reference sheet and quick tutorial here: [Markdown Reference](https://commonmark.org/help/) You can produce in-line code formatting by wrapping your code in backticks (`). For example, `Get-ChildItem` produces Get-ChildItem. For larger code blocks, place a line containing three backticks above and below your code. For example ``` # 256-Color Foreground & Background Charts echo “`n$esc[1;4m256-Color Foreground & Background Charts$esc[0m” foreach ($fgbg in 38,48) { #…

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [March 2, 2021, 6:50pm UTC](https://forums.powershell.org/t/converting-text-file-data-to-excel/15807/3 "2021-03-02T18:50:25Z")

</div>

This can be easily parsed with ConvertFrom-Csv with a space delimiter and exported to CSV:

```auto
$mockFile = @"
ModuleName FailedCount SucceededCount TotalCount
BESS 0 80 80
IG 0 14 14
"@ | ConvertFrom-Csv -Delimiter ' '

$mockFile | Export-Csv -Path C:\Scripts\MyOutput.csv -NoTypeInformation

```

Output:

```auto
ModuleName FailedCount SucceededCount TotalCount
---------- ----------- -------------- ----------
BESS 0 80 80
IG 0 14 14

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:27pm UTC](https://forums.powershell.org/t/converting-text-file-data-to-excel/15807/4 "2024-05-16T20:27:46Z")

</div>


