Mega Code Archive

 
Categories / Delphi / Printing
 

Printing a Developer Express Quantum Grid

Title: Printing a Developer Express Quantum Grid Question: Without having to purchase the Printing capabilities of the Developer Express Quantum grid, how can I print out my grid? Well, using Excel you can print out the grid nicely without having to purchase anything else, although it assumes you have Excel on your machine. Answer: This procedure will take and input QuantumGrid and print it out to the default printer formatted very nicely. You can use even more Excel automation formatting if you wish but here I just used some of the basics for formatting. procedure PrintQuantumGrid(Grid : TdxDBGrid; bAllRows : boolean = False; bPortrait : boolean = True); var strFilename : string; ExcelApplication : TExcelApplication; WorkSheet : _WorkSheet; begin Grid.Refresh; Application.ProcessMessages; Screen.Cursor := crHourglass; {Create the excel object and try and connect} ExcelApplication := TExcelApplication.Create(Application); try try strFileName := ExtractFilePath(Application.ExeName) + 'grid.xls'; {if the file exists, try and delete the file} if FileExists(strFileName) then if DeleteFile(strFileName) = False then raise Exception.Create('Could not create file: ' + strFilename + '. The file is in use.'); {save the grid as an Excel Spreadsheet} Grid.SaveToXLS(strFileName,bAllRows); {try to connect to Excel} ExcelApplication.ConnectKind := ckRunningOrNew; try ExcelApplication.Connect; except on E: Exception do begin E.Message := 'Excel unavailable.'; raise; end; end; {actually manipulate the document } with ExcelApplication do begin Workbooks.Open(strFilename,EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam,EmptyParam, EmptyParam,0); Columns.AutoFit; {set up properties for the page} Worksheet := ActiveWorkBook.ActiveSheet as _Worksheet; with Worksheet.PageSetup do begin if bPortrait then Orientation := xlPortrait else Orientation := xlLandscape; LeftHeader := 'USERNAME HERE'; RightHeader := '&D'; CenterFooter := 'Page &P of &N'; LeftMargin := 25; RightMargin := 25; TopMargin := 35; BottomMargin := 35; HeaderMargin := 0; FooterMargin := 0; end; {with PageSetup} //Visible[0] := True; //ActiveWorkBook.PrintPreview(True,0); ActiveWorkbook.PrintOut(EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam, EmptyParam,0); ActiveWorkbook.Close(False,EmptyParam,EmptyParam,0); end; finally {free the Excel Object and the status window} ExcelApplication.Disconnect; ExcelApplication.Quit; ExcelApplication.Destroy; Screen.Cursor := crDefault; end; {try finally} except on E: Exception do begin MessageDlg(E.Message,mtError,[mbOK],0); end; end; {try except} end;