开发者

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.开发者_StackOverflow社区Cells[CurrRow,CurrCol].Formula := '=Char(25A1)';  

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

Really hoping someone could help me with this


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.


You are probably getting an error message which in Excel would be displayed in the Cell as #VALUE!. For one you are feeding it a hex string while it wants a normal number. For another you are giving the Char function a number outside of its acceptable range. The Excel help for CHAR specifically states:

a number between 1 and 255 specifying which character you want. The character is from the character set used by your computer

It appears that Excel does not have any ChrW function. And I can't find any function that takes a numeric value and converts it to its Unicode character equivalent.

Looking through the help, the way you insert Unicode characters is by using the numeric value from the keyboard (holding down alt) or by using the menu: Insert, text, symbol, Unicode (hex). You should be able to simulate the keyboard input, and the menu should be available through the COM model, but not sure whether the character selection then is.

Having said all that, if you are looking for a way to insert Unicode text from Delphi into Excel cells, then all you should need to do, is:

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

And I am only using the Delphi (!) Chr function because you did. You could simply enter assign a text string containing the character you need to the cell in the Delphi source.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜