单元格中的Excel XML换行

我正在使用LINQ to XML创build一个Excel XML文件。 我想在单元格中包含换行符。 Excel使用 字面上代表一个新的行。 如果我尝试使用XText添加它:

 XText newlineElement = new XText( "Foo
Bar" ); 

我得到:

Foo
Bar

在Excel中显示为:

Foo Bar

有没有办法写&#10 XML文件没有做

 String.Replace( "
", "
" ) 

在生成的文件文本?

编辑这里是一个代码片段,显示了如何为Excel文档创build一个行。 因为它有点混乱,我避免了它。 :)让我知道如果更多的代码会有所帮助,或者如果这只是增加了更多的困惑。

在这个例子中, newlineElement描述的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"; 

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

这产生:

  <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> 

感谢迄今的帮助!

不知道这是否有帮助,但下面的代码:

 var newlineElement = new XText("Foo&#10;Bar"); Console.WriteLine(newlineElement); Console.WriteLine(newlineElement.Value); 

产生这个输出:

Foo$amp;#10;Bar

Foo&#10;Bar

因此,在使用newlineElement对象的代码中,是否可以使用.Value属性?

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

编辑:

好吧 – 这是完全错误的,但帮助我得到(我认为)的作品。 如果你想embedded一些本来会被XML渲染转义的东西,你需要把它embedded到CDATA块中。 看看这个关于C#和XML的相关问题 , 这些问题使我记忆犹新。