troubleshooting Question

Error in Powershell Trying to Convert Excel Spreadsheet .xlsx to .csv

Avatar of Rob M.
Rob M.Flag for United States of America asked on
Microsoft OfficeMicrosoft SQL ServerMicrosoft ExcelPowershellSQL
4 Comments2 Solutions358 ViewsLast Modified:
Hello,

I am tasked with creating a Powershell script that will run via a SQL Server Agent Job (SQL Server 2014). The script will convert all Excel spreadsheets in a folder from from .xlsx to .csv. The script is erroring out.

Below is both the raw script as well as the run output with the error. If someone could point me in the right direction I would greatly appreciate it.

Thanks in advance.

Robert

Error:

ForEach-Object : Unable to find type [Microsoft.Office.Interop.Excel.XlFileFormat]. Make sure that the assembly that
contains this type is loaded.
At line:11 char:15
+ $ExcelFiles | ForEach-Object {
+               ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Microsoft.Offic...el.XlFileFormat:TypeName) [ForEach-Object], Runtime
   Exception
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.ForEachObjectCommand

Script:

$ErrorActionPreference = 'Stop'

Function Convert-CsvInBatch
{
	[CmdletBinding()]
	Param
	(
		[Parameter(Mandatory=$true)][String]$Folder
	)
	$ExcelFiles = Get-ChildItem -Path $Folder -Filter *.xlsx -Recurse

	$excelApp = New-Object -ComObject Excel.Application
	$excelApp.DisplayAlerts = $false

	$ExcelFiles | ForEach-Object {
		$workbook = $excelApp.Workbooks.Open($_.FullName)
		$csvFilePath = $_.FullName -replace "\.xlsx$", ".csv"
		$workbook.SaveAs($csvFilePath, [Microsoft.Office.Interop.Excel.XlFileFormat]::xlCSV)
		$workbook.Close()
	}

	# Release Excel Com Object resource
	$excelApp.Workbooks.Close()
	$excelApp.Visible = $true
	Start-Sleep 5
	$excelApp.Quit()
	[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excelApp) | Out-Null
}
#
# 0. Prepare the folder path which contains all excel files
$FolderPath = "C:\Users\username\Downloads\TEST"

Convert-CsvInBatch -Folder $FolderPath

Output with Error:

Windows PowerShell
Copyright (C) 2014 Microsoft Corporation. All rights reserved.

PS C:\Users\username> $ErrorActionPreference = 'Stop'
PS C:\Users\username>
PS C:\Users\username> Function Convert-CsvInBatch
>> {
>> [CmdletBinding()]
>> Param
>> (
>> [Parameter(Mandatory=$true)][String]$Folder
>> )
>> $ExcelFiles = Get-ChildItem -Path $Folder -Filter *.xlsx -Recurse
>>
>> $excelApp = New-Object -ComObject Excel.Application
>> $excelApp.DisplayAlerts = $false
>>
>> $ExcelFiles | ForEach-Object {
>> $workbook = $excelApp.Workbooks.Open($_.FullName)
>> $csvFilePath = $_.FullName -replace "\.xlsx$", ".csv"
>> $workbook.SaveAs($csvFilePath, [Microsoft.Office.Interop.Excel.XlFileFormat]::xlCSV)
>> $workbook.Close()
>> }
>>
>> # Release Excel Com Object resource
>> $excelApp.Workbooks.Close()
>> $excelApp.Visible = $true
>> Start-Sleep 5
>> $excelApp.Quit()
>> [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excelApp) | Out-Null
>> }
>>
PS C:\Users\username> #
PS C:\Users\username> # 0. Prepare the folder path which contains all excel files
PS C:\Users\username> $FolderPath = "C:\Users\username\Downloads\TEST"
PS C:\Users\username>
PS C:\Users\username> Convert-CsvInBatch -Folder $FolderPath
ForEach-Object : Unable to find type [Microsoft.Office.Interop.Excel.XlFileFormat]. Make sure that the assembly that
contains this type is loaded.
At line:11 char:15
+ $ExcelFiles | ForEach-Object {
+               ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Microsoft.Offic...el.XlFileFormat:TypeName) [ForEach-Object], Runtime
   Exception
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.ForEachObjectCommand
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros