【发布时间】:2010-05-28 03:22:02
【问题描述】:
我正在使用 LINQ to XML 创建一个Excel XML 文件。我想在单元格中包含换行符。 Excel 使用 
 文字来表示新行。如果我尝试使用 XText 添加它:
XText newlineElement = new XText( "Foo Bar" );
我明白了:
Foo
Bar
在 Excel 中显示为:
Foo
Bar
有没有办法在不做的情况下将&#10 写入 XML 文件
String.Replace( "
", " " )
在结果文件文本上?
编辑 这是一段代码 sn-p,显示了我如何为 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";
...
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&#10;Bar</ss:Data>
</ss:Cell>
<ss:Cell ss:StyleID="s0" />
</ss:Row>
感谢您迄今为止的帮助!
【问题讨论】:
-
信不信由你,这似乎是不可能的。
-
您能否发布一些示例代码,说明这是如何进入 Excel 文件的?
-
基本上,在创建 newlineElement 对象后,如果能看到一些代码显示您是如何使用它的,那就太好了。
标签: c# excel linq-to-xml