DevExpress打印通用代码
DevExpress打印相关代码
using System;
using System.Windows.Forms;
using DevExpress.XtraPrinting;
using System.Xml.Serialization;
namespace MyDevExpressDemo
{
/// <summary>
/// PrintSettingController 的摘要说明。
/// </summary>
public class PrintSettingController
{
PrintingSystem ps = null;
string formName=null;
DevExpress.XtraPrinting.PrintableComponentLink link=null;
/// <summary>
///
/// </summary>
/// <param name="control">要打印的部件</param>
/// <param name="FormName">此部件对应的布局信息</param>
public PrintSettingController(IPrintable control,string FormName)
{
formName=FormName;
ps=new DevExpress.XtraPrinting.PrintingSystem();
link=new DevExpress.XtraPrinting.PrintableComponentLink(ps);
ps.Links.Add(link);
link.Component=control;
ps.PageSettingsChanged-=new EventHandler(ps_PageSettingsChanged);
LoadPageSetting();
ps.PageSettingsChanged+=new EventHandler(ps_PageSettingsChanged);
ps.AfterMarginsChange+=new MarginsChangeEventHandler(ps_AfterMarginsChange);
}
public void Preview()
{
try
{
if(DevExpress.XtraPrinting.PrintHelper.IsPrintingAvailable)
{
Cursor.Current=Cursors.AppStarting;
if(_PrintHeader!=null)
{
PageHeaderFooter phf = link.PageHeaderFooter as PageHeaderFooter;
phf.Header.Content.Clear();
phf.Header.Content.AddRange(new string[] {"",_PrintHeader,""});
phf.Header.Font=new System.Drawing.Font("宋体",14,System.Drawing.FontStyle.Bold);
phf.Header.LineAlignment=BrickAlignment.Center;
}
link.PaperKind=ps.PageSettings.PaperKind;
link.Margins=ps.PageSettings.Margins;
link.Landscape=ps.PageSettings.Landscape;
link.CreateDocument();
ps.PreviewForm.Show();
}
else
{
Cursor.Current=Cursors.Default;
MessageBox.Show("打印机不可用", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
finally
{
Cursor.Current=Cursors.Default;
}
}
/// <summary>
/// 打印控制器
/// </summary>
/// <param name="control">要打印的部件</param>
public PrintSettingController(IPrintable control)
{
if(control==null)return;
Control c=(Control)control;
formName=c.FindForm().GetType().FullName+"."+c.Name;
ps=new DevExpress.XtraPrinting.PrintingSystem();
link=new DevExpress.XtraPrinting.PrintableComponentLink(ps);
ps.Links.Add(link);
link.Component=control;
ps.PageSettingsChanged-=new EventHandler(ps_PageSettingsChanged);
LoadPageSetting();
ps.PageSettingsChanged+=new EventHandler(ps_PageSettingsChanged);
ps.AfterMarginsChange+=new MarginsChangeEventHandler(ps_AfterMarginsChange);
}
public void ExportToHtml()
{
try
{
using(SaveFileDialog fd=new SaveFileDialog())
{
fd.Title="导出HTML文件";
fd.RestoreDirectory=true;
fd.Filter="HTML文件|*.htm";
fd.FilterIndex=1;
if(fd.ShowDialog()==DialogResult.OK)
{
// if(obj is DevExpress.XtraGrid.GridControl)
// {
// ((DevExpress.XtraGrid.GridControl)obj).ExportToHtml(fd.FileName);
// MessageBox.Show("文件导出成功","导出",MessageBoxButtons.OK,MessageBoxIcon.Information);
// }
// else if(obj is DevExpress.XtraTreeList.TreeList)
// {
link.CreateDocument();
ps.ExportToHtml(fd.FileName);
MessageBox.Show("文件导出成功","导出",MessageBoxButtons.OK,MessageBoxIcon.Information);
// }
}
}
}
finally
{
}
}
/// <summary>
/// 网格分组时要导出,请使用这个,
/// </summary>
public void GridGroupToExcel()
{
DevExpress.XtraGrid.GridControl grid=this.link.Component as DevExpress.XtraGrid.GridControl;
if(grid!=null)
{
using(SaveFileDialog fd=new SaveFileDialog())
{
fd.Title="导出Excel文件";
fd.RestoreDirectory=true;
fd.Filter="Excel文件|*.xls";
fd.FilterIndex=1;
if(fd.ShowDialog()==DialogResult.OK)
{
grid.ExportToExcel(fd.FileName);
MessageBox.Show("文件导出成功","导出",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
}
}
public void ExportToExcel()
{
try
{
using(SaveFileDialog fd=new SaveFileDialog())
{
fd.Title="导出Excel文件";
fd.RestoreDirectory=true;
fd.Filter="Excel文件|*.xls";
fd.FilterIndex=1;
if(fd.ShowDialog()==DialogResult.OK)
{
// if(obj is DevExpress.XtraGrid.GridControl)
// {
// ((DevExpress.XtraGrid.GridControl)obj).ExportToExcel(fd.FileName);
// MessageBox.Show("文件导出成功","导出",MessageBoxButtons.OK,MessageBoxIcon.Information);
// }
// else if(obj is DevExpress.XtraTreeList.TreeList)
// {
link.CreateDocument();
ps.ExportToXls(fd.FileName);
MessageBox.Show("文件导出成功","导出",MessageBoxButtons.OK,MessageBoxIcon.Information);
// }
}
}
}
finally
{
}
}
string _PrintHeader=null;
/// <summary>
/// 打印时的标题
/// </summary>
public string PrintHeader
{
set
{
_PrintHeader=value;
}
}
/// <summary>
/// 进行打印
/// </summary>
public void Print()
{
try
{
if(DevExpress.XtraPrinting.PrintHelper.IsPrintingAvailable)
{
if(_PrintHeader!=null)
{
PageHeaderFooter phf = link.PageHeaderFooter as PageHeaderFooter;
phf.Header.Content.Clear();
phf.Header.Content.AddRange(new string[] {"",_PrintHeader,""});
phf.Header.Font=new System.Drawing.Font("宋体",14,System.Drawing.FontStyle.Bold);
phf.Header.LineAlignment=BrickAlignment.Center;
}
link.PaperKind=ps.PageSettings.PaperKind;
link.Margins=ps.PageSettings.Margins;
link.Landscape=ps.PageSettings.Landscape;
link.CreateDocument();
link.CreateDocument();
ps.Print();
}
else
{
Cursor.Current=Cursors.Default;
MessageBox.Show("打印机不可用", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
finally
{
}
}
private void ps_AfterMarginsChange(object sender, MarginsChangeEventArgs e)
{
SavePageSetting();
}
private void ps_PageSettingsChanged(object sender, EventArgs e)
{
SavePageSetting();
}
//获取页面设置信息
void LoadPageSetting()
{
try
{
string path=System.Windows.Forms.Application.StartupPath+"\\PrintLayout";
if(!System.IO.Directory.Exists(path))
{
return;
}
path+="\\"+formName+".xml";
if(!System.IO.File.Exists(path))
{
return;
}
XmlSerializer ser=new XmlSerializer(typeof(UserPageSetting));
UserPageSetting setting=(UserPageSetting)ser.Deserialize(new System.IO.FileStream(path,System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.ReadWrite));
System.Drawing.Printing.Margins m=new System.Drawing.Printing.Margins(setting.Left,setting.Right,setting.Top,setting.Bottom);
ps.PageSettings.Assign(m,(System.Drawing.Printing.PaperKind)setting.PaperKind,setting.Landscape);
}
catch{}
}
/// <summary>
/// 保存当前网格的布局
/// </summary>
void SavePageSetting()
{
try
{
string path=System.Windows.Forms.Application.StartupPath+"\\PrintLayout";
if(!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
path+="\\"+formName+".xml";
DevExpress.XtraPrinting.XtraPageSettings setting= ps.PageSettings;
UserPageSetting s=new UserPageSetting();
s.Landscape=setting.Landscape;
s.Left=setting.Margins.Left;
s.Right=setting.Margins.Right;
s.Top=setting.Margins.Top;
s.Bottom=setting.Margins.Bottom;
s.PaperKind=(int)setting.PaperKind;
XmlSerializer ser=new XmlSerializer(s.GetType());
ser.Serialize(new System.IO.FileStream(path,System.IO.FileMode.Create,System.IO.FileAccess.Write,System.IO.FileShare.ReadWrite),s);
}
catch{}
}
}
/// <summary>
/// 最终用户对某个打印页的设置
/// </summary>
[Serializable()]
public class UserPageSetting
{
public UserPageSetting()
{
}
public bool Landscape;
public int PaperKind;
public int Top;
public int Bottom;
public int Left;
public int Right;
}
}
DevExpress打印通用代码的更多相关文章
- DevExpress Winform 通用控件打印方法(允许可自定义边距) z
DevExpress Winform 通用控件打印方法,包括gridcontrol,treelist,pivotGridControl,ChartControl,LayoutControl...(所有 ...
- 设计模式--5.5 代理模式-通用代码及aop
1.通用代码 (1)Subjects package com.design.代理模式.通用代码; public interface Subject { void request(); } (2)Rea ...
- axis1客户端调用webservice的通用代码
1.axis1 作为web service 客户端时,调用web service 服务端的通用代码 String url = "http://www.webxml.com.cn/webser ...
- Rsa加解密Java、C#、php通用代码 密钥转换工具
之前发了一篇"TripleDes的加解密Java.C#.php通用代码",后面又有项目用到了Rsa加解密,还是在不同系统之间进行交互,Rsa在不同语言的密钥格式不一样,所以过程中主 ...
- python爬取网页的通用代码框架
python爬取网页的通用代码框架: def getHTMLText(url):#参数code缺省值为‘utf-8’(编码方式) try: r=requests.get(url,timeout=30) ...
- Lodop设置打印维护返回打印语句代码
打印设计关闭时,可以返回设计的打印代码,打印维护则返回成功打印的次数.不过打印维护也是返回打印程序代码的,方法就是加个语句,这个语句是: LODOP.SET_PRINT_MODE("PRIN ...
- 不会提示是否关闭浏览器 IE6、IE7、FF通用代码
start = setInterval('CloseWin();', 2000); function CloseWin() //这个不会提示是否关闭浏览器 IE6.IE7.FF通用代码: { if ( ...
- [Spring cloud 一步步实现广告系统] 4. 通用代码模块设计
一个大的系统,在代码的复用肯定是必不可少的,它能解决: 统一的响应处理(可以对外提供统一的响应对象包装) 统一的异常处理(可以将业务异常统一收集处理) 通用代码定义.配置定义(通用的配置信息放在统一的 ...
- 百度UEditor添加视频 增加支持“通用代码”功能,支持微信
今天发现用UEditor默认的添加视频,在微信内置浏览器里无法正常显示.估计是微信屏蔽了UEditor使用的<embeded>标签.插入iframe形式的通用代码则能正常显示. 用百度UE ...
随机推荐
- struts2的配置文件为什么可以使用${}符号?
转自:https://www.cnblogs.com/sharpest/p/6030265.html 一.#符号的用途一般有三种. “#”主要有三种用途: 1. 访问OGNL上下文和Action上下文 ...
- angular下如何绑定form表单的change事件
ng-change = "formChange()" 不起作用 应该改为: onchanged = "angular.element(this).scope().form ...
- 关于canvas绘制大转盘并旋转
O(∩_∩)O包子不才,最近磕磕巴巴写了一个大转盘的效果.现在想说一下整个的思路部分,要是有设么不对的还请多多指教,期待共同成为优秀的前端~~大转盘整个思路: 绘制整个转盘 编写一个随机数,用来当接口 ...
- 直播未来属于RTMP还是HTTP
直播未来属于RTMP还是HTTP? - Tinywan - 博客园 https://www.cnblogs.com/tinywan/p/6122065.html 直播未来属于RTMP还是HTTP? H ...
- 常用的SQLalchemy 字段类型
https://blog.csdn.net/weixin_41896508/article/details/80772238 常用的SQLAlchemy字段类型 类型名 python中类型 说明 In ...
- Grunt-Less批量编译为css
Grunt批量编译less module.exports = function (grunt) { grunt.initConfig({ less: { main: { expand: true, s ...
- 总结学习! xml与java对象转换 --- JDK自带的JAXB(Java Architecture for XML Binding)
JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例文档反向 ...
- 一.数据库连接对象connection
1.python 3.5,需要把MySQLdb换成pymysql
- 修改sql server实例、数据库、表、字段的排序规则
转自:http://blog.51cto.com/jimshu/1095780 概念与详情请参考:字符编码与排序规则:https://www.cnblogs.com/gered/p/9145123.h ...
- smtplib与email模块(实现邮件的发送)
SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和email两个模块,email负责构造邮件, ...