Imports Microsoft.Reporting.WebForms
Imports System.Xml
Imports System.Xml.XPath
Public Class ExportRDLCReport
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ExportReport()
End Sub
Private Sub CreateTempRDLCFile()
Dim doc As New System.Xml.XmlDocument
Dim objXmlNamespaceManager As New XmlNamespaceManager(doc.NameTable)
objXmlNamespaceManager.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner")
objXmlNamespaceManager.AddNamespace("d", "http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition")
doc.Load(Server.MapPath("~") & "\Reports\OriginalReport.rdlc")
Dim navigator As XPathNavigator = doc.CreateNavigator()
Dim arr(3, 23) As String
Dim index As Integer = 0
'Find TablixColumn Node From xml Doc and save xml data in 0th row of Array
Dim nodes As XmlNodeList = doc.SelectNodes("//d:TablixColumn", objXmlNamespaceManager)
For _index As Short = 0 To 23
arr(index, _index) = nodes.ItemOf(_index).InnerXml
Next
index += 1
'Find TablixCell Node From xml Doc and save xml data in 1st and 2nd row of Array
Dim xpath As String = "/d:Report/d:Body/d:ReportItems/d:Tablix/d:TablixBody/d:TablixRows/d:Tablix"
'Row("")
Dim nodes1 As XmlNodeList = doc.SelectNodes(xpath, objXmlNamespaceManager)
For Each nod As XmlNode In nodes1
Dim childnode1 As XmlNodeList = nod.SelectNodes("child::d:TablixCells/d:TablixCell", objXmlNamespaceManager)
For _index As Short = 0 To 23
arr(index, _index) = childnode1.ItemOf(_index).InnerXml
Next
index += 1
Next
'Find TablixMember Node From xml Doc and save xml data in 1st and 2nd row of Array
Dim xpathParam As String = "/d:Report/d:Body/d:ReportItems/d:Tablix/d:TablixColumnHierarchy"
nodes1 = doc.SelectNodes(xpathParam, objXmlNamespaceManager)
For Each nod As XmlNode In nodes1
Dim childnode1 As XmlNodeList = nod.SelectNodes("child::d:TablixMembers/d:TablixMember", objXmlNamespaceManager)
For _index As Short = 0 To 23
arr(index, _index) = childnode1.ItemOf(_index).InnerXml
Next
Next
'Set comma seperated list of column number to be displayed in exported report
'please note that you need to add all columns in your original report here otherwise
'it will give error
Dim tempStrColumnSortOrder As String = ",5,3,2,4,1,"
'Rewrite xml as doc as per new sortOrder of columns and save it in temp(file)
Dim strNewSortOrder() As String = Split(tempStrColumnSortOrder.TrimEnd(CChar(",")), ",")
Dim _nexIndex As Short = 0
For i As Short = 0 To CShort(strNewSortOrder.Length - 1)
Dim childnode0 As XmlNodeList = doc.SelectNodes("//d:TablixColumn", objXmlNamespaceManager)
childnode0.ItemOf(_nexIndex).InnerXml = arr(0, CInt(strNewSortOrder(i)))
Dim childnode1 As XmlNodeList = doc.SelectNodes(xpath, objXmlNamespaceManager).ItemOf(0).SelectNodes("child::d:TablixCells/d:TablixCell", objXmlNamespaceManager)
childnode1.ItemOf(_nexIndex).InnerXml = arr(1, CInt(strNewSortOrder(i)))
Dim childnode2 As XmlNodeList = doc.SelectNodes(xpath, objXmlNamespaceManager).ItemOf(1).SelectNodes("child::d:TablixCells/d:TablixCell", objXmlNamespaceManager)
childnode2.ItemOf(_nexIndex).InnerXml = arr(2, CInt(strNewSortOrder(i)))
Dim childnode3 As XmlNodeList = doc.SelectNodes(xpathParam, objXmlNamespaceManager).ItemOf(0).SelectNodes("child::d:TablixMembers/d:TablixMember", objXmlNamespaceManager)
childnode3.ItemOf(_nexIndex).InnerXml = arr(3, CInt(strNewSortOrder(i)))
_nexIndex = CShort(_nexIndex + 1)
Next
doc.Save(Server.MapPath("~") & "\Reports\TempReport.rdlc")
End Sub
Sub ExportReport()
Dim ab As New Microsoft.Reporting.WebForms.LocalReport()
Dim dsSource As DataSet ''Your dataset for report
ab.ReportPath = "Reports\OriginalReport.rdlc"
CreateTempRDLCFile()
ab.ReportPath = "Reports\TempReport.rdlc"
ab.ShowDetailedSubreportMessages = True
Dim paramList As New List(Of Global.Microsoft.Reporting.WebForms.ReportParameter)
paramList.Add(New ReportParameter("prm1", CStr((True)), False))
ab.SetParameters(paramList)
ab.DataSources.Add(New ReportDataSource("GroupDetail", dsSource.Tables(0).DefaultView))
ab.DataSources.Add(New ReportDataSource("ContactDetail", dsSource.Tables(1)))
ab.DataSources.Add(New ReportDataSource("GroupAddress", dsSource.Tables(2)))
AddHandler (ab.SubreportProcessing), AddressOf Me.SetSubDataSource
ab.Refresh()
Dim results As Byte() = Nothing
Dim mimeType As String = String.Empty
Dim extension As String = String.Empty
Dim encoding As String = String.Empty
Dim streamIDs As String() = Nothing
Dim warnings As Warning() = Nothing
Dim name As String = ""
Dim type As String = ""
Dim strFileName As String = "Your Required File Name"
strFileName = strFileName.Replace("\", "")
strFileName = strFileName.Replace("/", "")
strFileName = strFileName.Replace(":", "")
strFileName = strFileName.Replace("?", "")
strFileName = strFileName.Replace("*", "")
strFileName = strFileName.Replace("<", "")
strFileName = strFileName.Replace(">", "")
strFileName = strFileName.Replace("|", "")
'IF want to export as Excel
results = ab.Render("Excel", Nothing, mimeType, encoding, extension, streamIDs, warnings)
name = strFileName & ".xls"
type = "Application/x-msexcel"
'If want to export as PDF
results = ab.Render("Pdf", Nothing, mimeType, encoding, extension, streamIDs, warnings)
name = strFileName & ".pdf"
type = "Application/pdf"
HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=" + name)
If type <> "" Then
HttpContext.Current.Response.ContentType = type
End If
If Not IsNothing(results) Then
HttpContext.Current.Response.BinaryWrite(results)
HttpContext.Current.Response.End()
End If
End Sub
Public Sub SetSubDataSource(ByVal sender As Object, ByVal e As SubreportProcessingEventArgs)
e.DataSources.Add(New ReportDataSource("AdventureWorksDataSet_Contact"))
End Sub
End Class
Wednesday, September 14, 2011
Export RDLC Report in Asp.Net
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment