0

I am new to Powershell and I need to parse text file into excel and edit content.

I managed to open file with the following script:

$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$Excel.DisplayAlerts = $false
$Excel.Workbooks.OpenText( "Documents\myfile.txt")

It runs without exception, but the data I'm left with tells me the file is not loaded properly.

I need to set parameters like FieldInfo for export.

How do I do this?

Thanks for help.

2
  • What do you mean by "its not loaded properly" ? Commented Dec 7, 2016 at 14:27
  • It loads columns with ID-values as numbers and that means loss of inital zeroes in ID numbers. For example employee number 00051 is loaded as employee number 51. Commented Dec 7, 2016 at 14:36

2 Answers 2

1

You have to get the item from the file in order to load it in excel. DO like this:

$excel = new-object -comobject excel.application
$file = get-item "E:\myfile.txt" # Mention the path of the file
$excel.Visible=$true
$excel.displayalerts = $False
$wb = $excel.workbooks.open($file)

Hope it helps you.

Sign up to request clarification or add additional context in comments.

4 Comments

How can I set column type as text before loading data? I have problem with excel auto-casting column as number instead of text
Share the text file and tell what exactly you want to get displayed in Excel. Its very generic to answer that without having the actual data. The sample I posted its just to load the data from the text file. You can use it for any file type
Imagine input like this: pastebin.com/eBcNpuwy. Inital zeroes are lost when its parsed into excel. I need to specify 1st column as text. How can I do that ?
I saw the data in pastebin. So what you can do is. Take the data as it is and load in the excel using the script I have given. It will populate the data in the single column. Now go to the Excel , go to Data pane , click on Text to Columns. Split using the delimiter Space. I think that is what you are looking for. Even if you want you can save it as csv and you can use the content as import-csv...
0

When you bring data into Excel via text file or manual input, Excel will try to interpret what type of data it is, text, date or number. You are bringing in text that looks like a number to Excel.

One solution is to make Powershell do all the work, get it to load the file and then loop through each row. You can then force the format before loading the data.

Something like this:

$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$wb = $Excel.Workbooks.add()
$ws = $wb.Worksheets[1]

Import-Csv "Documents\myfile.txt" | % {$i = 1}{

  $ws.Range("A" + $i).NumberFormat = '@'
  $ws.Range("A" + $i).Value = $_.Field1

  #Repeat for each field

  $i++
)  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.