C# 如何在OPENXML电子表格单元格中插入换行符?
我目前正在使用类似的方法在单元格中插入内联字符串:C# 如何在OPENXML电子表格单元格中插入换行符?,c#,cell,openxml,spreadsheet,line-breaks,C#,Cell,Openxml,Spreadsheet,Line Breaks,我目前正在使用类似的方法在单元格中插入内联字符串: new Cell() { CellReference = "E2", StyleIndex = (UInt32Value)4U, DataType = CellValues.InlineString, InlineString = new InlineString(new Text( "some text")) } 但是\n插入换行符不起作用,我该怎么做 反应 new Cell(
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。)将单元格标记为“包装文本”。如果使用现有电子表格作为模板,则可以手动在电子表格中执行此操作。只需右键单击单元格并选择“设置单元格格式…”,单击“对齐”选项卡并选中“换行文本”复选框。
或…您可以通过编程方式设置单元格格式。如果您有一个名为“cf”的CellFormat对象,您可以执行以下操作:
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”,对于所有其他单元格(数字和日期)我没有将单元格的数据类型设置为任何类型,因为当我查看Excel创建的标记时,Excel就是这样做的。如何将
CellFormat
对象或Alignment
对象应用于给定的单元格?这不适用于我。我刚刚在与的行中遇到一个错误,\r\n excel将不会打开该工作表,其中的文本表示该工作表已损坏