【问题标题】:SpreadsheetDocument OpenXML decoding the encoded text in excel fileSpreadsheetDocument OpenXML 解码 excel 文件中的编码文本
【发布时间】:2017-11-28 01:13:34
【问题描述】:

我正在尝试使用 SpreadsheetDocument OpenXML C# 将字符串“Site_x0020_Column_x0020_Test”写入 excel,但是当我们打开 excel 文件时,字符串变为“Site Column Test”。如何在excel文件中保留原字符串。

以下是用于写入excel单元格的代码

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

谢谢

【问题讨论】:

    标签: c# excel


    【解决方案1】:

    According to documentation,下划线充当转义字符,必须对其本身进行转义才能使其正确。

    要存储转义序列的文字形式,初始下划线本身应进行转义(即存储为 x005F)。 [示例:字符串文字 x0008 将存储为 _x005F_x0008_。

    所以,

    "Site_x0020_Column_x0020_Test"
    

    会变成

    "Site_x005F_x0020_Column_x005F_x0020_Test"
    

    我真的希望,分配给cellValue.Text 属性会解决这个问题。如果需要自己编码,请使用XmlConvert.EncodeName 方法。

    【讨论】:

    • 非常好,工作就像一个魅力。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 2014-04-23
    • 2015-05-23
    • 2021-12-13
    • 2013-04-05
    • 2015-10-01
    相关资源
    最近更新 更多