从Webforms应用程序C#打印SSRS报告

时间:2015-10-07 05:22:08

标签: c# asp.net reporting-services webforms

我试图从用C#编写的webforms应用程序打印SSRS报告。该报告将加载到reportviewer中,到目前为止我的代码是:

    protected void printAll(object sender, EventArgs e)
    {
        //loads report into reportviewer
        showReport("/SHOW_ALL");

        string mimeType;
        string encoding;
        string extension;
        string[] streams;
        Warning[] warnings;
        byte[] pdfBytes = ReportViewer.ServerReport.Render("IMAGE", string.Empty, out mimeType,
            out encoding, out extension, out streams, out warnings);

        string outputPath = "C://Temp/";
        string outputFile = Path.GetFileName(ReportViewer.ServerReport.ReportPath) + "_" + client.FullName + ".tiff";
        string fullOutput = outputPath + outputFile;

        if (File.Exists(fullOutput))
        {
            File.Delete(fullOutput);
        }
        using (FileStream fs = new FileStream(fullOutput, FileMode.Create))
        { 
            fs.Write(pdfBytes, 0, pdfBytes.Length);
            fs.Close();
        }


    }

我只想将图像写入iFrame并将其打印出来。我已经尝试过PDF但是IE没有渲染任何内容(没有内置的pdf查看器),如果我使用.tiff选项,那么除了第一页之外没有办法打印任何东西。我已经浏览了许多与winforms或其他各种技术有关的文章,到目前为止还没有人帮助过。跨浏览器的东西会很好,但目前我只是想找一些最适合IE的东西。

1 个答案:

答案 0 :(得分:0)

这是我在之前的项目中尝试过的解决方案。希望这有帮助。

private void Export(ServerReport report)
    {
        Microsoft.Reporting.WebForms.Warning[] webWarning = null;

        m_streams = new List<Stream>();

        NameValueCollection nvc = new NameValueCollection();

        string extension = null;
        string encoding = null;
        string mimeType = null;
        string type = "HTML4.0";

        Stream stream = report.Render(type, null, nvc, out mimeType, out extension);
        StreamReader f = new StreamReader(stream);
        string strHtml = f.ReadToEnd();

        int startStyle = strHtml.IndexOf("<style", StringComparison.OrdinalIgnoreCase);
        int closeStyle = strHtml.IndexOf("</style>", StringComparison.OrdinalIgnoreCase);
        string style = strHtml.Substring(startStyle, closeStyle - startStyle) + "</style>";

        string body = strHtml.Substring(strHtml.IndexOf("<body", StringComparison.OrdinalIgnoreCase));
        body = body.Replace("</html>", "");
        body = body.Replace("<body", "<div");
        body = body.Replace("</body>", "</div>");

        string script = @"
        <script type='text/javascript'>
            function print_me(idx) {
                var browser = navigator.userAgent;
                if (browser.indexOf('MSIE') >= 0) {
                    document.execCommand('print', false, null);
                }
                else {
                    window.print();
                }

            }

            $(function () {
                print_me();
            });
        </script>";

        expandPrintHtml.InnerHtml = style + body + script;           

    }