前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >RDLC(Reportview)报表直接打印,支持所有浏览器,客户可在linux下浏览使用

RDLC(Reportview)报表直接打印,支持所有浏览器,客户可在linux下浏览使用

作者头像
冰封一夏
发布2019-09-11 15:30:35
2.1K0
发布2019-09-11 15:30:35
举报
文章被收录于专栏:c#Winform自定义控件系列

最近在做一个打印清单的,但是rdlc报表自带的工具栏中的打印按钮只有在ie内核下的浏览器才可以使用(其他的就会 隐藏),这导致了使用火狐和谷歌浏览器还有使用linux系统的客户打印成了问题,于是就自己百度搜,谷歌搜,然后就解决了,下面放上源码

打印类 PrintHelp(我需要两个table座位数据源所以参数中有两个datatable)

代码语言:javascript
复制
  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 }

调用很简单 直接一句话

代码语言:javascript
复制
            new PrintHelp().Run(AppDomain.CurrentDomain.BaseDirectory + "\\Reports\\Report.rdlc", this.TextBox1.Text.Trim(), stationapplication.dthead, "headsource", stationapplication.dtmessage, "datasource", true);

由于原来的工具栏没了打印按钮 所以也就没有存在的必要了 隐藏掉 自己做一个工具栏出来

缩放、上一页下一页首页什么的

代码语言:javascript
复制
 /// <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);
    }

导出文件

代码语言:javascript
复制
  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 什么的就不说了 很简单 搜一下一箩筐

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013-08-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云 BI
腾讯云 BI(Business Intelligence,BI)提供从数据源接入、数据建模到数据可视化分析全流程的BI能力,帮助经营者快速获取决策数据依据。系统采用敏捷自助式设计,使用者仅需通过简单拖拽即可完成原本复杂的报表开发过程,并支持报表的分享、推送等企业协作场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档