如何在 OPENXML 电子表格单元格中插入换行符?
我目前正在使用类似的方法在单元格中插入内联字符串:
new Cell()
{
CellReference = "E2",
StyleIndex = (UInt32Value)4U,
DataType = CellValues.InlineString,
InlineString = new InlineString(new Text( "some text"))
}
但是 \n
无法插入换行符,我该怎么做?
响应
new Cell(
new CellValue("string \n string")
)
{
CellReference = "E2",
StyleIndex = (UInt32Value)4U,
DataType = CellValues.String
}
I'm currently using something like this to insert inline string in a cell :
new Cell()
{
CellReference = "E2",
StyleIndex = (UInt32Value)4U,
DataType = CellValues.InlineString,
InlineString = new InlineString(new Text( "some text"))
}
But \n
doesn't work to insert line break, how can i do this?
The response
new Cell(
new CellValue("string \n string")
)
{
CellReference = "E2",
StyleIndex = (UInt32Value)4U,
DataType = CellValues.String
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要执行两件事:
1.) 将单元格标记为“换行文本”。 如果您使用现有电子表格作为模板,则可以在电子表格中手动执行此操作。 只需右键单击单元格并选择“设置单元格格式..”,单击“对齐”选项卡并选中“自动换行< /em>”复选框。
或...您可以通过编程方式设置单元格格式。 如果您有一个名为“cf”的 CellFormat 对象,您将执行如下操作:
2.) 您不应使用“\n ",您应该使用标准的回车+换行组合:“\r\n”
如果您混合两者(即“\n”而没有前面的“\r”和“\r\n”),这应该在填充单元格值之前修复它:
我本人不使用CellValues。字符串甚至CellValues.InlineString。
相反,我使用 CellValues.SharedString 构建文本单元格(就像 Excel 一样)。
对于 Bool,我使用“CellValues.Boolean”,对于所有其他单元格(数字)和日期)我没有将单元格的DataType设置为任何内容 - 因为这就是 Excel 在查看它创建的标记时所做的事情。
You need to do two things:
1.) Mark the cell as "Wrapped Text". You can do this in the spreadsheet by hand if you are using an existing spreadsheet as your template. Just right-click on the cell(s) and select "Format Cells..", click on the "Alignment" tab and check the "Wrap Text" checkbox.
OR... You can set the CellFormat programmatically. If you have a CellFormat object called "cf ", you would do something like this:
2.) You shouldn't use "\n", instead you should be using the standard carriage-return + line-feed combination: "\r\n"
If you are mixing the two (i.e. "\n" without the preceeding "\r" and "\r\n"), this should fix it before populating the cell value:
I, myself, do not use CellValues.String or even CellValues.InlineString.
Instead I build my text cells using CellValues.SharedString (just like Excel does).
For Bool I use "CellValues.Boolean", and for all others (numerics and dates) I do not set the cell's DataType to anything - because that is what Excel does when I look at the markup it creates.
尝试使用 CellValues.String 而不是 CellValues.InlineString。
Try CellValues.String instead of CellValues.InlineString.
我的解决方案是:在创建
CellFormat
时设置WrapText = true
。My solution was: when creating
CellFormat
setWrapText = true
.