.net winform rdlc报表打印

本文介绍了在VS2015中安装RDLC报表模块的方法,通过控制面板修改勾选相关工具进行安装。还给出了使用RDLC报表的代码示例,包括指定rdlc文件、添加数据源、设置参数、刷新报表等操作,以及实现报表自打印的详细代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    vs2015安装rdlc报表模块
  控制面板-Microsoft Visual Studio …2005-更改-修改-勾选{Microsoft SQL Server Data Tools},安装即可

private void FrmWholeCarReceipts_Load(object sender, EventArgs e)
        {
            try
            {
       //  指定rdlc文件
this.reportViewer1.LocalReport.ReportEmbeddedResource = "AloneSell.Report.OrderRefund.rdlc";
            }
            catch (Exception ex)
            {
                Utility.Pub.ShowError("报表加载失败," + ex.Message);
                return;
            }
            this.reportViewer1.LocalReport.DataSources.Clear();

            BLL.T_X_BackCarM_BLL bllBack = new BLL.T_X_BackCarM_BLL();
            DataTable dt = bllBack.GetList("BackID='" + BackID + "'").Tables[0];
            if (dt.Rows.Count <= 0) return;
            try
            {
                Model.T_X_BackCarM_Model m = bllBack.DataTableToList(dt)[0];
                BLL.T_X_BeSpeakeCar_BLL bllBS = new BLL.T_X_BeSpeakeCar_BLL();
                Model.T_X_BeSpeakeCar_Model mBS = bllBS.GetModel(BackID);
                if (mBS == null)
                {
                    Utility.Pub.ShowInfo("订单不存在");
                    return;
                }
                string Value1 = mBS.GuestName + "           支付 整车收款 ";
                string Value2 = new Money(m.BackMoney, false).ToString() + "(" + m.BackMoney.ToString("f1") + ")";

              //添加报表数据源
  this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("T_X_BackCarM", dt));

               //参数设置
List<ReportParameter> parameterList = new List<ReportParameter>();
                parameterList.Add(new ReportParameter("Company", Utility.Pub.Company));
                parameterList.Add(new ReportParameter("Value1", Value1));
                parameterList.Add(new ReportParameter("Value2", Value2));
                this.reportViewer1.LocalReport.SetParameters(parameterList);

                this.reportViewer1.RefreshReport();
                //设置打印布局模式,显示物理页面大小
                this.reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
                //缩放模式为百分比,以100%方式显示
                this.reportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.Percent;
                this.reportViewer1.ZoomPercent = 100;

                string _AutoPrint = ConfigFileManage.GetAppKeyValue("ShowPrint");
                if (_AutoPrint.Trim() == "否")
                {
                    this.Visible = false;
                    PPrint();
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                Utility.Pub.ShowError("操作失败," + ex.Message);
                return;
            }
        }

        #region 报表自打印
        private string _reportname = Application.StartupPath + "\\rptOperation.rdlc";//rptBase.rdlc
        private DataSet _datasource = null;
        private string _autoprint = "";
        private IList<Stream> m_streams;
        private int m_currentPageIndex;
        public void PPrint()
        {

            try
            {

                LocalReport report = this.reportViewer1.LocalReport;
                Export(report);

                m_currentPageIndex = 0;

                NBPrint();

                if (m_streams != null)
                {

                    foreach (Stream stream in m_streams)

                        stream.Close();

                    m_streams = null;

                }

            }

            catch (Exception ex)
            {
                MessageBox.Show("在打印过程中出现异常! " + ex.Message.ToString());
            }

        }

        private void Export(LocalReport report)
        {

            //7.5in 3.66in 0 0 0 0 当前设置为A4纵向 

            string deviceInfo =

              "<DeviceInfo>" +

              "  <OutputFormat>EMF</OutputFormat>" +

              "  <PageWidth>20.7cm</PageWidth>" +

              "  <PageHeight>28cm</PageHeight>" +

              "  <MarginTop>0in</MarginTop>" +

              "  <MarginLeft>0in</MarginLeft>" +

              "  <MarginRight>0in</MarginRight>" +

              "  <MarginBottom>0in</MarginBottom>" +

              "</DeviceInfo>";

            Warning[] warnings;

            m_streams = new List<Stream>();

            report.Render("Image", deviceInfo, CreateStream, out warnings);

            foreach (Stream stream in m_streams)
            {

                stream.Position = 0;

            }

        }

        private void NBPrint()
        {

            if (m_streams == null || m_streams.Count == 0)

                return;
            System.Drawing.Printing.PrintDocument printDoc = new System.Drawing.Printing.PrintDocument();

            if (!printDoc.PrinterSettings.IsValid)
            {

                string msg = String.Format("Can't find printer \"{0}\".", "默认打印机!");

                MessageBox.Show(msg, "找不到默认打印机");

                return;

            }

            printDoc.PrintPage += new PrintPageEventHandler(PrintPage);

            printDoc.Print();

            this.Close();

        }

        private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
        {

            string filenameext = DateTime.Now.Year.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();

            Stream stream = new FileStream(name + "." + fileNameExtension, FileMode.Create);

            m_streams.Add(stream);

            return stream;

        }

        private void PrintPage(object sender, PrintPageEventArgs ev)
        {

            Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);

            ev.Graphics.DrawImage(pageImage, ev.PageBounds);

            m_currentPageIndex++;

            ev.HasMorePages = (m_currentPageIndex < m_streams.Count);

        }

        #endregion

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值