如何插入OPENXML电子表格单元格内换行?
-
06-09-2019 - |
题
我目前使用这样的插入细胞中的内联字符串:
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
}
解决方案
尝试CellValues.String代替CellValues.InlineString。
其他提示
您需要做两件事情:
<强> 1)。强>标记的单元为 “换行文本”。你可以,如果你使用的是现有电子表格模板中手工电子表格做到这一点。在单元格只需右击并选择“的设置单元格格式.. 的”,点击“对齐的”标签,勾选“自动换行”复选框。点击或... 您可以通过编程设置CellFormat。如果你有一个叫做对象CellFormat “ CF 的”,你会做这样的事情:
cf.ApplyAlignment = true;//Set this so that Excel knows to use it.
if (cf.Alignment == null)//If no pre-existing Alignment, then add it.
cf.Alignment = new Alignment() { WrapText = true };
Alignment a = cf.Alignment;
if (a.WrapText == null || a.WrapText.Value == false)
a.WrapText = new BooleanValue(true);//Update pre-existing Alignment.
<强> 2)您不应该使用 “\ n” 个,相反,您应使用标准回车+换行组合: “\ r \ n” 个搜索 如果要混合的两个(即“\ n”个未经前述“\ r”和“\ r \ n”个),这应该填充单元值之前将其修复:
sHeaderText = sHeaderText.Replace("\r\n", "\n").Replace("\n", "\r\n");
我自己,不使用的 CellValues.String 的甚至的 CellValues.InlineString 的。结果 相反,我使用的 CellValues.SharedString 的(就像Excel中一样)。结果对于bool我用 “的 CellValues.Boolean 的”,而对于所有其他(NUMERICS建立我的文字细胞和日期)我不设置单元格的数据类型的任何东西 - 因为这是Excel中做什么,当我看着它创建的标记
不隶属于 StackOverflow