drawing borders around groups of cells

Given a rectangular block of cells, say B2..D5, is there a built-in method to draw a border around that block of cells? Or must I code that behavior in the application?


Thanks,

Bill Meyer

Bill,

Sorry for the delay, it's been a busy day today.  Currently there isn't a function specific to draw borders, but one of the overloads of SetCellFormat takes as last parameter a "ExteriorBorders" boolean. When true, this setcellformat will draw a box over the cells.

Here is a simple application showing how it would work:

program Project58;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, VCL.FlexCel.Core, FlexCel.XlsAdapter;


procedure DrawBorders(const Xls: TExcelFile; const a1, a2: TCellAddress;
                      const BorderStyle: TFlxBorderStyle; const BorderColor: TExcelColor);
var
  Fmt: TFlxFormat;
  ApplyFmt: TFlxApplyFormat;
begin
  Fmt := Xls.GetDefaultFormat;
  Fmt.Borders.Left := TFlxOneBorder.Create(BorderStyle, BorderColor);
  Fmt.Borders.Right := Fmt.Borders.Left;
  Fmt.Borders.Top := Fmt.Borders.Left;
  Fmt.Borders.Bottom := Fmt.Borders.Left;

  ApplyFmt.SetAllMembers(false);
  ApplyFmt.Borders.SetAllMembers(true);

  Xls.SetCellFormat(a1.Row, a1.Col, a2.Row, a2.Col, Fmt, ApplyFmt, true);

end;

var
  Xls: TXlsFile;
begin
  Xls := TXlsFile.Create(1, TExcelFileFormat.v2013, true);
  try
    DrawBorders(Xls, TCellAddress.Create('B2'), TCellAddress.Create('D5'), TFlxBorderStyle.Thin, Colors.Black);

    Xls.Save('r:\test.xlsx');
  finally
    Xls.Free;
  end;

end.

It isn't really complex, but this post got me thinking that we could add a "SetBorders" method to TXlsFile which would basically do the same as the "DrawBorders" method above. It will probably be much easier to find than the SetCellFormat with ExteriorBorders = true. On the other side, one nice thing of SetCellFormat over a "DrawBorders" is that SetCellFormat can do the borders and other stuff at the same time. For example, you could set the borders and the fill color in the same call by changing the ApplyFmt variable above.

Adrian,

Thanks for your reply. I see a number of things I need to explore. I had not yet seen the SetAllMembers method, so I need to look that up. Is this DrawBorders, though, only going to draw the outer border, and leave the borders, if any, on the cells inside the rectangle unaffected? And yes, being able to do the borders and the fill at the same time would be nice.

Thanks again for your prompt and very useful reply.

OK, I ran your sample, and did some further experiments. I now have a routine for the borders and another which also fills the cell shading. Both much nicer than what I thought I would have to code!