RDLC直接打印帮助类
代码
/// <summary>
/// 打印帮助类
/// </summary>
public class PrintHelper
{ private int m_currentPageIndex;
private IList<Stream> m_streams; /// <summary>
/// 报表直接打印
/// </summary>
/// <param name="reportPath">报表文件路径</param>
/// <param name="printerName">打印机名称</param>
/// <param name="dt">DataTable</param>
/// <param name="sourceName">rdlc的数据集名称</param>
/// <param name="paraList">参数列表</param>
public void Run(string reportPath, string printerName, DataTable dt, string sourceName, List<ReportParameter> paraList)
{
LocalReport report = new LocalReport();
report.ReportPath = reportPath;
report.DataSources.Add(new ReportDataSource(sourceName,dt));
report.EnableExternalImages = true;
report.SetParameters(paraList);
Export(report);
m_currentPageIndex = ;
Print(printerName);
} private void Export(LocalReport report)
{
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>EMF</OutputFormat>"+
"</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)
{
string errmessage = innerEx.Message;
innerEx = innerEx.InnerException; }
} foreach (Stream stream in m_streams)
{
stream.Position = ;
}
} private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
{
Stream stream = new FileStream(name + DateTime.Now.Millisecond + "." + fileNameExtension, FileMode.Create);
m_streams.Add(stream);
return stream;
}
private void Print(string printerName)
{
if (m_streams == null || m_streams.Count == ) return;
PrintDocument printDoc = new PrintDocument();
if (printerName.Length > )
{
printDoc.PrinterSettings.PrinterName = printerName;
}
foreach (PaperSize ps in printDoc.PrinterSettings.PaperSizes)
{
if (ps.PaperName == "A4")
{
printDoc.PrinterSettings.DefaultPageSettings.PaperSize = ps;
printDoc.DefaultPageSettings.PaperSize = ps;
}
}
if (!printDoc.PrinterSettings.IsValid)
{
string msg = string.Format("找不到打印机:{0}",printerName);
LogUtil.Log(msg);
return;
}
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();
} private void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
ev.Graphics.DrawImage(pageImage, , , , );//像素
m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
}
说明
打印格式
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>EMF</OutputFormat>" +
" <PageWidth>210mm</PageWidth>" +
" <PageHeight>297mm</PageHeight>" +
" <MarginTop>5mm</MarginTop>" +
" <MarginLeft>10mm</MarginLeft>" +
" <MarginRight>10mm</MarginRight>" +
" <MarginBottom>5mm</MarginBottom>" +
"</DeviceInfo>";//这里是设置打印的格式 边距什么的
关于OutputFormat:
http://support.supermap.com.cn/DataWarehouse/WebDocHelp/6.1.3/iserverOnlineHelp/mergedProjects/iServerJavadoc/com/supermap/services/components/commontypes/OutputFormat.html
参考文章
http://www.cnblogs.com/bfyx/p/3279385.html (详细)
http://blog.csdn.net/moshuchao/article/details/2607017
http://www.cnblogs.com/qiuweiguo/archive/2011/08/26/2154706.html
收集另一文
http://www.cnblogs.com/hlxs/archive/2010/11/18/2087988.html
//初始化报表信息
private void SetReportInfo(string reportPath,string sourceName,DataTable dataSource,bool isFengPi)
{
if (!File.Exists(reportPath))
{
MessageBox.Show("报表文件:" + reportPath + " 不存在!","提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
} if (dataSource == null || dataSource.Rows.Count == )
{
MessageBox.Show("没有找到案卷号为:"+txtArchiveNum.Text.Trim()+"的相关目录信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
pos = ;
LocalReport report1 = new LocalReport();
//设置需要打印的报表的文件名称。
report1.ReportPath = reportPath;
if (isFengPi)
{
//设置参数
string archveTypeName = GetArchiveTypeName();
ReportParameter archiveType = new ReportParameter("ArchiveType", archveTypeName);
report1.SetParameters(archiveType);
}
//创建要打印的数据源
ReportDataSource source = new ReportDataSource(sourceName, dataSource);
report1.DataSources.Add(source);
//刷新报表中的需要呈现的数据
report1.Refresh();
pos = ;
m_streams = new List<Stream>();
string deviceInfo ="<DeviceInfo>" +
" <OutputFormat>EMF</OutputFormat>" +
" <PageWidth>21cm</PageWidth>" +
" <PageHeight>29.7cm</PageHeight>" +
" <MarginTop>2.0066cm</MarginTop>" +
" <MarginLeft>2.0066cm</MarginLeft>" +
" <MarginRight>2.0066cm</MarginRight>" +
" <MarginBottom>2.0066cm</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
//将报表的内容按照deviceInfo指定的格式输出到CreateStream函数提供的Stream中。
report1.Render("Image", deviceInfo, CreateStream, out warnings);
} //声明一个Stream对象的列表用来保存报表的输出数据
//LocalReport对象的Render方法会将报表按页输出为多个Stream对象。
private List<Stream> m_streams;
//用来提供Stream对象的函数,用于LocalReport对象的Render方法的第三个参数。
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek) {
pos = ;
//如果需要将报表输出的数据保存为文件,请使用FileStream对象。
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
} //用来记录当前打印到第几页了
private int m_currentPageIndex; #region 打印报表
private void Print()
{
pos = ;
m_currentPageIndex = ;
if (m_streams == null || m_streams.Count == )
return;
//声明PrintDocument对象用于数据的打印
PrintDocument printDoc = new PrintDocument();
//指定需要使用的打印机的名称,使用空字符串""来指定默认打印机
// printDoc.PrinterSettings.PrinterName = "";
//判断指定的打印机是否可用
if (!printDoc.PrinterSettings.IsValid)
{
MessageBox.Show("没有找到打印机!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
pos = ;
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
//执行打印操作,Print方法将触发PrintPage事件。
printDoc.Print(); //释放资源
foreach (Stream stream in m_streams)
{
stream.Dispose();
stream.Close();
}
m_streams = null;
} private void PrintPage(object sender, PrintPageEventArgs ev)
{
pos =;
//Metafile对象用来保存EMF或WMF格式的图形,
//我们在前面将报表的内容输出为EMF图形格式的数据流。
m_streams[m_currentPageIndex].Position = ;
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
//指定是否横向打印
ev.PageSettings.Landscape = false;
//这里的Graphics对象实际指向了打印机
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
m_streams[m_currentPageIndex].Close();
m_currentPageIndex++;
//设置是否需要继续打印
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
#endregion //打印封皮
private void btPrint_Click(object sender, EventArgs e)
{
string reportPath = Application.StartupPath + "\\Files\\ReportEnvelop.rdlc";
SetReportInfo(reportPath, "DataSet1", GetDataSource(true), true);
Print(); }
RDLC直接打印帮助类的更多相关文章
- 分享一个动态生成RDLC报表的类
在实际工作中,当需要进行大批量查询和生成报表的时候,可以使用我写的类. 特点: 无需报表设计器.无需为报表设置数据集 只需要传入查询结果就可以全自动生成报表,传入的对象为Dynamic(目前支持Dat ...
- 干货,比较全面的c#.net公共帮助类(Common.Utility)
Common.Utility 初衷 网上有各式各样的帮助类,公共类,但是比较零碎,经常有人再群里或者各种社交账号上问我有没有这个helper,那个helper,于是萌生了收集全部helper的念头,以 ...
- Java(215-231)【Object类、常用API】
1.Object类的toString方法 java.lang.Object 类 Object 是类层次结构的根(父)类. 每个类(Person,Student...)都使用 Object 作为超(父) ...
- Object类的toString和Equals方法,以及Objects类的Equals方法
Object类 toString()方法 public class Person { private String name; private int age; public Person() { } ...
- Java类的继承与多态特性-入门笔记
相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...
- java面向对象---对象容器
泛型类--ArrayList<>; 2.对象数组中的每个元素都是对象的管理者而并非对象本身!!!!! 3.java类的基本数据类型 基本数据类型 包装类 byte Byte short S ...
- 支持单色条码图像生成的条形码控件Barcode Professional
Barcode Professional for .NET Windows Forms条形码控件是一款灵活和强大的.NET组件(.NET DLL 类库),它让您轻松地添加条码生成和打印功能到您的.NE ...
- 拾遗与填坑《深度探索C++对象模型》3.3节
<深度探索C++对象模型>是一本好书,该书作者也是<C++ Primer>的作者,一位绝对的C++大师.诚然该书中也有多多少少的错误一直为人所诟病,但这仍然不妨碍称其为一本好书 ...
- Python基础6 面向对象
本节内容 面向对象编程介绍 为什么要面向对象开发? 面向对象的特性:封装,继承,多态 类,方法 引子 假设现在我们需要开发一款简单的游戏,譬如叫做人兽战争.我们需要简单的2个角色,一个人,一个怪兽,而 ...
随机推荐
- andriod 带看括弧的计算器
界面 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=& ...
- linux网络流量实时监控工具之iptraf
这个工具还是很强大 linux网络流量实时监控工具之iptraf [我的Linux,让Linux更易用]IPTraf是一个网络监控工具,功能比nload更强大,可以监控所有的流量,IP流量,按协议分的 ...
- cocospods的安装与应用
安装cocospods 一,升级Ruby环境 ~ xxx$ sudo gem update --system 二,安装Cocoapods时需要访问cocoapods.org,该网站可能被墙掉,但是 ...
- 主程序底部TabBar功能跟登录页面布局
1:主程序底部TabBar的功能实现 效果图: 主要代码如下: - (UITabBarController*)setRootVC:(BOOL)bShowCart { //创建一个子控制器 用于显示当前 ...
- 配置JDK环境变量
•配置JDK环境变量<Windows系统下> 点击我的电脑右键----->属性------>高级------>环境变量-------> 新建(建议在系统变量中新建 ...
- android 之 桌面的小控件AppWidget
AppWidget是创建的桌面窗口小控件,在这个小控件上允许我们进行一些操作(这个视自己的需要而定).作为菜鸟,我在这里将介绍一下AppWeight的简单使用. 1.在介绍AppWidget之前,我们 ...
- new与malloc的区别
看起来,它们的不同只是new比malloc用起来更方便而已.仅仅是这样吗?不是吗? 1 来源不同: 1)new/delete是C++中的操作符,而malloc/free是C中的标准库函数,需要库文件支 ...
- 使用Ant构建struts2 web工程,自动编译,打包成war
c&c++语言通常使用make脚本来构建和管理自己的工程,同样java也有自己的构建工具(Ant),使用时需要写一个biuld.xml,有点类似c&c++里的makefile. 一.首 ...
- .net串口通信
背景: 前一段时间需要写一个向蓝牙模块发消息的功能. 对蓝牙的机制不太了解,所以一直在查资料, 但始终没找到我需要的东西,还误以为需要配套的一套开发模板和开发包, 偶然间发现只需要简单的串口通信,并且 ...
- 页面间(窗口间)的取值赋值及获取iframe下的window对象
①同一个窗口中,获取某个iframe的信息 <body> <iframe id="PAID" name="PA" src="Item ...