PoshCode Logo PowerShell Code Repository

Out-Report by Chad MIller 7 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1015"></script>download | new post

Sends output to Excel, PDF, or Image files

  1. #Depends on Microsoft Report Viewer Redistributable and ReportExporters
  2. #ReportExporters available at http://www.codeproject.com/KB/reporting-services/ReportExporters_WinForms.aspx
  3. #Download demo version of ReportExporters for compiled dlls
  4. #Tested with Microsoft Report Viewer 2008 SP1 Redistributable, although 2005 and base 2008 version should work
  5. #Visual Studio installations as well as SQL Server Reporting Services may already include Microsoft Report Viewer
  6. #Microsoft Report Viewer Redist available at http://www.microsoft.com/downloads/details.aspx?familyid=BB196D5D-76C2-4A0E-9458-267D22B6AAC6&displaylang=en
  7. #EXAMPLES
  8. #get-alias | ./out-report.ps1 "c:\users\u00\bin\aliases.xls" xls
  9. #get-alias | ./out-report.ps1 "c:\users\u00\bin\aliases.pdf" pdf
  10. #get-alias | ./out-report.ps1 "c:\users\u00\bin\aliases.jpeg" -filetype image -imagetype JPEG -height 22 -width 14
  11.  
  12. param($fileName,$fileType,$imageType,$height=11,$width=8.5)
  13.  
  14. $libraryDir = Convert-Path (Resolve-Path "$ProfileDir\Libraries")
  15. [void][reflection.assembly]::LoadWithPartialName("Microsoft.ReportViewer.WinForms")
  16. [void][Reflection.Assembly]::LoadFrom("$libraryDir\ReportExporters.Common.dll")
  17. [void][Reflection.Assembly]::LoadFrom("$libraryDir\ReportExporters.WinForms.dll")
  18.  
  19. $fileTypes = 'XLS','PDF','IMAGE'
  20. if (!($fileTypes -contains $fileType))
  21. { throw 'Valid file types are XLS, PDF, IMAGE' }
  22.  
  23. $imageTypes = 'BMP','EMF','GIF','JPEG','PNG','TIFF'
  24. if ( $imageType -and !($imageTypes -contains $imageType))
  25. { throw 'Valid image types are BMP,EMF,GIF,JPEG,PNG or TIFF' }
  26.  
  27. #######################
  28. function New-ImageDeviceInfo
  29. {
  30.     param($imageType,$height,$width)
  31.  
  32.     $deviceInfo = new-object ("ReportExporters.Common.Exporting.ImageDeviceInfoSettings") $imageType
  33.     $deviceInfo.PageHeight = new-object System.Web.UI.WebControls.Unit($height,[System.Web.UI.WebControls.UnitType]::Inch)
  34.     $deviceInfo.PageWidth = new-object System.Web.UI.WebControls.Unit($width,[System.Web.UI.WebControls.UnitType]::Inch)
  35.     $deviceInfo.StartPage = 0
  36.     return $deviceInfo
  37.  
  38. } #New-ImageDeviceInfo
  39.  
  40. #DataTable section from http://mow001.blogspot.com/2006/05/powershell-out-datagrid-update-and.html
  41. $dt = new-Object Data.datatable  
  42.   $First = $true  
  43.   foreach ($item in $input){  
  44.     $DR = $DT.NewRow()  
  45.     $Item.PsObject.get_properties() | foreach {  
  46.       If ($first) {  
  47.         $Col =  new-object Data.DataColumn  
  48.         $Col.ColumnName = $_.Name.ToString()  
  49.         $DT.Columns.Add($Col)       }  
  50.       if ($_.value -eq $null) {  
  51.         $DR.Item($_.Name) = "[empty]"  
  52.       }  
  53.       ElseIf ($_.IsArray) {  
  54.         $DR.Item($_.Name) =[string]::Join($_.value ,";")  
  55.       }  
  56.       Else {  
  57.         $DR.Item($_.Name) = $_.value  
  58.       }  
  59.     }  
  60.     $DT.Rows.Add($DR)  
  61.     $First = $false  
  62.   } #End DataTable section  
  63.  
  64. $ds = new-object System.Data.dataSet
  65. $ds.merge($dt)
  66. $dsaProvider = new-object ReportExporters.Common.Adapters.DataSetAdapterProvider $ds
  67. $dsa = $dsaProvider.GetAdapters()
  68. $reportExporter = new-object ReportExporters.WinForms.WinFormsReportExporter $dsa
  69.  
  70. switch ($fileType)
  71. {
  72.     'XLS'   { $content =$reportExporter.ExportToXls() }
  73.     'PDF'   { $content = $reportExporter.ExportToPdf() }
  74.     'IMAGE' { $deviceInfo = New-ImageDeviceInfo $imageType $height $width; $content =  $reportExporter.ExportToImage($deviceInfo) }
  75. }
  76.  
  77. [System.IO.File]::WriteAllBytes($fileName,$content.ToArray())

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me