2015-10-07 93 views
0

我試圖從用C#編寫的webforms應用程序打印SSRS報告。該報告被加載到的ReportViewer這裏是我到目前爲止的代碼:從Webforms應用程序打印SSRS報告C#

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的工具。

回答

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;   

    } 
+0

感謝您的回答。在我的場景'expandPrintHtml'是一個iFrame,運行時,這段代碼似乎只是掛起,根本不會更新innerHtml。你使用了什麼樣的元素? – DylanB

+0

您可以使用