#Copyright (c), Bryan O ' Connell#license:http://bryanoconnell.blogspot.com/p/licenses.html#Purpose:extract all of the worksheets from a Excel file into separate files.[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 ' was actually a ' text ' type, but it seemed#The best for splitting the worksheets into separate Excel files.functiongetoutputfileformatid{Param([string]$fomat _name) $Result= 0Switch($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-GT0) {Write-output"Now processing: $WorkbookName" $FileFormat= Getoutputfileformatid ($output _type) #Strip off the Excel extension. $WorkbookName=$filepath -replace ". xlsx","" #Post Extension $WorkbookName=$WorkbookName -replace ". xls","" #Pre Extension #$Worksheet = $Workbook. Worksheets.item (1) foreach($Worksheet inch $Workbook. Worksheets) {$ExtractedFileName=$WorkbookName+"~~"+$Worksheet. Name +"."+$output _type $Worksheet. SaveAs ($ExtractedFileName,$FileFormat) Write-output"Created file: $ExtractedFileName" }}#Clean up & Close the main Excel objects.$Workbook. Close ()$Excel. Quit ()
Http://bryanoconnell.blogspot.jp/2012/09/extract-worksheets-from-excel.html
XLSX to CSVs (general)