不预览直接打印 Microsoft RDLC报表

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

        {

            Run();

        }



        private int m_currentPageIndex;

        private IList 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 =

              "" +

              "  EMF" +

              //"  8.5in" +

              //"  11in" +

              //"  0.25in" +

              //"  0.25in" +

              //"  0.25in" +

              //"  0.25in" +

              "";

            Warning[] warnings;

            m_streams = new List();

            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

例子原码: 下载

你可能感兴趣的:(C#)