博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用 LocalReport 对象进行打印
阅读量:6848 次
发布时间:2019-06-26

本文共 4156 字,大约阅读时间需要 13 分钟。

 
  1. 从“项目”菜单中,选择“添加引用”。将显示“添加引用”对话框。

  2. 从“.NET”选项卡上显示的列表框中,选择 Winforms 和 Drawing 组件。

  1. 应打开 Program.cs 文件以供编辑。如果未打开,在“解决方案资源管理器”窗口中双击 Program.cs 文件。

  2. 使用以下代码替换 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
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) {
    

//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 =          "
" + "
EMF
" + "
8.5in
" + "
11in
" + "
0.25in
" + "
0.25in
" + "
0.25in
" + "
0.25in
" + "
"; Warning[] warnings; m_streams = new List
(); 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();        }    }}

 

参考:

转载地址:http://croul.baihongyu.com/

你可能感兴趣的文章
android 1.6中LinearLayout getBaseline函数的一个bug
查看>>
shell3
查看>>
分享几个好用的工具,有效提升工作效率
查看>>
论北京北漂的家人们
查看>>
delphi 检查用户输入必须是汉字串
查看>>
思科交换机和路由器设备实现DHCP功能
查看>>
MongoDB安装与操作大全
查看>>
人我的是好有是的好sula
查看>>
编译工程时报java:[1,0] illegal character: \65279问题排查与解决过
查看>>
RHEL6子接口及双网卡绑定配置
查看>>
常见系统故障排查
查看>>
正则验证手机号是否合法
查看>>
《Git权威指南》读书笔记 第四章 git初始化
查看>>
《Head first HTML与CSS 第二版》读书笔记 第九章 盒模型
查看>>
《Python面向对象……》之目录
查看>>
集群入门简析及LB下LVS详解
查看>>
Linux与GPT
查看>>
管理或技术
查看>>
分配到弱属性;对象将在赋值之后释放
查看>>
java作用域public ,private ,protected 及不写时的区别
查看>>