Click here to Skip to main content
15,888,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm new to this, and this is out of my wheelhouse. I'm trying to come up with an automated process to ultimately load an excel spreadsheet with multiple tabs into MS SQL as individual tables. We use this for data conversion purposes.

From the initial reading I've done, I was thinking I could use PowerShell to split out the spreadsheet into individual files, but I'm getting stuck. I've browsed a few other topics on this but none of the solutions are working for me, and I haven't been able to figure it out. I've tried using .xlsx and .xls files. PowerShell 2.0.

What I have tried:

I've created a folder on my C drive called load, and that is where I'm placing the files. C:\Load\Load.xls and C:\Load\Load.ps1.

I'm pasting in the script I'm using below, and I'm attempting execute this from cmd using (I've totally borrowed all this from other posts I've seen BTW)


PowerShell.exe -command "C:\Load\Load.ps1" -filepath "C:\Load\Load.xls" -output_type "csv"

I'm getting an error saying

Method Invocation failed because [System.__ComObject] doesn't contain a method named 'SaveAs'
At C:\Load\Load.ps1 :50 char:26
+ $WorkSheet.SaveAs<<<< ($ExtractedFileName + $FileFormat)
+ CategoryInfo : Invalid Operation: (SaveAs:String) [],RuntimeException
+FullyQualifiedErrorId : MethodNotFound


this is the script in the ps1 file

[CmdletBinding()]
Param ( 
    [Parameter(Mandatory=$true,Position=0)] 
    [string]$filepath,
    [Parameter(Mandatory=$true,Position=1)] 
    [ValidateSet("csv","txt","xls","html")] 
    [string]$output_type 
)


#-----------------------------------------------------------------------------#


# Figures out and returns the 'XlFileFormat Enumeration' ID for the specified format.
# http://msdn.microsoft.com/en-us/library/office/bb241279%28v=office.12%29.aspx 
# NOTE: The code being used for 'xls' is actually a 'text' type, but it seemed
# to work the best for splitting the worksheets into separate Excel files.
function GetOutputFileFormatID 
{ 
Param([string]$fomat_name) 
    $Result = 0 
    switch($fomat_name) 
    { 
        "csv" {$Result = 6} 
        "txt" {$Result = 20} 
        "xls" {$Result = 21} 
        "html" {$Result = 44} 
        default {$Result = 51} 
    } 
    
    return $Result 
}

#-----------------------------------------------------------------------------# 
$Excel = New-Object -ComObject "Excel.Application" 
$Excel.Visible = $false #Runs Excel in the background. 
$Excel.DisplayAlerts = $false #Supress alert messages. 
$Workbook = $Excel.Workbooks.open($filepath) 
#Loop through the Workbook and extract each Worksheet
#     in the specified file type. if ($Workbook.Worksheets.Count -gt 0) { 
    write-Output "Now processing: $WorkbookName" 
    
    $FileFormat = GetOutputFileFormatID($output_type) 
    #Strip off the Excel extension. 
    $WorkbookName = $filepath -replace ".xlsx", "" #Post 2007 extension
    $WorkbookName = $WorkbookName -replace ".xls", "" #Pre 2007 extension 
    $Worksheet = $Workbook.Worksheets.item(1) 
    foreach($Worksheet in $Workbook.Worksheets) { 
        $ExtractedFileName = $Worksheet.Name + "." + $output_type 
        $Worksheet.SaveAs($ExtractedFileName + $FileFormat) 
        write-Output "Created file: $ExtractedFileName" 
    } 

#Clean up & close the main Excel objects. 
$Workbook.Close() 
$Excel.Quit()




Any thoughts?
Posted
Updated 13-Feb-16 12:22pm
v3
Comments
Sergey Alexandrovich Kryukov 13-Feb-16 16:38pm    
What thoughts? Someone needs to run it step by step to debug it. Lasest PowerShell development tool offers decent debugging capabilities; I enjoed using them; moreover, you can prototype separate operations interactively. Who would do it? Why not you? And then, if your problem is not yet solved, you will be able to point out more specific problem and show in what line is your concern. Yes, you provided this information: "50, char: 26". But who is going to count your line using just your post, how? You could at least add a comment on this line and refer to this comment in your question post...
—SA
RDBurmon 13-Feb-16 22:41pm    
Is use of Powershell mandatory in task? If I had the same exercise then I would have achieved it using C#. Let me know if that works for you. I know how to achieve this through C#

1 solution

An Excel file is a WorkBook, so it is not a surprise that a WorkSheet can(t be saved.
Quote:
Method Invocation failed because [System.__ComObject] doesn't contain a method named 'SaveAs'
At C:\Load\Load.ps1 :50 char:26
+ $WorkSheet.SaveAs<<<< ($ExtractedFileName + $FileFormat)
+ CategoryInfo : Invalid Operation: (SaveAs:String) [],RuntimeException
+FullyQualifiedErrorId : MethodNotFound

I would try :
- Create a new WorkBook
- Copy the WorkSheet to the new WorkBook
- save the new WorkBook
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900