0

I'm aware of the following solution for adding symbols into cells using closedxml in vb

ws.Cell(1, 1).Value = "❶ Symbol";

(How can I add symbols to a cell using closedxml?)

And I am currently using the below example to set the wingdings value in c#

private void CreateExcelTableBodyCellSymbolCircle(IXLWorksheet worksheet, string cellID, XLColor colour)
{
    const string circleSymbol = "l";

    PopulateExcelTableCellWithSymbol(worksheet, cellID, colour, circleSymbol);
}

private void PopulateExcelTableCellWithSymbol(IXLWorksheet worksheet, string cellID, XLColor colour, string symbolValue)
{
    worksheet.Cell(cellID).DataType = XLCellValues.Text;
    worksheet.Cell(cellID).Value = symbolValue;
    worksheet.Cell(cellID).Style.Font.FontName = "Wingdings";
    worksheet.Cell(cellID).Style.Font.FontColor = colour;
}

But I'm looking for a way to add the symbols into the cell by utilising the symbol's hex character code (displayed in the symbol popup in excel)

Kelsall
  • 57
  • 2
  • 14
  • Could you clarify what you mean by "the symbol character code"? It's not clear to me what you want to be able to do - if you could show the code you'd *like* to use, but which presumably doesn't work at the moment, that would really help to clarify the question. – Jon Skeet Aug 01 '18 at 09:28
  • @DaisyShipton I have edited the question. Thank you. – Kelsall Aug 01 '18 at 10:01
  • You talk about "calling a function with the symbol's hex character code" but you haven't shown what *exactly* you mean by that. Would you really have the character code as a string, which is what you appear to be trying to use in the second snippet? If you have a `char`, you can just call `ToString()` on that char, for example. – Jon Skeet Aug 01 '18 at 10:04
  • @DaisyShipton Ill be writing a function to add the hex character code into the string, as the previous example uses a string with the symbol character. Passing in the hex value into the function and then formatting the value assigned to the cell with the passed in hex value – Kelsall Aug 01 '18 at 10:09
  • I'm asking you to show what you'd like the function to look like. Then the answer can basically be an implementation of that function. – Jon Skeet Aug 01 '18 at 10:35

2 Answers2

1

This is more about strings in C# and less about ClosedXML. You can use the char.ConvertFromUtf32() method like this:

worksheet.Cell(cellID).Value = char.ConvertFromUtf32(0x008C) + " Symbol";

PS: For me, 008C does not give a valid symbol, but for example 2776 gives a symbol similar to that in your question.

Raidri
  • 17,258
  • 9
  • 62
  • 65
0

Following on from Raidri's answer - which I've accepted - as it was more along the lines strings. The solution in C# is below

private void CreateExcelTableBodyCellSymbolCircle(IXLWorksheet worksheet, string cellID, XLColor colour)
{
    string circleSymbol = char.ConvertFromUtf32(0x006C);

    PopulateExcelTableCellWithSymbol(worksheet, cellID, colour, circleSymbol);
}

private void PopulateExcelTableCellWithSymbol(IXLWorksheet worksheet, string cellID, XLColor colour, string symbolValue)
{
    worksheet.Cell(cellID).DataType = XLCellValues.Text;
    worksheet.Cell(cellID).Value = symbolValue;
    worksheet.Cell(cellID).Style.Font.SetFontName("Wingdings");
    worksheet.Cell(cellID).Style.Font.SetFontColor(colour);
}
Kelsall
  • 57
  • 2
  • 14