Search code examples
delphiexceldelphi-2007tms

Help required in new version of flexcel for Delphi 2007


I am using flexcel component to generate Excel files from Delphi 2007. Now we switched to the latest version of this component because the old one was not compatible with Excel 2010.

Now I'm facing an issue when we set the format of each cell to a different value. A file is getting generated but with default formatting for all the cells.

Below is a code snippet that demonstrates this behavior:

var
  V: TXlsCellValue;
begin
  with V do
  begin
    Value := S; //text
    XF := Fmt;  //format
    IsFormula := false;
  end;
  FXls.AssignCellDataX(Succ(Row), C, V);  // FXls : TXLSFile; 
end;

Solution

  • This is one of the solution that I am able to find. Here we need to create our own customized formats.

    Fmt: TFlxFormat;
    GetDefaultFormat(Fmt); // used to get the default format of a blank cell 
    

    and below is the function to add format to the format list of this component

    Fmt.Font.Size20 := 240;
    Rmt.WrapText := True;
    tempXF:= FlexCelImport1.AddFormat(Fmt);
    

    This AddFormat function will return an integer value which denotes newly created format number. Hereon we can use this tempXF number to format cell.

    Below is the working snippet:

    var
        V: TXlsCellValue;
    begin
        with V do
        begin
            Value := S; // text to be inserted
            XF := tempXF; // newly created format
            IsFormula := FALSE;
        end;
        FXls.AssignCellDataX(Succ(Row), Col, V); // FXls : TXLSFile; 
    End;
    

    Anyone with answers having better approach is always appreciated.