Windows 7 Print output of two commands in two columns in excel file

melinadavid

New Member
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,
 
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()
 
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.
 
#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()
 
Back
Top