Preface
Recently in the use of Fastreport to develop the report work, of course, also in reading, suddenly thought can be written in the book to achieve a Fastreport help class.
For reference to the third-party class library, we will go to the library before the call to the appropriate encapsulation, that is, the so-called program provider pattern. Encapsulation of third parties, when the need to change a third party (using "fourth Party") does not affect our previous implementation, just need to be processed at the program provider.
Well, don't say much, just go to work:
First, define the basic implementation class
Public classFastreporthelper { PublicFastreport.report report =NULL; Publicfastreport.report Mreport {Get { returnReport ; } Set{ Report=value; } } PublicFastreporthelper (stringTemplateFile,stringprintername, FastReport.Preview.PreviewControl pc) { if(string. IsNullOrEmpty (TemplateFile)) {Throw NewArgumentNullException ("report template cannot be empty! "); } if(!file.exists (templatefile)) { Throw NewFileNotFoundException (string. Format ("report template ' {0} ' does not exist! ", templatefile)); } if(!string. IsNullOrEmpty (printername) &&!printerinfo.validateprinterisexist (printername)) { Throw NewException (string. Format ("printer ' {0} ' does not exist! ")); } if(PC! =NULL) {report. Preview=pc; } Report=NewFastreport.report (); Report. Load (TemplateFile); } Public voidShow () {if(Report! =NULL) {report. Prepare (); Report. Showprepared (); } } Public voidPrint () {if(Report! =NULL) {report. Printprepared (); Report. Print (); } } Public voidPrint (PrinterSettings printsettings) {if(Report! =NULL) { This. Report. Printprepared (printsettings); This. Report. Print (); } } }
Printing required is generally divided into: 1, template, 2, data source, 3, printing settings, of course, the above class does not realize printing several, single-sided printing or double-sided printing, etc., so are the default. If you ask me to write for Mao, please look below
Secondly, the basic implementation class is extended to implement chained programming at the time of invocation.
Public Static classFastreportextend { Public StaticFastreporthelper Regesterdata ( Thisfastreporthelper Fastreporthelper, DataSet DS) { for(inti =0; I < DS. Tables.count; i++) {FastReportHelper.MReport.RegisterData (ds. tables[0], DS. tables[0]. TableName); } returnFastreporthelper; } Public StaticFastreporthelper Regesterdata ( ThisFastreporthelper Fastreporthelper, ienumerable<Object> Data,stringdataname) {fastReportHelper.report.RegisterData (data, dataname); returnFastreporthelper; } Public StaticFastreporthelper Regesterdata ( ThisFastreporthelper Fastreporthelper, DataTable DT,stringDtname ="") { stringName =string. IsNullOrEmpty (dtname)?dt. Tablename:dtname; FastReportHelper.report.RegisterData (dt, name); returnFastreporthelper; } Public StaticFastreporthelper Setparameters ( ThisFastreporthelper Fastreporthelper, dictionary<string,Object>dicparameters) { if(Dicparameters! =NULL|| Dicparameters.count >0) { foreach(keyvaluepair<string,Object> Iteminchdicparameters) {FastReportHelper.report.SetParameterValue (item. Key, item. Value); } } returnFastreporthelper; } Public StaticFastreport.report Setparameters ( ThisFastreport.report report, dictionary<string,Object>dicparameters) { if(Dicparameters! =NULL|| Dicparameters.count >0) { foreach(keyvaluepair<string,Object> Iteminchdicparameters) {report. Setparametervalue (item. Key, item. Value); } } returnReport ; } }
Finally, the printer gets
/// <summary> /// /// </summary> Public classPrinterinfo {/// <summary> ///determine if the printer exists/// </summary> /// <param name= "printername" ></param> /// <returns></returns> Public Static BOOLValidateprinterisexist (stringprintername) { foreach(varIteminchprintersettings.installedprinters) {if(Item = =printername)return true; } return false; } /// <summary> ///get the default printer name/// </summary> /// <returns></returns> Public Static stringGetDefaultPrinter () {return NewPrintDocument (). Printersettings.printername; } }
A mess, recently in the vb.net, want to have the heart of excrement have.
Wash yourself and sleep.
Fastreport extension Classes