不预览直接打印 Microsoft RDLC报表

     我用.net写程序,做报表时一直用水晶报表来做,最近发现用Microsoft的RDLC做报表也不错,而且方便,最主要布署(WEB)的时修没有水晶报表那么麻烦。 但是唯一的缺点是学习资料太少了,都得自己瞎搞。唯一好的资源就只有蜡人张同志的《 RDLC报表》系列,当然还有 MSDN 。 下面是不预览直接打印的实现,主要代码来自MSDN。
        private void btnPrint_Click(object sender, EventArgs e)

        {

            Run();

        }



        private int m_currentPageIndex;

        private IList<Stream> m_streams;



        private DataTable LoadSalesData()

        {

            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["testrdlc.Properties.Settings.NorthwindConnectionString"].ConnectionString);

            SqlCommand cmd = new SqlCommand("SELECT * FROM Employees", con);

            SqlDataAdapter adp = new SqlDataAdapter();

            adp.SelectCommand = cmd;

            con.Open();

            DataTable dt = new DataTable();

            adp.Fill(dt);

            con.Close();

            return dt;

            





        }



        private Stream CreateStream(string name, string fileNameExtension,

         Encoding encoding, string mimeType, bool willSeek)

        {

            Stream stream = new FileStream(name + "." + fileNameExtension,

              FileMode.Create);

            m_streams.Add(stream);

            return stream;

        }



        private void Export(LocalReport report)

        {

            string deviceInfo =

              "<DeviceInfo>" +

              "  <OutputFormat>EMF</OutputFormat>" +

              //"  <PageWidth>8.5in</PageWidth>" +

              //"  <PageHeight>11in</PageHeight>" +

              //"  <MarginTop>0.25in</MarginTop>" +

              //"  <MarginLeft>0.25in</MarginLeft>" +

              //"  <MarginRight>0.25in</MarginRight>" +

              //"  <MarginBottom>0.25in</MarginBottom>" +

              "</DeviceInfo>";

            Warning[] warnings;

            m_streams = new List<Stream>();

            try

            {

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

            }

            catch (Exception ex)

            {

                Exception innerEx = ex.InnerException;//取内异常。因为内异常的信息才有用,才能排除问题。

                while (innerEx != null)

                {

                    MessageBox.Show(innerEx.Message);

                    innerEx = innerEx.InnerException;

                }

            }





            foreach (Stream stream in m_streams)

                stream.Position = 0;

        }



        private void PrintPage(object sender, PrintPageEventArgs ev)

        {

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

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



            m_currentPageIndex++;

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

        }



        private void Print()

        {

            const string printerName = "Microsoft Office Document Image Writer";



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

                return;



            PrintDocument printDoc = new PrintDocument();

            printDoc.PrinterSettings.PrinterName = printerName;

            if (!printDoc.PrinterSettings.IsValid)

            {

                string msg = String.Format("Can't find printer /"{0}/".", printerName);

                Debug.WriteLine(msg);

                return;

            }

            printDoc.PrintPage += new PrintPageEventHandler(PrintPage);

            printDoc.Print();

        }



        private void Run()

        {

            LocalReport report = new LocalReport();

            report.ReportPath = Application.StartupPath +"//Report1.rdlc";//加上报表的路径

            report.DataSources.Add(new ReportDataSource("NorthwindDataSet_Employees", LoadSalesData()));



            Export(report);



            m_currentPageIndex = 0;

            Print();

        }


要说明的是:
一、report.ReportPath 属性指定的位置一定要有报表文件。
二、如果report.Render出现异常,必须捕获内异常信息,因为最外层异常信息的用处不大,根本无法排除问题。
三、
LocalReport.Render 方法的第一个参数 format

呈现报表所用的格式。 此参数将映射到某个呈现扩展插件。 支持的格式包括 Microsoft Office Excel、PDF 和 Image。详情参见MSDN

例子原码: 下载
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值