不使用 Report Viewer, 实现调用 Reporting Service 来实现直接输出报表。 .

5 篇文章 0 订阅
   因项目的需要,在VS2003 中调用一个VS 2005 的Reporting Service Report,而无法使用 Report Viewer 控件,故必须使用 Web Service 的方式。最终成功的代码如下:

 

    但有几个问题:

1, 无法实现让用户在报表页面选择输出格式,即显示对应的Toolbar,然后让用户选择不同的 Export Format;

2, 在代码写的方式,目前已经能实现 HTML / PDF / Excel / Word 方式的输出。

 

* 之前无法输入Excel ,原来原因仅是因为 ContentType 中的值写的不正规,非"application/excel",而是"application/vnd.ms-excel".

 

 

    Public Function OpenReportWeb(ByVal p_ReportFormat As ReportType, Optional ByVal p_RS_Parameter() As ReportParameter = Nothing) As Byte()
        Dim rs As New ReportingService
        Dim nc As System.Net.NetworkCredential
        Dim cc As New System.Net.CredentialCache

        nc = New Net.NetworkCredential
        nc.UserName = sUserName
        nc.Password = sPassword
        cc.Add(New Uri(zUrl), "NTLM", nc)
        rs.Credentials = cc

        rs.Url = zUrl

        Dim credentials() As DataSourceCredentials
        Dim cc2() As DataSourceCredentials

        Dim reportDefinition As Byte() = Nothing
        Dim result As Byte() = Nothing
        Dim sFormat As String = "EXCEL"  'OK : HTML4.0 / PDF ; Not OK : MHTML / EXCEL

        Select Case p_ReportFormat
            Case ReportType.HTML
                sFormat = "HTML4.0"
            Case ReportType.PDF
                sFormat = "PDF"
            Case ReportType.Excel
                sFormat = "EXCEL"
        End Select

        Dim sHistoryID As String = Nothing
        Dim sDevInfo As String = "<DeviceInfo><Parameters>False</Parameters></DeviceInfo>" '
        Dim parameters() As ParameterValue

        Dim showHideToggle As String = Nothing
        Dim encoding As String
        Dim mimeType As String = "application/excel"
        Dim warnings As Warning() = Nothing
        Dim reportHistoryParameters As ParameterValue() = Nothing
        Dim streamIDs As String() = Nothing
        Dim sh As New SessionHeader
        rs.SessionHeaderValue = sh

        Dim definition As New DataSourceDefinition
        definition.CredentialRetrieval = CredentialRetrievalEnum.Prompt
        definition.ConnectString = sConnStr
        definition.Enabled = True
        definition.Extension = "SQL"

        Try
            Dim dsDefinition As DataSourceDefinition
            Dim strURL As String
            Try
                dsDefinition = rs.GetDataSourceContents(sReportDataSourceName)
                If Not (dsDefinition Is Nothing) Then
                    dsDefinition.ConnectString = sReportDataSource
                    dsDefinition.UserName = sSqlLoginID
                    dsDefinition.Password = sSqlLoginPwd
                    rs.SetDataSourceContents(sReportDataSourceName, dsDefinition)
                End If
            Catch ex As Exception
                Throw ex
            End Try

            If Not IsNothing(p_RS_Parameter) Then
                If p_RS_Parameter.Length > 0 Then
                    rs.SetReportParameters(sReportName, p_RS_Parameter)
                End If
            End If

            rs.RequestEncoding = System.Text.UTF8Encoding.UTF8
            result = rs.Render(sReportName, sFormat, sHistoryID, sDevInfo, Nothing, cc2, showHideToggle, encoding, mimeType, reportHistoryParameters, warnings, streamIDs)


            sh.SessionId = rs.SessionHeaderValue.SessionId

            '========================================================
            'Response.Clear();  
            HttpContext.Current.Response.ClearHeaders()
            HttpContext.Current.Response.ClearContent()

            Dim sContentType As String
            Select Case p_ReportFormat
                Case ReportType.HTML
                    sContentType = "text/html"
                Case ReportType.PDF
                    sContentType = "text/html"
                Case ReportType.Excel
                    sContentType = "application/vnd.ms-excel"
            End Select
            '="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/ms-word  
            HttpContext.Current.Response.ContentType = sContentType
            HttpContext.Current.Response.AppendHeader("Context-HttpContext.Current.Disposition", "filename='Report'")
            HttpContext.Current.Response.BinaryWrite(result)
            HttpContext.Current.Response.End()

 

            '========================================================

        Catch ex As SoapException
            Throw (ex)
        End Try

        Return result
    End Function

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值