此演练演示如何使用 LocalReport 对象和 CreateStreamCallback 回调函数以编程方式进行打印。
您必须具有访问示例报表和数据源的权限。有关详细信息,请参阅用于打印演练的示例数据和报表。
执行下列步骤可创建 Microsoft Visual Studio 控制台应用程序项目。在此示例中,将使用 Microsoft Visual C# 创建应用程序。
-
在“文件”菜单上,指向“新建”,然后选择“项目”。
-
在“项目类型”窗格中,选择“Visual C#”和“Windows”。
-
在“模板”窗格中,选择“控制台应用程序”以创建 Microsoft 控制台应用程序。
-
在“名称”框中,键入项目名称:LocalReport Printing。
-
在“位置”框中,输入要保存项目的目录,或者单击“浏览”以导航到该目录。将打开项目,并在“代码”窗口中显示 Program.cs。
-
从“项目”菜单中,选择“添加引用”。将显示“添加引用”对话框。
-
从“.NET”选项卡上显示的列表框中,选择 >Winforms 和 >Drawing 组件。
-
应打开 Program.cs 文件以供编辑。如果未打开,在“解决方案资源管理器”窗口中双击 Program.cs 文件。
-
使用以下代码替换 Program.cs 文件中的现有代码。确保使用本地计算机上示例报表的有效路径来替换报表引用。不要向项目添加 Data.xml 和 Report.rdlc。下面以编程方式访问这些文件。
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;
public class Demo : IDisposable
{
private int m_currentPageIndex;
private IList<Stream> m_streams;
private DataTable LoadSalesData()
{
DataSet dataSet = new DataSet();
dataSet.ReadXml(@"c:\My Reports\data.xml");
return dataSet.Tables[0];
}
private Stream CreateStream(string name,
string fileNameExtension, Encoding encoding,
string mimeType, bool willSeek)
{
Stream stream = new FileStream(@"c:\My Reports\" + 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>();
report.Render("Image", deviceInfo, CreateStream, out warnings);
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, ev.PageBounds);
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);
MessageBox.Show(msg, "Print Error");
return;
}
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();
}
private void Run()
{
LocalReport report = new LocalReport();
report.ReportPath = @"c:\My Reports\Report.rdlc";
report.DataSources.Add(new ReportDataSource("Sales", LoadSalesData()));
Export(report);
m_currentPageIndex = 0;
Print();
}
public void Dispose()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}
public static void Main(string[] args)
{
using (Demo demo = new Demo())
{
demo.Run();
}
}
}
-
在“生成”菜单上,单击“生成解决方案”以生成应用程序。生成应用程序的过程中,将编译报表并找到所有错误(如报表中使用的表达式中的语法错误),将其添加到“任务列表”。
-
按 F5 运行此应用程序。
参考信息
>LocalReport
Microsoft.Reporting.WinForms.CreateStreamCallback
Microsoft.Reporting.WebForms.CreateStreamCallback
其他资源
示例和演练