SpreadsheetDocument OpenXML decoding the encoded text in excel file

Viewed 744

I am trying to write a string "Site_x0020_Column_x0020_Test" to excel using SpreadsheetDocument OpenXML C#, but when we open excel file the string becomes "Site Column Test". How to preserve the original string in the excel file.

Below is the code used to write in to excel cell

        // Add a new Excel Cell to our Row 
        var cell = new Cell { CellReference = cellReference, DataType = CellValues.String};
        var cellValue = new CellValue();
        cellValue.Text = cellStringValue;
        cell.Append(cellValue);
        excelRow.Append(cell);

Thanks

1 Answers

According to documentation, the underscore acts as an escape character, which itself must be escaped to get it right.

To store the literal form of an escape sequence, the initial underscore shall itself be escaped (i.e. stored as x005F). [Example: The string literal x0008 would be stored as _x005F_x0008_.

So,

"Site_x0020_Column_x0020_Test"

would become

"Site_x005F_x0020_Column_x005F_x0020_Test"

I really would have expected, that the assignment to the cellValue.Text property took care of this. If you need to encode it yourself, use the XmlConvert.EncodeName method.

Related