从代码打印SSRS报告(asp.net)

时间:2017-05-26 13:40:33

标签: asp.net reporting-services

我找到了有关如何自动下载文件到客户广告的良好信息,作为如何使用代码进行打印的解决方案 (https://forums.asp.net/t/1233841.aspx?How+do+I+print+from+Reporting+Services+AUTOMATICALLY+in+VB+Net+web+app+) 但我需要做的是让代码在没有用户交互的情况下打印文档。

从我发现的情况来看,这似乎无法做到,因为人们可能随便想一想。例如,ReportViewer没有'print'方法。 只有两个解决方案似乎是使用ProcessStart(这意味着在打印之前将文件保存到文件系统,我不想这样做) 或者(今天将研究这个)使用代码创建订阅,然后稍后删除它。

1 个答案:

答案 0 :(得分:0)

您无法直接从asp.net页面打印报告。原因是安全性。如果它允许您通过网络将文件发送到客户端计算机,然后在计算机上搜索打印机,则可能会导致严重的安全问题。报表查看器确实有一个打印图标,但在部署项目并远程运行页面时,这会消失。我过去遇到过同样的问题,发现最好只将报告导出为PDF并允许用户下载。我过去使用以下代码来完成此任务:

  Private Sub CreatePDFMatrix(fileName As String)
    '  ReportViewer1.LocalReport.DataSources.Clear()
    Dim adapter As New ReportDataSetTableAdapters.vwPrintTagsTableAdapter
    Dim table As New ReportDataSet.vwPrintTagsDataTable
    Dim month = MonthName(Date.Today.Month)
    Dim year = Date.Today.Year
    Dim p(1) As ReportParameter
    Dim warnings() As Warning
    Dim streamIds As String()
    Dim mimeType As String = String.Empty
    Dim encoding As String = String.Empty
    Dim extension As String = String.Empty
    Dim adpt2 As New ReportDataSetTableAdapters.vwPrintTagsTableAdapter


        adapter.FillForMainReport(table, DropDownList1.SelectedValue, g_Company, g_Division)


    Me.ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("DataSet1", CType(table, DataTable))) 'Add(New ReportDataSource("ReportingData", CType(table, DataTable)))
    Me.ReportViewer1.DataBind()
    Dim viewer = ReportViewer1
    viewer.ProcessingMode = ProcessingMode.Local
    viewer.LocalReport.ReportPath = "Report1.rdlc"
    p(0) = New ReportParameter("MonthYear", month & "-" & year)
    Dim check = DropDownList1.SelectedValue
    ReportViewer1.LocalReport.SetParameters(p(0))
    p(1) = New ReportParameter("Location", DropDownList1.SelectedValue)
    ReportViewer1.LocalReport.SetParameters(p(1))

    Try
        Dim bytes As Byte() = viewer.LocalReport.Render("PDF", Nothing, mimeType, encoding, ".pdf", streamIds, warnings)

        Response.Buffer = True
        Response.Clear()
        Response.ContentType = mimeType
        Response.AddHeader("content-disposition", Convert.ToString((Convert.ToString("attachment; filename=") & fileName) + ".") & extension)
        Response.BinaryWrite(bytes)
        ' create the file
        Response.Flush()
    Catch ex As Exception
        Debug.Print(ex.ToString)
    End Try

End Sub