0

I am trying to download all CSV files from the \tmp\ directory with a PS script and convert them to one excel file as a report and place it in the \reports\ directory under the name LDAP.xlsx . My CSV files have varying amounts of saved data.

In the forum I found this how-to-export-a-csv-to-excel-using-powershell and my code looks like this:

Clear-Host
# SOURCE
##########
# config file
$conf_file = "C:\PS_LDAP_searchlight\config\searchlight_conf.conf"
$conf_values = Get-Content $conf_file | Out-String | ConvertFrom-StringData

# variables from config file
$main_path = $conf_values.main_path
$tmp_path = $conf_values.tmp_path
$reports_path = $conf_values.reports_path

# PROGRAM
##########
$workingdir = $main_path + $tmp_path + "*.csv"
$reportsdir = $main_path + $reports_path
$csv = dir -path $workingdir
foreach($inputCSV in $csv){
$outputXLSX = $reportsdir + "\" + $inputCSV.Basename + ".xlsx"
### Create a new Excel Workbook with one empty sheet
$excel = New-Object -ComObject excel.application 
$excel.DisplayAlerts = $False
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.worksheets.Item(1)
### Build the QueryTables.Add command
### QueryTables does the same as when clicking "Data » From Text" in Excel
$TxtConnector = ("TEXT;" + $inputCSV)
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)
### Set the delimiter (, or ;) according to your regional settings
### $Excel.Application.International(3) = ,
### $Excel.Application.International(5) = ;
$query.TextFileOtherDelimiter = $Excel.Application.International(5)
### Set the format to delimited and text for every column
### A trick to create an array of 2s is used with the preceding comma
$query.TextFileParseType  = 1
$query.TextFileColumnDataTypes = ,2 * $worksheet.Cells.Columns.Count
$query.AdjustColumnWidth = 1
### Execute & delete the import query
$query.Refresh()
$query.Delete()
### Save & close the Workbook as XLSX. Change the output extension for Excel 2003
$Workbook.SaveAs($outputXLSX,51)
$excel.Quit()

# Cleaner
$inputCSV = $null
$outputXLSX = $null

}
## To exclude an item, use the '-exclude' parameter (wildcards if needed)
#remove-item -path $workingdir -exclude *Crab4dq.csv

# CLEANER
###############################
# SOURCE
###############################
# config file
    $conf_file = $null
    $conf_values = $null
# variables from config file
    $main_path = $null
    $tmp_path = $null
    $reports_path = $null
# PROGRAM
###############################
    $workingdir = $null
    $csv = $null
    $reportsdir = $null

the code reads all files but writes one to one. I need help and explanation on how to make a many-to-one option. I would like each CSV file to be saved as a separate sheet under its own name like:

users_all_inf.csv in excel\sheet1 => users_all_inf
active_users_last_logon_year_ago.csv in excel\sheet2 => active_users_last_logon_year_ago
nextfilename.csv in excel\next_sheet => nextfilename

so that all data will be available in one excel report.xlsx file.
I will be grateful for any hint or help in converting the code.

Kubix
  • 73
  • 7

2 Answers2

1

Finally my code looks like:

Clear-Host
# config file
$conf_file = "C:\SEARCHLIGHT\config\searchlight_conf.conf"
$conf_values = Get-Content $conf_file | Out-String | ConvertFrom-StringData

# variables from config file
$main_path = $conf_values.main_path
$tmp_path = $conf_values.tmp_path
$reports_path = $conf_values.reports_path
$system_name = $conf_values.system_name

$tmp_dir = $main_path + $tmp_path + "*" # source file
$outputfilename = $(get-date -f yyyyMMdd) + "_" + $system_name + "_report.xlsx" # destination file with date
    
# get list of csvs files
$csvs = Get-ChildItem $tmp_dir -Include *.csv
$y = $csvs.Count
Write-Host "Detected the following CSV files: ($y)"
foreach ($csv in $csvs) {
    Write-Host " "$csv.Name
}

Write-Host Creating: $outputfilename
# Create a new Excel workbook
$excelapp = new-object -comobject Excel.Application
$excelapp.sheetsInNewWorkbook = $csvs.Count
$xlsx = $excelapp.Workbooks.Add()
$sheet=1
$delimiter = ";" # delimiter used in the csv file
foreach ($csv in $csvs) {
    #$row=1
    #$column=1
    $worksheet = $xlsx.Worksheets.Item($sheet)
    $worksheet.Name = $csv.Name
    # Build the QueryTables.Add command and reformat the data
    $TxtConnector = ("TEXT;" + $csv)
    $Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
    $query = $worksheet.QueryTables.item($Connector.name)
    $query.TextFileOtherDelimiter = $delimiter
    $query.TextFileParseType = 1
    $query.TextFileColumnDataTypes = ,1 * $worksheet.Cells.Columns.Count
    $query.AdjustColumnWidth = 1
    # Execute & delete the import query
    $query.Refresh()
    $query.Delete()
    $sheet++
} # end foreach ($csv in $csvs)
# Save & close the Workbook as XLSX
$output = $main_path + $reports_path + $outputfilename
$xlsx.SaveAs($output)
$excelapp.quit()
Kubix
  • 73
  • 7
0

Please find the github link on using ImportExcel module. https://github.com/dfinke/ImportExcel/tree/master/Examples

$outputFile = "C:\Temp\OutputExcelFile.xlsx" #Output File
$csvFiles = Get-childItem -Filter *.csv # Filtering CSV file in my present working dir
foreach ($csvFile in $csvFiles) {
#Import csv file and export it contents to Output excel file and rename the sheet.
Import-csv $csvFile | Export-Excel $outputFile -WorksheetName $csvFile.BaseName
}

Hope it helps.

Dilly B
  • 1,280
  • 2
  • 11
  • 15
  • I used the code and nothing happened `Export-Excel : The 'Export-Excel' command was found in the module 'ImportExcel', but the module could not be loaded. For more information, run 'Import-Module ImportExcel'. At line:30 char:23 + Import-csv $csv | Export-Excel $outputfilename -WorksheetName $cs ... + ~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Export-Excel:String) [], CommandNotFoundE xception + FullyQualifiedErrorId : CouldNotAutoloadMatchingModule` – Kubix Nov 15 '22 at 17:13
  • The above error gives us the information that your module is not loaded. To resolve this issue please refer the above github link and read about how to use powershell modules from the following link: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_modules?view=powershell-7.3 – Dilly B Nov 16 '22 at 04:48