0

I have a CSV file that has similar products within it and quantities of each product beside it.

Sample from CSV file

Qty Ordered         Product/Item Description    Top row (header)
   7                Product1
   3                Product2
   5                Product1
   3                Product3

I need a method to find all the similar product#s, add up their Quantities, and place the total of each similar product in a new row.

Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property 
@{
Multiselect = $false # Multiple files can be chosen
Filter = 'Excel (*.csv, *.xlxs)|*.csv;*.xlsx' # Specified file types
}

[void]$FileBrowser.ShowDialog()

$file = $FileBrowser.FileNames;




[Reflection.Assembly]::LoadWithPartialName
("Microsoft.Office.Interop.Excel")|Out-Null
$excel = New-Object Microsoft.Office.Interop.Excel.ApplicationClass
$excel.Visible = $true 
$wb = $excel.Workbooks.Open($file)
$ws = $wb.ActiveSheet
$c = $ws.Columns
$c.Item(2).hidden = $true

This code, asks the user to select the csv file, hides useless columns and auto-sizes the important columns as well.

6
  • 1
    What have you tried so far? Did you import the csv file into Excel? What code have you tied? Commented May 24, 2017 at 19:11
  • Yes, I can make it work in excel but need to make it work in powershell as our suppliers are submitting us purchase orders per day in csv format. It would be very beneficial to have a script that can add up the quantities of the products in bulk for our packaging. Commented May 24, 2017 at 19:21
  • 1
    Please add your code by editing your question, so that is properly formatted Commented May 24, 2017 at 19:29
  • Thanks Ben, I've added the code to the question. Commented May 24, 2017 at 19:31
  • A .csv file is just a comma delimited ascii text file. By the data above I assume it is two pieces of data per row in the csv file. Can you edit your question to show the first few lines of one of the csv files? Make sure the top line is present. Commented May 24, 2017 at 19:40

1 Answer 1

1

Rather than using Excel as a COM Object you could use Import-CSV and then Group-Object. Then loop through the groups for the information you need.

Add-Type -AssemblyName System.Windows.Forms 
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
    Multiselect = $false # Multiple files can be chosen 
    Filter = 'Excel (.csv, *.xlxs)|.csv;*.xlsx' # Specified file types 
} 
[void]$FileBrowser.ShowDialog() 
ForEach ($file in $FileBrowser.FileNames) {
    $CSV = Import-CSV $file | Add-Member -Name Total -Value 0 -MemberType NoteProperty
    $Groups = $CSV | Group-Object "Product/Item Description"
    $NewCSV = Foreach ($Group in $Groups) {
        $Count = 0
        $Group.Group."Qty Ordered" | ForEach-Object {$Count += $_}
        Foreach ($value in $CSV) {
            If ($value."Product/Item Description" -eq $Group.Name) {
                 $value.Total = $Count
                 $value
            }
        }
    }
    Export-CSV "$filenew" -NoTypeInformation
}
Sign up to request clarification or add additional context in comments.

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.