如何在 OPENXML 电子表格单元格中插入换行符?

发布于 2024-07-16 04:36:54 字数 519 浏览 13 评论 0原文

我目前正在使用类似的方法在单元格中插入内联字符串:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有心的人 2024-07-23 04:36:54

您需要执行两件事:

1.) 将单元格标记为“换行文本”。 如果您使用现有电子表格作为模板,则可以在电子表格中手动执行此操作。 只需右键单击单元格并选择“设置单元格格式..”,单击“对齐”选项卡并选中“自动换行< /em>”复选框。
或...您可以通过编程方式设置单元格格式。 如果您有一个名为“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。字符串甚至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:

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.) 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:

sHeaderText = sHeaderText.Replace("\r\n", "\n").Replace("\n", "\r\n");

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.

柠北森屋 2024-07-23 04:36:54

尝试使用 CellValues.String 而不是 CellValues.InlineString。

Try CellValues.String instead of CellValues.InlineString.

静谧 2024-07-23 04:36:54

我的解决方案是:在创建CellFormat时设置WrapText = true

new Alignment
{
    Horizontal = HorizontalAlignmentValues.Left,
    Vertical = VerticalAlignmentValues.Center,
    WrapText = true, //This setting
};

My solution was: when creating CellFormat set WrapText = true.

new Alignment
{
    Horizontal = HorizontalAlignmentValues.Left,
    Vertical = VerticalAlignmentValues.Center,
    WrapText = true, //This setting
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文