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
有没有办法在
不做的情况下写入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&#10;Bar</ss:Data>
</ss:Cell>
<ss:Cell ss:StyleID="s0" />
</ss:Row>
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助!