`

[转]在不预览的情况下打印RDLC报表

    博客分类:
  • C#
阅读更多

水晶报表是相当不错的报表,不管是在报表浏览器中还是直接打印方式下,编程都相当方便,但有一点在web下好象权限管理有些麻烦,影响了正常打印,所以换成 Microsoft Sql Server Report,但有一个问题又出现了,他没有直接打印功能。头痛。后在msdn中找到了解决方案.

using System;
using System.IO;
using System.Data;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
publicclass Demo : IDisposable
{   
    privateint m_currentPageIndex;   
    private IList<Stream> m_streams;   
    private DataTable LoadSalesData()   
    {       
        // Create a new DataSet and read sales data file        
        //    data.xml into the first DataTable.       
        DataSet dataSet =new DataSet();       
        dataSet.ReadXml(@"..\..\data.xml");       
        return dataSet.Tables[0];   
     }   

    // Routine to provide to the report renderer, in order to   
    //    save an image for each page of the report.   
    private Stream CreateStream(string name,     string fileNameExtension, Encoding encoding,     string mimeType, bool willSeek)   
     {       
        Stream stream =new MemoryStream();       
        m_streams.Add(stream);       
        return stream;   
    }   

     // Export the given report as an EMF (Enhanced Metafile) file.   
    privatevoid 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>();       
        report.Render("Image", deviceInfo, CreateStream,          out warnings);       
        foreach (Stream streamin m_streams)           
             stream.Position = 0;   
    }   

    // Handler for PrintPageEvents   
     privatevoid PrintPage(object sender, PrintPageEventArgs ev)   
     {       
        Metafile pageImage =new           Metafile(m_streams[m_currentPageIndex]);       

        // Adjust rectangular area with printer margins.       
        Rectangle adjustedRect =new Rectangle(           
                        ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,           
                        ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,            ev.PageBounds.Width,           
                        ev.PageBounds.Height);       // Draw a white background for the report 
                        ev.Graphics.FillRectangle(Brushes.White, adjustedRect);       // Draw the report content       
                        ev.Graphics.DrawImage(pageImage, adjustedRect);       // Prepare for the next page. Make sure we haven't hit the end.       
                        m_currentPageIndex++;
                        ev.HasMorePages = (m_currentPageIndex < m_streams.Count);   

    }   

    privatevoid Print()   

     {       
         if (m_streams ==null || m_streams.Count == 0)           
                 thrownew Exception("Error: no stream to print.");       
        PrintDocument printDoc =new PrintDocument();       
         if (!printDoc.PrinterSettings.IsValid)       
         {           
            throw new Exception("Error: cannot find the default printer.");       
         }       
         else       
         {           
            printDoc.PrintPage +=new PrintPageEventHandler(PrintPage);           
            m_currentPageIndex = 0;           
            printDoc.Print();       
         }   
     }   

    // Create a local report for Report.rdlc, load the data,   

     //    export the report to an .emf file, and print it.   

     privatevoid Run()   

    {       

        LocalReport report =new LocalReport();       

        report.ReportPath =@"..\..\Report.rdlc";       

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

        Export(report);       

        Print();   

    }   

    public void Dispose()   

     {       

        if (m_streams != null)       

        {           

            foreach (Stream stream in m_streams)               

            stream.Close();           

            m_streams =null;       

        }   

     }   

    publicstatic void Main(string[] args)   

     {       

        using (Demo demo = new Demo())       

         {           

             demo.Run();       

             }   

        }

    }

}
 

你可以修改以上代码来完成你的需求(有个问题。Microsoft为什么不封装这些,成为一个功能可选项呢)

【代码】来源于MSDN http://msdn.microsoft.com/zh-cn/library/ms252091.aspx

转载:http://blog.csdn.net/shcy0524/article/details/6846847

分享到:
评论

相关推荐

    c# 不预览 直接打印rdlc 不用reportviewer

    直接打印rdlc ,不用reportviewer 预览

    直接打印RDLC报表

    直接调用,传递参数即可 可在asp.net下或winform下使用,无需预览直接进行打印,可根据自己的需要进行修改 有适当的备注

    rdlc报表打印控件

    总之,RDLC报表打印控件为.NET开发者提供了一种方便的工具,使得在应用程序中处理和打印RDLC报表变得更加简单和灵活。它允许用户自定义打印设置,提升用户体验,同时简化了开发过程。通过深入理解和熟练运用这个控件...

    MVC下使用RDLC报表的示例

    本示例将探讨如何在ASP.NET MVC应用程序中集成和使用RDLC报表。 首先,我们需要了解MVC的基本组成部分。模型(Model)负责处理业务逻辑和数据管理,视图(View)负责显示用户界面,控制器(Controller)则处理用户...

    RDLC报表直接打印

    在本案例中,我们关注的是如何在不通过预览界面的情况下,直接将RDLC报表发送到打印机,实现直接打印。 ### RDLC报表直接打印的关键步骤 #### 1. 数据准备 首先,你需要准备报表所需的数据源。代码示例中通过SQL...

    C# RDLC 报表打印实例--带数据库

    在创建RDLC报表时,我们需要连接到这个数据库,通过查询获取数据,然后在报表中展示这些数据。 报表的创建过程通常包括以下步骤: 1. **设计报表布局**:在Visual Studio中,开发者可以使用RDLC设计器来拖放表格、...

    C# RDLC报表打印实例

    C# RDLC报表打印实例 项目描述 C# RDLC 报表打印实例 本实例上传带的数据库是SQL SERVER 2005生成的, 在本实例中,包括了基本的报表分组、统计、表达式获得系统时间、获得本机IP。 RDLC 1、简单易用的控件,特别...

    C#+RDLC报表打印实例源代码

    总结起来,C#结合RDLC报表打印实例涉及到的主要知识点包括RDLC报表的设计与数据绑定、报表在C#代码中的呈现、报表的渲染到特定格式以及最终的打印操作。通过理解和实践这些步骤,开发者能够为应用程序添加强大的报表...

    VS .NET报表开发之RDLC报表随笔

    **RDLC报表**是在Microsoft Visual Studio .NET环境下用于创建报表的一种技术。它利用**Report Definition Language (RDL)**来定义报表的结构和内容,并通过**Report Viewer Control**在应用程序中展示这些报表。 #...

    不需预览实现rdlc文件打印

    本文将深入探讨如何在不进行预览的情况下,直接实现RDLC文件的打印。 首先,我们需要理解RDLC报表的工作原理。RDLC文件本质上是一个XML文件,它定义了报表的布局、数据源和计算逻辑。在C#的Windows Forms应用中,...

    C# RDLC 报表打印实例

    RDLC报表在WinForm中的使用,可以提供用户交互式查看、打印和导出报表的功能,无需服务器支持。 描述“C# winform使用RDLC 打印报表实例源码”表明这个压缩包包含了一个完整的C# WinForm项目,该项目演示了如何集成...

    RDLC 报表自适应

    RDLC报表自适应主要是指报表在不同设备或屏幕尺寸下能够自动调整其布局,以适应显示环境。这在移动设备和多屏幕分辨率的现代应用场景中尤为重要。RDLC报表自适应主要涉及到以下几个关键概念和方法: 1. **布局设计*...

    RDLC报表总结

    3. 高度可编程性,在你的项目中,甚至不需要有一个报表文件,通过代码就可以实现报表生成、预览和打印等一系列操作; 4. 支持DrillThrough数据钻取功能; 5. 导出的Excel文件格式非常完美,任何其它报表在这方面都不...

    Rdlc打印一维码二维码打印

    在本文中,我们将深入探讨如何在C# WinForm应用程序中使用RDLC报表来实现一维码和二维码的打印功能。RDLC(Report Definition Language Client)是Microsoft开发的一种用于设计和生成报表的工具,广泛应用于.NET ...

    RDLC报表设计器

    6. **导出与打印**:RDLC报表支持多种格式的导出,如PDF、Excel、Word等,同时也能提供打印预览和打印功能,满足不同场合的输出需求。 7. **交互性**:在C#应用中,RDLC报表可以集成到用户界面,用户可以实时刷新...

Global site tag (gtag.js) - Google Analytics