DevExpress打印相关代码-ASP教程,打印相关
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;
}
}
怎么使用:
printsettingcontroller pc=new printsettingcontroller(this.treelist1); //这里能是所有实现iprintable 的控件
pc.printheader="我的报表";
pc.preview();