【问题标题】:Save RDLC reports as PDF programmatically以编程方式将 RDLC 报告另存为 PDF
【发布时间】:2019-08-02 04:26:35
【问题描述】:

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

我用来将单个报告呈现为 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()

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

【问题讨论】:

  • 您是否尝试过使用 FileStream 并获得解决方案?

标签: vb.net pdf export rdlc


【解决方案1】:

你在这里有什么问题?是不是不行?

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

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

Public Sub CreerFichierRapport(ByVal strNomFichier As String, ByVal strFormat As String) Dim bytes() As Byte 将 strDeviceInfo 调暗为 String = "" 暗淡 strMimeType As String = "" 暗淡 strEncoding As String = "" 暗淡 strExtension As String = "" 将 strStreams() 调暗为字符串 暗淡警告()作为警告 将 oFileStream 调暗为 FileStream _stream = 新列表(流的) 尝试 bytes = rptViewer1.LocalReport.Render(strFormat,strDeviceInfo,strMimeType,strEncoding,strExtension,strStreams,警告) oFileStream = New FileStream(strNomFichier, FileMode.Create) oFileStream.Write(bytes, 0, bytes.Length) _stream.Add(oFileStream) 最后 If Not IsNothing(oFileStream) Then oFileStream.Close() oFileStream.Dispose() 万一 结束尝试 结束子

【讨论】:

  • 我需要用 字节数组HttpContext.Current.Response.BinaryWrite(renderedBytes)。是否有可能或我需要先将文件保存到磁盘,然后发送给客户端?
  • 什么是_stream变量?课堂上的静态?
【解决方案2】:

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

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

【讨论】:

  • 我使用 bytes arrayHttpContext.Current.Response.BinaryWrite(renderedBytes) 但有时我会得到 exception OutOfMemory也许首先将文件保存到 ASP.NET 服务器中的文件系统?
【解决方案3】:
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);
                }
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-12
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    • 2017-04-11
    相关资源
    最近更新 更多