gpt4 book ai didi

c# - 单元格中的 Excel XML 换行符

转载 作者:太空狗 更新时间:2023-10-29 20:37:54 30 4
gpt4 key购买 nike

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

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

我得到:

Foo
Bar

在 Excel 中显示为:

Foo Bar

有没有办法在不执行

的情况下将 写入 XML 文件
String.Replace( "
", "
" )

在生成的文件文本上?

编辑 下面是一个代码片段,展示了我如何为 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&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 Bar

那么,在您使用 newlineElement 对象的代码中,您可以使用 .Value 属性吗?

关于c# - 单元格中的 Excel XML 换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2916397/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com