Recent

Author Topic: [Solved] Printing a TsWorksheetGrid  (Read 1692 times)

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 590
    • Double Dummy Solver - free download
[Solved] Printing a TsWorksheetGrid
« on: August 04, 2018, 08:13:13 pm »
I'm trying to print a grid, using fpSpreadSheet 1.8.4 . My code, shown below, seems to work perfectly, but for only one page??? There are actually 13 pages and a a total of 630 lines in my grid.

Is there a sample program that shows how to print a grid.

Code: Pascal  [Select][+][-]
  1. procedure PrintWSGrid(aGrid: TsWorksheetGrid; aCanvas: TCanvas; aTitle: string);
  2. var
  3.   X, X1, Y, w, h, t, aCol, aRow: integer;
  4.   s: string;
  5.   a: TsHorAlignment;
  6.   lCell: PCell;
  7.   fmt: PsCellFormat;
  8.   cbs: TsCellBorders;
  9.   fss: TsFontStyles;
  10.   fs: TFontStyles;
  11.   clr: TSColor;
  12.   sty: TFontStyles;
  13. begin
  14.   t := 20;
  15.   with aCanvas do
  16.   begin
  17.     aCanvas.Pen.Color := clBlack;
  18.     aCanvas.Font.Name := AGrid.Font.Name;
  19.     aCanvas.Font.Size := 12;
  20.     aCanvas.Font.Style := [fsBold];
  21.     aCanvas.TextOut(50, 100, aTitle);
  22.     Y := AGrid.RowCount;
  23.     Y := 200;
  24.     h := 130;
  25.     aCanvas.Font.Size := 8;
  26.     aCanvas.Font.Style := [];
  27.  
  28.     for aRow := 1 to AGrid.RowCount - 1 do
  29.     begin
  30.       x := 100;
  31.       y := y + h;
  32.       for aCol := 0 to aGrid.ColCount - 1 do
  33.       begin
  34.         lCell := aGrid.Worksheet.FindCell(ARow, ACol);
  35.         fmt := aGrid.Worksheet.GetPointerToEffectiveCellFormat(lCell);
  36.         w := Round(PrinterScaleX(aGrid.ColWidths[aCol + 1]));
  37.         s := aGrid.Worksheet.ReadAsText(aRow, aCol);
  38.         a := fmt^.HorAlignment;
  39.         if a = haCenter then
  40.           X1 := x + (w - PrinterScaleX(aGrid.Canvas.TextWidth(s))) div 2
  41.         else if a = haRight then
  42.           X1 := x + w - PrinterScaleX(aGrid.Canvas.TextWidth(s))
  43.         else
  44.           X1 := x;
  45.  
  46.         clr := aGrid.CellFontColor[ACol + 1, ARow + 1];
  47.         aCanvas.Font.Color := clr;
  48.  
  49.         fss := aGrid.CellFontStyle[ACol + 1, ARow + 1];
  50.         sty := [];
  51.         if fssBold in fss then
  52.           sty := sty + [fsBold];
  53.         if fssUnderline in fss then
  54.           sty := sty + [fsUnderline];
  55.         if fssItalic in fss then
  56.           sty := sty + [fsItalic];
  57.         aCanvas.Font.Style := sty;
  58.  
  59.         aCanvas.TextOut(X1, y, s);
  60.  
  61.         cbs := fmt^.Border;
  62.         if cbs <> [] then
  63.         begin
  64.           aCanvas.Brush.Color := clBlack;
  65.           if cbNorth in cbs then
  66.             aCanvas.FillRect(X, y, x + w, y + t);
  67.           if cbEast in cbs then
  68.             aCanvas.FillRect(X + w - t, y, x + w, y + h);
  69.           if cbSouth in cbs then
  70.             aCanvas.FillRect(X, y + h - t, x + w, y + h);
  71.           if cbWest in cbs then
  72.             aCanvas.FillRect(X, y, x + t, y + h);
  73.           fss := aGrid.CellFontStyles[acol, arow, acol + 1, arow + 1];
  74.           fs := aCanvas.Font.Style;
  75.           if fssBold in fss then
  76.             fs := [fsBold]
  77.           else
  78.             fs := [];
  79.           if fssUnderline in fss then
  80.             fs := fs + [fsUnderline];
  81.           aCanvas.Font.Style := fs;
  82.           aCanvas.Brush.Color := clWhite;
  83.         end;
  84.  
  85.         X := X + w;
  86.       end;
  87.     end;
  88.  
  89.   end;
  90. end;  
« Last Edit: August 05, 2018, 02:46:09 am by bobonwhidbey »
Lazarus 3.0RC2, FPC 3.2.2 x86_64-win64-win32/win64

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Printing a TsWorksheetGrid
« Reply #1 on: August 04, 2018, 11:19:15 pm »
you need to adjust your code to print a page at a time and if there are more pages then use the "NewPage" call to move to the
next page of course..
look here for a quick run down..

  https://www.freepascal.org/~michael/articles/lazprint/lazprint.pdf

 The NewPage function caches each page and when you hit the EndDoc which then will print the pages.
 
 
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Printing a TsWorksheetGrid
« Reply #2 on: August 05, 2018, 12:43:47 am »
Here is more (but not fully) complete code to print a worksheet. It does support text alignment, text rotation, (simple) background color (no patterns, though), borders, individual character formatting per cell, row height, col width, page break if there are more rows than fit on a page (but no page breaks if rows are longer than page width).

To does not support text overflow (too long text flowing into adjacent cells) and merged cells. If you need this you should study the code of TsWorksheetGrid and copy the required fragments - all the features of this demo are just copied (and slightly modified) parts of the TsWorksheetGrid code.

It also does not support the features of the PageLayout, i.e. col/row header titles repeated on each page.

So, there's still a lot to do - I knew why I never touched printing...

 

TinyPortal © 2005-2018