Quantcast
Channel: get-command | PowerShell » XML
Viewing all articles
Browse latest Browse all 3

Creating Excel tables on the fly

$
0
0

When I need to get the output of a one-liner quickly to an excel file, I often use this (xml)xls generating function.

# Example Use: get-process | select name, handles | convertto-xls | out-file ps.xls
function Convertto-Xls 
{
  param($input)
 
  $header = @"
<?xml version='1.0'?>
<?mso-application progid='Excel.Sheet'?>
<s:Workbook xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:s="urn:schemas-microsoft-com:office:spreadsheet">
  <s:Worksheet s:Name="Sample">
    <s:Table>
"@
 
  $body = 
    ($input | Convertto-Html | where {$_ -like "<tr>*</tr>"} |
	foreach {
			$_ -replace "<th>","<s:Cell><s:Data s:Type=`"String`">"
                 } |
	foreach {
			$_ -replace "<td>","<s:Cell><s:Data s:Type=`"String`">"
                 } |
	foreach {
			$_ -replace "<tr>","<s:Row>"
		 } |
	foreach {
			$_ -replace "</tr>","</s:Row>"
		 } |
	foreach {
			$_ -replace "</th>","</s:Data></s:Cell>"
		 } |
	foreach {
			$_ -replace "</td>","</s:Data></s:Cell>"
		 }
    )
 
  $footer = @"
    </s:Table>
  </s:Worksheet>
</s:Workbook>
"@
  $header + $body + $footer
}

It’s faster than COM objects and more culture-friendly than the original export-csv.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles



Latest Images