不预览直接打印 Microsoft RDLC报表
2008-07-03 09:03
459 查看
我用.net写程序,做报表时一直用水晶报表来做,最近发现用Microsoft的RDLC做报表也不错,而且方便,最主要布署(WEB)的时修没有水晶报表那么麻烦。 但是唯一的缺点是学习资料太少了,都得自己瞎搞。唯一好的资源就只有蜡人张同志的《RDLC报表》系列,当然还有MSDN 。 下面是不预览直接打印的实现,主要代码来自MSDN。
LocalReport.Render 方法的第一个参数format
呈现报表所用的格式。 此参数将映射到某个呈现扩展插件。 支持的格式包括 Microsoft Office Excel、PDF 和 Image。详情参见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
例子原码:下载
相关文章推荐
- C# WinForm RDLC报表不预览直接连续打印
- [转]在不预览的情况下打印RDLC报表
- 关闭rdlc报表打印预览后,关闭客户端,抛出异常“发生了应用程序级的异常 将退出”
- 【.Net码农】RDLC(Reportview)报表直接打印,支持所有浏览器,客户可在linux下浏览使用
- 在vs2005中的ReportViewer(RDLC报表)中使用直接打印功能,在vs2005使用ReportViewer2008
- 在vs2005中的ReportViewer(RDLC报表)中使用直接打印功能,在vs2005使用ReportViewer2008
- reportViewer(RDLC)不预览,直接打印
- 使用RSClientPrint直接打印本地RDLC报表
- 在不预览的情况下打印RDLC报表
- 在vs2005中的ReportViewer(RDLC报表)中使用直接打印功能,在vs2005使用ReportViewer2008
- 在vs2005中的ReportViewer(RDLC报表)中使用直接打印功能,在vs2005使用ReportViewer2008[转]
- RDLC(Reportview)报表直接打印,支持所有浏览器,客户可在linux下浏览使用
- RDLC报表的直接打印
- rdlc报表预览打印
- 在vs2005中的ReportViewer(RDLC报表)中使用直接打印功能,在vs2005使用ReportViewer2008
- 在vs2005中的ReportViewer(RDLC报表)中使用直接打印功能,在vs2005使用ReportViewer2008
- 完整的自定义纸张打印RDLC报表例子
- C#中直接打印Report文件(rdlc)
- 实现Web Client上的html报表直接打印
- rdlc报表 打印图片功能如何实现