RDLC(Reportview)報表直接打印,支持所有瀏覽器,客戶可在linux下瀏覽使用


最近在做一個打印清單的,但是rdlc報表自帶的工具欄中的打印按鈕只有在ie內核下的瀏覽器才可以使用(其他的就會 隱藏),這導致了使用火狐和谷歌瀏覽器還有使用linux系統的客戶打印成了問題,於是就自己百度搜,谷歌搜,然后就解決了,下面放上源碼

打印類 PrintHelp(我需要兩個table座位數據源所以參數中有兩個datatable)

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.IO;
  6 using Microsoft.Reporting.WebForms;
  7 using System.Drawing.Printing;
  8 using System.Drawing.Imaging;
  9 using System.Text;
 10 using System.Data;
 11 
 12 /// <summary>
 13 /// PrintHelp 的摘要描述
 14 /// </summary>
 15 public class PrintHelp
 16 {
 17   
 18 
 19     private int m_currentPageIndex;
 20     private IList<Stream> m_streams;
 21     /// <summary>
 22     /// 
 23     /// </summary>
 24     /// <param name="reportPath">報表路徑</param>
 25     /// <param name="printerName">打印機名稱(使用默認打印機,不賦值)</param>
 26     /// <param name="dt1">報表數據源1</param>
 27     /// <param name="dt1SourceName">報表中數據源1對應名稱</param>
 28     /// <param name="dt2">報表數據源2</param>
 29     /// <param name="dt2SourceName">報表中數據源2對應名稱</param>
 30     public void Run(string reportPath, string printerName, DataTable dt1, string dt1SourceName, DataTable dt2, string dt2SourceName,bool isHindeLogo)
 31     {
 32         LocalReport report = new LocalReport();
 33         report.ReportPath = reportPath;//加上報表的路徑
 34         report.DataSources.Add(new ReportDataSource(dt1SourceName, dt1));
 35         report.DataSources.Add(new ReportDataSource(dt2SourceName, dt2));
 36         report.EnableExternalImages = true;
 37         ReportParameter rp = new ReportParameter("isHindeLogoImg", isHindeLogo.ToString());//這里我在報表里弄的參數
 38         report.SetParameters(rp);
 39         Export(report);
 40         m_currentPageIndex = 0;
 41         Print(printerName);
 42     }
 43 
 44     private void Export(LocalReport report)
 45     {
 46         string deviceInfo =
 47           "<DeviceInfo>" +
 48           "  <OutputFormat>EMF</OutputFormat>" +
 49           "  <PageWidth>210mm</PageWidth>" +
 50           "  <PageHeight>297mm</PageHeight>" +
 51           "  <MarginTop>5mm</MarginTop>" +
 52           "  <MarginLeft>10mm</MarginLeft>" +
 53           "  <MarginRight>10mm</MarginRight>" +
 54           "  <MarginBottom>5mm</MarginBottom>" +
 55           "</DeviceInfo>";//這里是設置打印的格式 邊距什么的

56 Warning[] warnings; 57 m_streams = new List<Stream>(); 58 try 59 { 60 report.Render("Image", deviceInfo, CreateStream, out warnings);//一般情況這里會出錯的 使用catch得到錯誤原因 一般都是簡單錯誤 61 } 62 catch (Exception ex) 63 { 64 Exception innerEx = ex.InnerException;//取內異常。因為內異常的信息才有用,才能排除問題。 65 while (innerEx != null) 66 { 67 //MessageBox.Show(innerEx.Message); 68 string errmessage = innerEx.Message; 69 innerEx = innerEx.InnerException; 70 } 71 } 72 foreach (Stream stream in m_streams) 73 { 74 stream.Position = 0; 75 } 76 } 77 78 private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek) 79 { 80 //name 需要進一步處理 81 Stream stream = new FileStream(name + DateTime.Now.Millisecond + "." + fileNameExtension, FileMode.Create);//為文件名加上時間 82 m_streams.Add(stream); 83 return stream; 84 } 85 86 private void Print(string printerName) 87 { 88 //string printerName = this.TextBox1.Text.Trim();// "傳送至 OneNote 2007"; 89 if (m_streams == null || m_streams.Count == 0) 90 return; 91 PrintDocument printDoc = new PrintDocument(); 92 // string aa = printDoc.PrinterSettings.PrinterName; 93 if (printerName.Length > 0) 94 { 95 printDoc.PrinterSettings.PrinterName = printerName; 96 } 97 foreach (PaperSize ps in printDoc.PrinterSettings.PaperSizes) 98 { 99 if (ps.PaperName == "A4") 100 { 101 printDoc.PrinterSettings.DefaultPageSettings.PaperSize = ps; 102 printDoc.DefaultPageSettings.PaperSize = ps; 103 // printDoc.PrinterSettings.IsDefaultPrinter;//知道是否是預設定的打印機 104 } 105 } 106 if (!printDoc.PrinterSettings.IsValid) 107 { 108 string msg = String.Format("Can't find printer " + printerName); 109 System.Diagnostics.Debug.WriteLine(msg); 110 return; 111 } 112 printDoc.PrintPage += new PrintPageEventHandler(PrintPage); 113 printDoc.Print(); 114 } 115 116 117 private void PrintPage(object sender, PrintPageEventArgs ev) 118 { 119 Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]); 120 ev.Graphics.DrawImage(pageImage, 0, 0, 827, 1169);//設置打印尺寸 單位是像素 121 m_currentPageIndex++; 122 ev.HasMorePages = (m_currentPageIndex < m_streams.Count); 123 } 124 }


調用很簡單 直接一句話

            new PrintHelp().Run(AppDomain.CurrentDomain.BaseDirectory + "\\Reports\\Report.rdlc", this.TextBox1.Text.Trim(), stationapplication.dthead, "headsource", stationapplication.dtmessage, "datasource", true);

 

由於原來的工具欄沒了打印按鈕 所以也就沒有存在的必要了  隱藏掉 自己做一個工具欄出來

縮放、上一頁下一頁首頁什么的

 /// <summary>
    /// 刷新
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button7_Click(object sender, EventArgs e)
    {
        this.ReportViewer1.LocalReport.Refresh();

    }

    /// <summary>
    /// 首頁
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button8_Click(object sender, EventArgs e)
    {
        this.ReportViewer1.CurrentPage = 1;

    }
    /// <summary>
    /// 上一頁
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button9_Click(object sender, EventArgs e)
    {
        if (this.ReportViewer1.CurrentPage != 1)
        {
            this.ReportViewer1.CurrentPage--;

        }
    }
    /// <summary>
    /// 下一頁
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button10_Click(object sender, EventArgs e)
    {
        if (this.ReportViewer1.CurrentPage != this.ReportViewer1.LocalReport.GetTotalPages())
        {
            this.ReportViewer1.CurrentPage++;

        }
    }

    /// <summary>
    /// 尾頁
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button11_Click(object sender, EventArgs e)
    {
        this.ReportViewer1.CurrentPage = this.ReportViewer1.LocalReport.GetTotalPages();
    }

    /// <summary>
    /// 縮放
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.ReportViewer1.ZoomMode = ZoomMode.Percent;
        this.ReportViewer1.ZoomPercent = Convert.ToInt32(this.DropDownList1.SelectedValue);
    }

 

導出文件

  protected void Button3_Click(object sender, EventArgs e)
    {
        ExportFile(FileType.PDF, "out.pdf");
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        ExportFile(FileType.Image, "out.jpeg");//這里可以使用任意的圖片格式  只要修改out.jpeg為其他格式的名字就可以了
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
        ExportFile(FileType.Excel, "out.xls");
    }

    protected void Button6_Click(object sender, EventArgs e)
    {
        ExportFile(FileType.Word, "out.doc");
    }

    #region 導出文件
    private void ExportFile(FileType ft, string filename)
    {
        CreateFile(ft, filename);

        string strPath = Server.MapPath(filename);
        DownloadFile(strPath, filename);
    }

    private void CreateFile(FileType ft, string filename)
    {
        Warning[] warnings;
        string[] streamids;
        string mimeType;
        string encoding = "utf-8";
        string extension;

        byte[] bytes = this.ReportViewer1.LocalReport.Render(ft.ToString(), null, out mimeType,
                       out encoding, out extension, out streamids, out warnings);

        FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(filename), FileMode.Create);
        fs.Write(bytes, 0, bytes.Length);
        fs.Close();
        fs.Dispose();
    }

    public void DownloadFile(string path, string name)
    {
        try
        {
            System.IO.FileInfo file = new System.IO.FileInfo(path);
            Response.Clear();
            Response.Charset = "utf-8";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            // 添加頭信息,為"文件下載/另存為"對話框指定默認文件名
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
            // 添加頭信息,指定文件大小,讓瀏覽器能夠顯示下載進度
            Response.AddHeader("Content-Length", file.Length.ToString());
            // 指定返回的是一個不能被客戶端讀取的流,必須被下載
            Response.ContentType = "application/octet-stream";
            // 把文件流發送到客戶端
            Response.WriteFile(file.FullName);
            // 停止頁面的執行 
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('系統出現以下錯誤://n" + ex.Message + "!//n請盡快與管理員聯系.')</script>");
        }
    }

發送email  傳真fax  什么的就不說了 很簡單  搜一下一籮筐


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM