单元格中的Excel XML换行符

spa*_*rto 7 c# excel linq-to-xml

我正在使用LINQ to XML来创建Excel XML文件.我想在单元格中包含换行符.Excel使用
文字表示新行.如果我尝试使用XText添加它:

XText newlineElement = new XText( "Foo
Bar" );
Run Code Online (Sandbox Code Playgroud)

我明白了:

Foo
Bar

在Excel中显示为:

Foo
Bar

有没有办法在&#10不做的情况下写入XML文件

String.Replace( "
", "
" )
Run Code Online (Sandbox Code Playgroud)

在生成的文件文本?

编辑这是一个代码片段,显示我如何为Excel文档创建一行.因为它有点凌乱,我正在避开它.:)让我知道更多的代码是否会有所帮助,或者这是否会增加更多的混淆.

在该示例中,newlineElement上述内容由代码示例中的唯一XText表示.

const string CELL = "{urn:schemas-microsoft-com:office:spreadsheet}Cell";
const string DATA = "{urn:schemas-microsoft-com:office:spreadsheet}Data";
const string STYLE = "{urn:schemas-microsoft-com:office:spreadsheet}StyleID";
const string TYPE= "{urn:schemas-microsoft-com:office:spreadsheet}Type";   
const string HEIGHT = "{urn:schemas-microsoft-com:office:spreadsheet}Height";
const string ROW = "{urn:schemas-microsoft-com:office:spreadsheet}Row";
Run Code Online (Sandbox Code Playgroud)

...

XElement rowElement = new XElement( Row,
                                    new XElement( CELL,
                                        new XAttribute( STYLE, "s0" ) ),
                                    new XElement( CELL,
                                        new XElement( DATA,
                                            new XText( description.Replace("\n", "
") ), 
                                            new XAttribute( TYPE, "String" ) ),
                                        new XAttribute( STYLE, "sWrap" ) ),
                                    new XElement( CELL,
                                        new XAttribute( STYLE, "s0" ) ),
                                    new XAttribute( HEIGHT, height ) );
Run Code Online (Sandbox Code Playgroud)

这会产生:

  <ss:Row ss:Height="45">
    <ss:Cell ss:StyleID="s0" />
    <ss:Cell ss:StyleID="sWrap">
      <ss:Data ss:Type="String">Foo&amp;#10;Bar</ss:Data>
    </ss:Cell>
    <ss:Cell ss:StyleID="s0" />
  </ss:Row>
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助!

AJ.*_*AJ. 1

将实际字符而不是转义代码插入到字符串中 - LINQ 将为您处理转换。

编辑:

好吧 - 这是完全错误的,但帮助我得到了一些(我认为)有效的东西。如果要嵌入 XML 呈现时会转义的内容,则需要将其嵌入到块中CDATA。查看有关 C# 和 XML的相关问题,这勾起了我的记忆。