vb.net 以编程方式将 RDLC 报告另存为 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1652203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:30:05  来源:igfitidea点击:

Save RDLC reports as PDF programmatically

vb.netpdfexportrdlc

提问by monkeypushbutton

I have a report that I need to run multiple times and save as PDFs. I am currently generating the report as a PDF programatically but want to save the reports without the user having to choose the save option manually each time.

我有一份报告需要多次运行并另存为 PDF。我目前正在以编程方式将报告生成为 PDF,但希望保存报告,而无需用户每次都手动选择保存选项。

The code I use to render a single report as a PDF is:

我用来将单个报告呈现为 PDF 的代码是:

    Dim warnings As Microsoft.Reporting.WebForms.Warning() = Nothing

    Dim streamids As String() = Nothing

    Dim mimeType As String = Nothing

    Dim encoding As String = Nothing

    Dim extension As String = Nothing

    Dim deviceInfo As String

    Dim bytes As Byte()

    Dim lr As New Microsoft.Reporting.WebForms.LocalReport

    deviceInfo = "<DeviceInfo><SimplePageHeaders>True</SimplePageHeaders></DeviceInfo>"

    bytes = ReportViewer1.LocalReport.Render("PDF", deviceInfo, mimeType, encoding, extension, streamids, warnings)

    Response.ClearContent()

    Response.ClearHeaders()

    Response.ContentType = "application/pdf"

    Response.BinaryWrite(bytes)

    Response.Flush()

    Response.Close()

I was figuring I could run it in a loop and save the PDF each time.

我想我可以循环运行它并每次都保存 PDF。

回答by David Brunelle

What's your question in here? Is it that it does not work?

你在这里有什么问题?是它不起作用吗?

here is an example of something we didin 2005. We defined a control called rptViewer1 which can be visible or not depending of your needs. strFormat should contain "PDF" and strNomFicher the full path.

这是我们在 2005 年所做的一个例子。我们定义了一个名为 rptViewer1 的控件,它可以根据您的需要可见或不可见。strFormat 应包含“PDF”和 strNomFicher 的完整路径。

BTW the variable names and functions are in french, but that will work anyway :)

顺便说一句,变量名和函数是法语,但无论如何都可以:)

    Public Sub CreerFichierRapport(ByVal strNomFichier As String, ByVal strFormat As String)
        Dim bytes() As Byte
        Dim strDeviceInfo As String = ""
        Dim strMimeType As String = ""
        Dim strEncoding As String = ""
        Dim strExtension As String = ""
        Dim strStreams() As String
        Dim warnings() As Warning
        Dim oFileStream As FileStream
        _stream = New List(Of Stream)
        Try
            bytes = rptViewer1.LocalReport.Render(strFormat, strDeviceInfo, strMimeType, strEncoding, strExtension, strStreams, warnings)

            oFileStream = New FileStream(strNomFichier, FileMode.Create)
            oFileStream.Write(bytes, 0, bytes.Length)
            _stream.Add(oFileStream)
        Finally
            If Not IsNothing(oFileStream) Then
                oFileStream.Close()
                oFileStream.Dispose()
            End If
        End Try
    End Sub

回答by Brian MacKay

David's answer was very helpful to me. I thought I would publish my simplified and (slightly) translated version of this code, since the original contained some French, and also a few references that weren't relevant:

大卫的回答对我很有帮助。我想我会发布此代码的简化和(稍微)翻译版本,因为原始版本包含一些法语,以及一些不相关的参考资料:

Imports Microsoft.Reporting.WebForms
Imports System.IO

Public Class RenderToPDF
    Public Sub Save(ByVal viewer As ReportViewer, ByVal savePath As String)
        Dim Bytes() As Byte = viewer.LocalReport.Render("PDF", "", Nothing, Nothing, Nothing, Nothing, Nothing)

        Using Stream As New FileStream(savePath, FileMode.Create)
            Stream.Write(Bytes, 0, Bytes.Length)
        End Using
    End Sub
End Class

回答by Sibi Babu

dt = c.ds.Tables["vt5"];
            ReportViewer1.Visible = true;
            ReportViewer1.LocalReport.ReportPath = "InvoiceSGST.rdlc";
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("Invoice", dt));
            ReportDataSource rds = new ReportDataSource("Invoice", dt);

            this.ReportViewer1.LocalReport.EnableExternalImages = true;
            ReportViewer1.LocalReport.Refresh();

            // Variables
            Warning[] warnings;
            string[] streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;

            ReportViewer viewer = new ReportViewer();
            viewer.ProcessingMode = ProcessingMode.Local;
            viewer.LocalReport.ReportPath = "InvoiceSGST.rdlc";
            viewer.LocalReport.DataSources.Add(rds); // Add datasource here
            viewer.LocalReport.EnableExternalImages = true;
            byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
            if (bytes != null)
            {
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-length", bytes.Length.ToString());
                Response.BinaryWrite(bytes);


                string strFilePath = @"C:\Temp\";
                string strFileName = "Blah.PDF";
                string filename = Path.Combine(strFilePath, strFileName);
                using (FileStream fs = new FileStream(filename, FileMode.Create))
                {
                    fs.Write(bytes, 0, bytes.Length);
                }
            }