当前位置:文档之家› RDLC在不预览的情况下打印本地报表

RDLC在不预览的情况下打印本地报表

演练:在不预览的情况下打印本地报表Visual Studio 2012其他版本Visual Studio 2010Visual Studio 2008Visual Studio 2005此主题尚未评级 - 评价此主题本演练演示如何使用 LocalReport 对象和 CreateStreamCallback 回调函数以编程方式在不查看的情况下打印报表。

先决条件您必须具有访问示例报表和数据源的权限。

有关更多信息,请参见用于打印演练的示例数据和报表。

执行下列步骤可创建控制台应用程序项目。

创建新的控制台应用程序项目1. 在“文件”菜单上,指向“新建”,然后选择“项目”。

2. 在“已安装的模板”窗格中,选择“C#”或“VisualBasic”。

根据 Visual Studio 中的启动设置,“C#”或“Visual Basic”节点可能会显示在“其他语言”下。

3. 在“模板”窗格中,选择“控制台应用程序”。

4. 在“名称”框中,键入项目的名称:PrintLocalReport。

5. 在“位置”框中,输入要保存项目的目录,或者单击“浏览”以导航到该目录。

6. 单击“确定”。

项目与显示“Program”代码文件的“代码”窗口一起打开。

添加引用1. 从“项目”菜单中,选择“添加引用”。

2. 在“添加引用”对话框的“.NET”选项卡上,选择System.Drawing、System.Windows.Forms 和Microsoft.ReportViewer.Winforms。

3. 单击“确定”。

添加现有的 report.rdlc 和 data.xml 文件1. 从“项目”菜单中选择“添加现有项”。

随即将显示“添加现有项”对话框。

2. 定位到保存 report.rdlc 和 data.xml 的文件夹。

然后选择这两个文件。

3. 单击“添加”。

这两个文件将作为项目的一部分显示在解决方案资源管理器中。

添加代码1. Program 代码文件应该已经打开并处于待编辑状态。

如果该文件尚未打开,请在“解决方案资源管理器”窗口中双击Program.cs 或 Module1.vb 文件。

2. 根据您的编程语言选择下面的代码,并用其替换“Program”文件中的现有代码。

注意如果您的计算机上没有安装名为 Microsoft XPSDocument Writer 的打印机,请将粗体代码更改为您计算机上的指定打印机。

3. C#4. 复制5. using System;6. using System.IO;7. using System.Data;8. using System.Text;9. using System.Drawing.Imaging;10. using System.Drawing.Printing;11. using System.Collections.Generic;12. using System.Windows.Forms;13. using Microsoft.Reporting.WinForms;14.15. public class Demo : IDisposable16. {17. private int m_currentPageIndex;18. private IList<Stream> m_streams;19.20. private DataTable LoadSalesData()21. {22. // Create a new DataSet and read sales datafile23. // data.xml into the first DataTable.24. DataSet dataSet = new DataSet();25. dataSet.ReadXml(@"..\..\data.xml");26. return dataSet.Tables[0];27. }28. // Routine to provide to the report renderer, inorder to29. // save an image for each page of the report.30. private Stream CreateStream(string name,31. string fileNameExtension, Encoding encoding,32. string mimeType, bool willSeek)33. {34. Stream stream = new MemoryStream();35. m_streams.Add(stream);36. return stream;37. }38. // Export the given report as an EMF (EnhancedMetafile) file.39. private void Export(LocalReport report)40. {41. string deviceInfo =42. @"<DeviceInfo>43. <OutputFormat>EMF</OutputFormat>44. <PageWidth>8.5in</PageWidth>45. <PageHeight>11in</PageHeight>46. <MarginTop>0.25in</MarginTop>47. <MarginLeft>0.25in</MarginLeft>48. <MarginRight>0.25in</MarginRight>49. <MarginBottom>0.25in</MarginBottom>50. </DeviceInfo>";51. Warning[] warnings;52. m_streams = new List<Stream>();53. report.Render("Image", deviceInfo,CreateStream,54. out warnings);55. foreach (Stream stream in m_streams)56. stream.Position = 0;57. }58. // Handler for PrintPageEvents59. private void PrintPage(object sender,PrintPageEventArgs ev)60. {61. Metafile pageImage = new62. Metafile(m_streams[m_currentPageIndex]);63.64. // Adjust rectangular area with printermargins.65. Rectangle adjustedRect = new Rectangle(66. ev.PageBounds.Left -(int)ev.PageSettings.HardMarginX,67. ev.PageBounds.Top -(int)ev.PageSettings.HardMarginY,68. ev.PageBounds.Width,69. ev.PageBounds.Height);70.71. // Draw a white background for the report72. ev.Graphics.FillRectangle(Brushes.White,adjustedRect);73.74. // Draw the report content75. ev.Graphics.DrawImage(pageImage,adjustedRect);76.77. // Prepare for the next page. Make sure wehaven't hit the end.78. m_currentPageIndex++;79. ev.HasMorePages = (m_currentPageIndex <m_streams.Count);80. }81.82. private void Print()83. {84. if (m_streams == null || m_streams.Count ==0)85. throw new Exception("Error: no stream toprint.");86. PrintDocument printDoc = new PrintDocument();87. if (!printDoc.PrinterSettings.IsValid)88. {89. throw new Exception("Error: cannot findthe default printer.");90. }91. else92. {93. printDoc.PrintPage += newPrintPageEventHandler(PrintPage);94. m_currentPageIndex = 0;95. printDoc.Print();96. }97. }98. // Create a local report for Report.rdlc, loadthe data,99. // export the report to an .emf file, andprint it.100. private void Run()101. {102. LocalReport report = new LocalReport();103. report.ReportPath = @"..\..\Report.rdlc"; 104. report.DataSources.Add(105. new ReportDataSource("Sales",LoadSalesData()));106. Export(report);107. Print();108. }109.110. public void Dispose()111. {112. if (m_streams != null)113. {114. foreach (Stream stream in m_streams) 115. stream.Close();116. m_streams = null;117. }118. }119.120. public static void Main(string[] args)121. {122. using (Demo demo = new Demo())123. {124. demo.Run();125. }126. }127. }。

相关主题