-
从“项目”菜单中,选择“添加引用”。将显示“添加引用”对话框。
-
从“.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 IListm_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) {
//string strPath = @"D:\Report\";
//if (System.IO.Directory.Exists(strPath) == false) //{ // System.IO.Directory.CreateDirectory(strPath); //} //string filenameext = DateTime.Now.Year.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString(); //string strFile = strPath + filenameext + name + "." + fileNameExtension; //Stream stream = new FileStream(strFile, FileMode.Create); //m_streams.Add(stream); //return stream;Stream stream = new FileStream(@"c:\My Reports\" + name + "." + fileNameExtension, FileMode.Create); m_streams.Add(stream); return stream; } private void Export(LocalReport report) { string deviceInfo = "" + " "; Warning[] warnings; m_streams = new ListEMF " + "8.5in " + "11in " + "0.25in " + "0.25in " + "0.25in " + "0.25in " + "(); 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() {
//if (m_streams == null || m_streams.Count == 0)
// return; //PrintDocument printDoc = new PrintDocument();//if (!printDoc.PrinterSettings.IsValid)
//{ // string msg = String.Format("Can't find printer \"{0}\".", "默认打印机!"); // MessageBox.Show(msg, "找不到默认打印机"); // return; //} //printDoc.PrintPage += new PrintPageEventHandler(PrintPage); //printDoc.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(); } }}
参考: