I'm currently writing a script in powershell that searches for XLS files recursivly through a file tree. I'd like to save all the files to xml to be able to read through them and change data as needed.
If you want to create the xml file as the format you posted, you can refer to the object System.XMl.XmlTextWriter in powershell to manually create the xml file, and the script below is for your reference:
Download convert xls to xml powershell
Your script does not really convert Excel to XML. It loads a CSV file as a collection of custom objects and exports it as "command line interpreter XML" file which is a custom format used to save PowerShell objects.
This cmdlet is similar to Export-Clixml except that Export-Clixml stores theresulting XML in aCommon Language Infrastructure (CLI)file that can be reimported as objects with Import-Clixml. ConvertTo-Xmlreturns an in-memory representation of an XML document, so you can continue to process it inPowerShell. ConvertTo-Xml doesn't have an option to convert objects to CLI XML.
There is a lot more to this module to check out. Try the Import-Excel function, which lets you read an Excel spreadsheet and convert it to PowerShell objects. It lends itself to taking existing spreadsheets, applying a calculation (or adding more data from a look up), and quickly creating a new spreadsheet. Here is an example:
The ConvertTo-Json cmdlet converts any .NET object to a string in JavaScript Object Notation(JSON) format. The properties are converted to field names, the field values are converted toproperty values, and the methods are removed.
This example uses the ConvertTo-Json cmdlet to convert a System.DateTime object from theGet-Date cmdlet to a JSON-formatted string. The command uses the Select-Object cmdlet to get all(*) of the properties of the DateTime object. The output shows the JSON string thatConvertTo-Json returned.
Importing the converted Registry path into Group Policy Preferences is simple, too, although not self-evident. You have to right-click the XML file in File Explorer and then copy it to the Windows clipboard.
Thanks to Doug Finke and his awesome free module ImportExcel, reading and writing .xlsx files is a snap now - no Office installation required. Simply download and install this free module from the PowerShell Gallery:
Finding excel sample data is easy: just google for Download Excel Sample Data to come up with urls. They come as individual files and ZIP archives. To make downloading a pleasant experience, I created a bunch of helper functions.
The good news is: provided you have excel installed, it is trivial to convert .xls files to .xlsx files. If you are really still using .xls files, you should consider this transform for good. .xls is really outdated and should no longer be used.
Below is a function Convert-XlsToXlsx that auto-converts .xls files to .xlsx and .xlsm files, though. The script requires Microsoft Office to be installed on your box because only excel knows how to open the binary format used in .xls files:
When you run a SAS program on a remote session, sometimes the expected result is more substantial than just a text-based listing. What if your program creates Output Delivery System (ODS) files such as HTML or graphics? To retrieve those results, you need to download the files from the SAS session to your local machine, where your application can access them. Consider this program, implemented in a PowerShell script, which creates an image with PROC SGPLOT:# change these to your own SAS-session-based# file path and file name# Note that $destImg can't be > 7 chars$destPath = "/projects/results"$destImg = "hist"# program to run# could be read from external file$program = "ods graphics / imagename='$destImg'; ods listing gpath='$destPath' style=plateau; proc sgplot data=sashelp.cars; histogram msrp; density msrp; run;"# run the program$objSAS.LanguageService.Submit($program);When the program runs, it creates a file named hist.png in the /projects/results folder within the SAS session.
The SAS Workspace provides the FileService API, which allows you to transfer file-based content between your local application and the SAS session. As with most file-based operations in SAS, the FileService relies on the use of a SAS fileref, or the name that SAS uses to reference your file within a program. For a file download operation, these are the basic steps:
Here's a PowerShell program snippet that implements these steps. (The complete example is available on GitHub.)# local directory for downloaded file$localPath = "c:\temp"# now download the image file$fileref = ""# assign a Fileref so we can use FileService from IOM$objFile = $objSAS.FileService.AssignFileref( "img", "DISK", "$destPath/$destImg.png", "", [ref] $fileref);$StreamOpenModeForReading = 1$objStream = $objFile.OpenBinaryStream($StreamOpenModeForReading)# define an array of bytes[Byte[]] $bytes = 0x0$endOfFile = $false$byteCount = 0$outStream = [System.IO.StreamWriter] "$localPath\$destImg.png"do # read bytes from source file, 1K at a time $objStream.Read(1024, [ref]$bytes) # write bytes to destination file $outStream.Write($bytes) # if less than requested bytes, we're at EOF $endOfFile = $bytes.Length -lt 1024 # add to byte count for tally $byteCount = $byteCount + $bytes.Length while (-not $endOfFile)# close input and output files$objStream.Close()$outStream.Close()# free the SAS fileref$objSAS.FileService.DeassignFileref($objFile.FilerefName)Write-Output "Downloaded $localPath\$destImg.png: SIZE = $byteCount bytes"Here's an excerpt from the output when the script runs:NOTE: PROCEDURE SGPLOT used (Total process time): real time 13.18 seconds cpu time 0.12 seconds NOTE: Listing image output written to /projects/result/hist.png.NOTE: There were 428 observations read from the data set SASHELP.CARS.Downloaded c:\temp\hist.png: SIZE = 15092 bytes
Suppose, I were to write some ODS output to a temporary location, so that the residual output is not retained on the server...much like EG's result destination. I would need to download the result before the server releases the WORK library. So, as part of the process flow, I might better able to insure the server does not timeout before I return to run the download script externally.
Alternatively, I have the ideal that I might be able schedule a process flow and then insert the your download script into the scheduling script before the project is saved and closed (and the server shutdown). Ideas or problems (like does your script start a separate server)?
Chris,I would like to know how to use PowerShell to convert an XML file I've output from SAS (run in batch of course) to xlsx without manually opening it by Point and Click means. Running Invoke-item amounts to almost the same thing and isn't what I have in mind. I'm talking about an XML file I've produced with the ExcelXP tagset.Adeline.
Adeline, you could use PowerShell to automate the Excel application, open the XML file that you created from ExcelXP, and then automate the SaveAs operation...thus converting the file into the native file format. Something like this:$Excel = New-Object -Com Excel.Application$Excel.Visible = $false$Excel.DisplayAlerts = $false$WorkBook = $Excel.Workbooks.Open("$localPath\out.xml")$WorkBook.SaveAs("$localPath\out.xlsx",51)$WorkBook.Close
As already mentioned, Excel's Save As command is only able to convert an active worksheet. But what if your workbook contains a lot of different sheets, and you wish to turn them all into separate csv files? The only alternative suggested by Microsoft is saving each sheet under a different file name, which does not sound very inspiring, huh?
The below code converts all worksheets in the current workbook to individual CSV files, one for each sheet. The file names are created from the workbook and sheet names (WorkbookName_SheetName.csv) and saved to the same folder as the original document.
Please keep in mind that the above code saves sheets in the CSV format. If there are any non-ASCII characters in your data, then you need to convert to UTF-8 CSV. This can be done by changing the file format from xlCSV to xlCSVUTF8. That is, you replace FileFormat:=xlCSV with FileFormat:=xlCSVUTF8.
I came across this solution on some forum, cannot remember which exactly. To be honest, this method has never worked for me, but many users reported that special characters, which got lost when saving .xlsx directly to .csv, are preserved if to save a .xlsx file to .xls first, and and then save .xls as .csv as explained in How to convert Excel to CSV.
Hi thereThanks so much for this - after battling to simply 'save as csv.' the option of converting xlsx to xls to csv. helped as I was attempting to import a database of email addresses into GMAIL. What a relief! THANK YOU!!
HelloI have a problem when I try to convert Excel spreadsheet to CSV and cell containing text inside quotes like "Hello" converts always as """Hello""" in the CSV file. I'm running out of ideas, can you please help me to find the way to keep the data unchanged, Thanks. Antero
Hello, Thanks for posting about "Save .xlsx to .xls and then convert to .csv file"! This worked very well for me! Other methods were creating csv files that were not opening in Excel in the way that I wanted.
The solution to convert excel to a unicode text and then replacing the delimiter tab with comma or any other user specific delimiter should work. But what if my data is having tab in between the text? Can you suggest a way to convert those excel to UTF encoded CSV or a custom delimited file?Thanks.
Excellent, the section "How to convert Excel to CSV UTF-8" was precisely the solution that I needed for my problem!Excel kept saving the csv with semicolons instead of commas, despite having changed the regional settings as had been recommended elsewhere. With this work around I did get the right csv file to import in another system. 2ff7e9595c
Comments