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打印通用代码的更多相关文章

  1. DevExpress Winform 通用控件打印方法(允许可自定义边距) z

    DevExpress Winform 通用控件打印方法,包括gridcontrol,treelist,pivotGridControl,ChartControl,LayoutControl...(所有 ...

  2. 设计模式--5.5 代理模式-通用代码及aop

    1.通用代码 (1)Subjects package com.design.代理模式.通用代码; public interface Subject { void request(); } (2)Rea ...

  3. axis1客户端调用webservice的通用代码

    1.axis1 作为web service 客户端时,调用web service 服务端的通用代码 String url = "http://www.webxml.com.cn/webser ...

  4. Rsa加解密Java、C#、php通用代码 密钥转换工具

    之前发了一篇"TripleDes的加解密Java.C#.php通用代码",后面又有项目用到了Rsa加解密,还是在不同系统之间进行交互,Rsa在不同语言的密钥格式不一样,所以过程中主 ...

  5. python爬取网页的通用代码框架

    python爬取网页的通用代码框架: def getHTMLText(url):#参数code缺省值为‘utf-8’(编码方式) try: r=requests.get(url,timeout=30) ...

  6. Lodop设置打印维护返回打印语句代码

    打印设计关闭时,可以返回设计的打印代码,打印维护则返回成功打印的次数.不过打印维护也是返回打印程序代码的,方法就是加个语句,这个语句是: LODOP.SET_PRINT_MODE("PRIN ...

  7. 不会提示是否关闭浏览器 IE6、IE7、FF通用代码

    start = setInterval('CloseWin();', 2000); function CloseWin() //这个不会提示是否关闭浏览器 IE6.IE7.FF通用代码: { if ( ...

  8. [Spring cloud 一步步实现广告系统] 4. 通用代码模块设计

    一个大的系统,在代码的复用肯定是必不可少的,它能解决: 统一的响应处理(可以对外提供统一的响应对象包装) 统一的异常处理(可以将业务异常统一收集处理) 通用代码定义.配置定义(通用的配置信息放在统一的 ...

  9. 百度UEditor添加视频 增加支持“通用代码”功能,支持微信

    今天发现用UEditor默认的添加视频,在微信内置浏览器里无法正常显示.估计是微信屏蔽了UEditor使用的<embeded>标签.插入iframe形式的通用代码则能正常显示. 用百度UE ...

随机推荐

  1. ubuntu 使用串口picocom

    连上USB转串口 查看是否识别串口     dmesg | grep ttyUSB0 安装(mint / ubuntu): $ sudo apt-get install picocom 使用: 先赋予 ...

  2. Python2 显示 unicode

    用户想要看的是 u'中文' 而不是 u'\u4e2d\u6587',但是在 Python2 中有时并不能实现. 转译 转义字符是这样一个字符,标志着在一个字符序列中出现在它之后的后续几个字符采取一种替 ...

  3. mysql 数据库备 及移动当天数据到历史表里 window下可用

    1 数据移动到历史表中,同时删除当天数据 test-move-record.bat c: cd C:\FQ_ManageServer\mysqlback mysql.exe -h 10.71.1.23 ...

  4. pro-select-limit-if

    drop procedure if exists p9; CREATE PROCEDURE p9 () BEGIN DECLARE a INT; DECLARE b INT; DECLARE c IN ...

  5. mysql 使用命令执行外部sql文件

    语句 source e:\\phpPro\\fileName.sql 注意,不需要再后面加  分号  !!!!!

  6. Converts Internet addresses to Internet numbers. ip2long long2ip

    http://php.net/manual/en/function.long2ip.phpPHP: ip2long - Manual http://php.net/manual/en/function ...

  7. awesome-modern-cpp

    Awesome Modern C++ A collection of resources on modern C++. The goal is to collect a list of resouce ...

  8. 原!mysql5.6 存储过程 批量建表

    由于业务需求,需要按天分表,因此写了个存储过程,根据时间生成表. 根据createTime 的时间,以及 while循环的变量设置范围,生成该指定日期及之后的多张表. BEGIN ); ); ; '; ...

  9. redis 字符串和集合操作

    字符串 redis中的String在在内存中按照一个name对应一个value来存储 set() #在Redis中设置值,默认不存在则创建,存在则修改 r.set('name', 'zhangsan' ...

  10. 【我的Android进阶之旅】如何隐藏Android中EditText控件的默认下划线

    Android EditText控件是经常使用的控件,但是有时候我们并不需要它的一些默认的属性,比如说下划线,因为有时候这样的默认下划线看起来特别怪异,和其他控件在一起搭配的时候不协调,因此有时候就需 ...