melinadavid
New Member
- Joined
- Jun 5, 2017
- Messages
- 2
- Thread Author
-
- #1
Hi,
I am writing a windows script to run 2 commands. Now the outputs goes in two rows, but I want the output of each command goes in different columns in excel. This is My script:
For %%F in (*.txt) do (
Command_1 %%F >> output.xls & command_2 %%F >> output.xls
)
Now my Output.xls is like:
AAA
BBB
I want my output.xls be like :
AAA BBB
I will appreciate if somebody has any solution for this.
Thanks,
I am writing a windows script to run 2 commands. Now the outputs goes in two rows, but I want the output of each command goes in different columns in excel. This is My script:
For %%F in (*.txt) do (
Command_1 %%F >> output.xls & command_2 %%F >> output.xls
)
Now my Output.xls is like:
AAA
BBB
I want my output.xls be like :
AAA BBB
I will appreciate if somebody has any solution for this.
Thanks,
Solution
In Powershell...
Code:
$command1Output = Command_1
$command2Output = Command_2
$excelObject = New-Object -ComObject Excel.Application
$workbook = $excelObject.Workbooks.Add()
$worksheet = $workbook.ActiveSheet
$index = 1
foreach($output in $command1Output)
{
$worksheet.Cells.Item($index,1)=$output
$index++
}
$index = 1
foreach($output in $command2Output)
{
$worksheet.Cells.Item($index,2)=$output
$index++
}
$workbook.SaveAs("C:\somefile.xlsx")
$excelObject.Workbooks.Close()
$excelObject.Quit()
- Joined
- Jul 4, 2015
- Messages
- 8,998
In Powershell...
Code:
$command1Output = Command_1
$command2Output = Command_2
$excelObject = New-Object -ComObject Excel.Application
$workbook = $excelObject.Workbooks.Add()
$worksheet = $workbook.ActiveSheet
$index = 1
foreach($output in $command1Output)
{
$worksheet.Cells.Item($index,1)=$output
$index++
}
$index = 1
foreach($output in $command2Output)
{
$worksheet.Cells.Item($index,2)=$output
$index++
}
$workbook.SaveAs("C:\somefile.xlsx")
$excelObject.Workbooks.Close()
$excelObject.Quit()
melinadavid
New Member
- Joined
- Jun 5, 2017
- Messages
- 2
- Thread Author
-
- #3
In Powershell...
Code:$command1Output = Command_1 $command2Output = Command_2 $excelObject = New-Object -ComObject Excel.Application $workbook = $excelObject.Workbooks.Add() $worksheet = $workbook.ActiveSheet $index = 1 foreach($output in $command1Output) { $worksheet.Cells.Item($index,1)=$output $index++ } $index = 1 foreach($output in $command2Output) { $worksheet.Cells.Item($index,2)=$output $index++ } $workbook.SaveAs("C:\somefile.xlsx") $excelObject.Workbooks.Close() $excelObject.Quit()
Thanks for your answer Neemobeer!
I am trying to understand the code you wrote.
- Joined
- Jul 4, 2015
- Messages
- 8,998
#Stores the output from Command_1 and _2 into separate variables (Should store as an array of elements)
$command1Output = Command_1
$command2Output = Command_2
#Creates an excel interface
$excelObject = New-Object -ComObject Excel.Application
#Creates an excel workbook in memory
$workbook = $excelObject.Workbooks.Add()
#Sets $worksheet to the active worksheet in the workbook, there is always 1 worksheet in a workbook
$worksheet = $workbook.ActiveSheet
$index = 1
#Loop through each element in the array and saves that element ($output) into the row at (Row,Column) $index is incremented so the the element is in the next row
foreach($output in $command1Output)
{
$worksheet.Cells.Item($index,1)=$output
$index++
}
$index = 1
#Loop through each element in the array and saves that element ($output) into the row at (Row,Column) $index is incremented so the the element is in the next row
foreach($output in $command2Output)
{
$worksheet.Cells.Item($index,2)=$output
$index++
}
#Save the workbook to a file
$workbook.SaveAs("C:\somefile.xlsx")
#Close open workbook and excel interface
$excelObject.Workbooks.Close()
$excelObject.Quit()
$command1Output = Command_1
$command2Output = Command_2
#Creates an excel interface
$excelObject = New-Object -ComObject Excel.Application
#Creates an excel workbook in memory
$workbook = $excelObject.Workbooks.Add()
#Sets $worksheet to the active worksheet in the workbook, there is always 1 worksheet in a workbook
$worksheet = $workbook.ActiveSheet
$index = 1
#Loop through each element in the array and saves that element ($output) into the row at (Row,Column) $index is incremented so the the element is in the next row
foreach($output in $command1Output)
{
$worksheet.Cells.Item($index,1)=$output
$index++
}
$index = 1
#Loop through each element in the array and saves that element ($output) into the row at (Row,Column) $index is incremented so the the element is in the next row
foreach($output in $command2Output)
{
$worksheet.Cells.Item($index,2)=$output
$index++
}
#Save the workbook to a file
$workbook.SaveAs("C:\somefile.xlsx")
#Close open workbook and excel interface
$excelObject.Workbooks.Close()
$excelObject.Quit()