Kelsall
Kelsall

Reputation: 57

Insert symbol into a cell using closedxml and character code

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)

Upvotes: 0

Views: 2211

Answers (2)

Kelsall
Kelsall

Reputation: 57

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);
}

Upvotes: 0

Raidri
Raidri

Reputation: 17550

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.

Upvotes: 1

Related Questions