Search code examples
delphiexport-to-excel

Delphi - Add special character in Excel


I'm trying to insert a symbol - white square (Unicode 25A1) into Excel spreadsheet but keep getting errors:

I tried following with no avail:

1. WorkSheet.Cells[CurrRow,CurrCol].Formula := '=ChrW(&H25A1)';  

2. WorkSheet.Cells[CurrRow,CurrCol].Formula := '=Char(25A1)';  

And running macro didn't help either as it said '?'

Really hoping someone could help me with this


Solution

  • The COM interface to Excel is a Unicode API. Excel works internally with Unicode strings. Just pass your special character to Excel in a Delphi WideString. You don't need an Excel formula.

    WorkSheet.Cells[CurrRow,CurrCol].Value := WideString(#$25A1);
    

    If you are using a Unicode version of Delphi (i.e. 2009 or later) then you can include the Unicode character in your source code if you make your source code a UTF-8 file.

    WorkSheet.Cells[CurrRow,CurrCol].Value := WideString('□');
    

    The IDE will convert your source file to UTF-8 if you start adding non-ANSI characters.