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