I am have a problem converting my multisheet .xlsx files to .csv where it will create a .csv file for everysheet on the .xlsx file. I need to poll a folder and when a new .xlsx file is dropped into the folder then it converts the file and saves it to another folder and moves the original file to an archive folder. the console is telling me that it is converting the file but it is not creating the converted files, would someone be able to help me.
$ErrorActionPreference = "Stop"
$inputFolder = "C:\PYTHON\input_folder"
$outputFolder = "C:\PYTHON\output_folder"
Import-Module ImportExcel
function Convert-ExcelToCsv {
param (
[string]$excelFilePath,
[string]$outputFolder
)
try {
$excelData = Import-Excel -Path $excelFilePath
foreach ($worksheet in $excelData.Workbook.Worksheets) {
$csvFileName = "$($worksheet.Name).csv"
$csvPath = Join-Path $outputFolder $csvFileName
Write-Host "Creating CSV: $csvPath"
$csvData = $worksheet.Cells.Text -split '\r?\n' | ConvertFrom-Csv
$csvData | Export-Csv -Path $csvPath -NoTypeInformation -Force
}
Write-Host "Converted $excelFilePath to CSV"
} catch {
Write-Host "Error converting $($excelFilePath): $($_.Exception.Message)"
}
}
while ($true) {
$latestFile = Get-ChildItem -Path $inputFolder -Filter *.xlsx | Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ($latestFile -ne $null) {
Write-Host "New file detected: $($latestFile.FullName)"
Convert-ExcelToCsv -excelFilePath $latestFile.FullName -outputFolder $outputFolder
$archiveFolder = Join-Path $inputFolder "Archive"
if (-not (Test-Path $archiveFolder)) {
New-Item -ItemType Directory -Path $archiveFolder | Out-Null
}
Move-Item -Path $latestFile.FullName -Destination $archiveFolder
Start-Sleep -Seconds 5
Get-ChildItem -Path $outputFolder
}
# Sleep for a short duration before checking again
Start-Sleep -Seconds 2
}
I am getting the following output on the console
New file detected: C:\PYTHON\input_folder\carrier_info.XLSX
Converted C:\PYTHON\input_folder\carrier_info.XLSX to CSV
but there is no files in my output folder. does anyone have any ideas why this is not working or any ideas how I achieve my goal of converting the files into my output folder.
thanks,