如何以编程方式在 C# 中的 Excel 单元格中插入新行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1176548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:05:10  来源:igfitidea点击:

How to insert programmatically a new line in an Excel cell in C#?

c#.netexcelaspose

提问by User

I'm using the Aspose library to create an Excel document. Somewhere in some cell I need to insert a new line between two parts of the text.

我正在使用 Aspose 库来创建 Excel 文档。在某个单元格的某处,我需要在文本的两个部分之间插入一个新行。

I tried "\r\n" but it doesn't work, just displays two square symbols in cell. I can however press Alt+Enter to create a new line in that same cell.

我试过 "\r\n" 但它不起作用,只是在单元格中显示两个方形符号。但是,我可以按 Alt+Enter 在同一个单元格中创建一个新行。

How do I insert a new line programmatically?

如何以编程方式插入新行?

采纳答案by Ahmad Mageed

From the Aspose Cells forums: How to use new line char with in a cell?

来自 Aspose Cells 论坛:如何在单元格中使用换行符?

After you supply text you should set the cell's IsTextWrapped style to true

提供文本后,您应该将单元格的 IsTextWrapped 样式设置为 true

worksheet.Cells[0, 0].Style.WrapText = true;

回答by Gary McGill

You need to insert the character code that Excel uses, which IIRC is 10 (ten).

您需要插入 Excel 使用的字符代码,其中 IIRC 为 10(十)。



EDIT: OK, here's some code. Note that I was able to confirm that the character-code used is indeed 10, by creating a cell containing:

编辑:好的,这是一些代码。请注意,通过创建包含以下内容的单元格,我能够确认使用的字符代码确实是 10:

A

B

一种

...and then selecting it and executing this in the VBA immediate window:

...然后选择它并在 VBA 即时窗口中执行此操作:

?Asc(Mid(Activecell.Value,2,1))

So, the code you need to insert that value into another cell in VBA would be:

因此,您需要将该值插入到 VBA 中的另一个单元格的代码是:

ActiveCell.Value = "A" & vbLf & "B"

(since vbLf is character code 10).

(因为 vbLf 是字符代码 10)。

I know you're using C# but I find it's mucheasier to figure out what to do if you first do it in VBA, since you can try it out "interactively" without having to compile anything. Whatever you do in C# is just replicating what you do in VBA so there's rarely any difference. (Remember that the C# interop stuff is just using the same underlying COM libraries as VBA).

我知道您使用的是 C#,但我发现如果您首先在 VBA 中执行它,那么弄清楚要做什么容易得多,因为您可以“交互式”地尝试它而无需编译任何东西。无论您在 C# 中做什么,都只是复制您在 VBA 中所做的,因此几乎没有任何区别。(请记住,C# 互操作只是使用与 VBA 相同的底层 COM 库)。

Anyway, the C# for this would be:

无论如何,用于此的 C# 将是:

oCell.Value = "A\nB";

Spot the difference :-)

指出不同 :-)



EDIT 2: Aaaargh! I just re-read the post and saw that you're using the Aspose library. Sorry, in that case I've no idea.

编辑 2:啊啊啊!我刚刚重新阅读了这篇文章,发现您正在使用 Aspose 库。对不起,在那种情况下我不知道。

回答by Joey

Internally Excel uses U+000D U+000A (CR+LF, \r\n) for a line break, at least in its XML representation. I also couldn't find the value directly in a cell. It was migrated to another XML file containing shared strings. Maybe cells that contain line breaks are handled differently by the file format and your library doesn't know about this.

Excel 在内部使用 U+000D U+000A (CR+LF, \r\n) 作为换行符,至少在其 XML 表示中是这样。我也无法直接在单元格中找到该值。它被迁移到另一个包含共享字符串的 XML 文件。也许文件格式对包含换行符的单元格的处理方式不同,而您的库不知道这一点。

回答by Joey

Have you tried "\n" I guess, it should work.

你有没有试过 "\n" 我猜,它应该可以工作。

回答by Joe Erickson

SpreadsheetGear for .NETdoes it this way:

.NET 的 SpreadsheetGear 是这样做的:

        IWorkbook workbook = Factory.GetWorkbook();
        IRange a1 = workbook.Worksheets[0].Cells["A1"];
        a1.Value = "Hello\r\nWorld!";
        a1.WrapText = true;
        workbook.SaveAs(@"c:\HelloWorld.xlsx", FileFormat.OpenXMLWorkbook);

Note the "WrapText = true" - Excel will not wrap the text without this. I would assume that Aspose has similar APIs.

请注意“WrapText = true” - 如果没有这个,Excel 将不会包装文本。我假设 Aspose 有类似的 API。

Disclaimer: I own SpreadsheetGear LLC

免责声明:我拥有 SpreadsheetGear LLC

回答by Ahmad Mageed

"\n" works fine. If the input is coming from a multi-line textbox, the new line characters will be "\r\n", if this is replaced with "\n", it will work.

"\n" 工作正常。如果输入来自多行文本框,则新行字符将是“\r\n”,如果将其替换为“\n”,它将起作用。

回答by Kiran

cell.Text = "your firstline<br style=\"mso-data-placement:same-cell;\">your secondline";

If you are getting the text from DB then:

如果您从 DB 获取文本,则:

cell.Text = textfromDB.Replace("\n", "<br style=\"mso-data-placement:same-cell;\">");

回答by Chris

Using PEAR 'Spreadsheet_Excel_Writer' and 'OLE':

使用 PEAR 'Spreadsheet_Excel_Writer' 和 'OLE':

Only way I could get "\n" to work was making the cell $format->setTextWrap();and then using "\n" would work.

我可以让“ \n”工作的唯一方法是制作细胞$format->setTextWrap();,然后使用“ \n”会起作用。

回答by Abdul Muis

Actually, it is really simple.

其实,这真的很简单。

You may edit an xml version of excel. Edit a cell to give it new line between your text, then save it. Later you may open the file in editor, then you will see a new line is represented by &#10;

您可以编辑 xml 版本的 excel。编辑一个单元格以在文本之间添加新行,然后保存它。稍后您可以在编辑器中打开该文件,然后您会看到一个新行由 &#10;

Have a try....

试试....

回答by Cosmin T.

If anyone is interested in the Infragistics solution, here it is.

如果有人对 Infragistics 解决方案感兴趣,请看这里。

  1. Use

    Environment.NewLine

  2. Make sure your cell is wrapped

    dataSheet.Rows[i].Cells[j].CellFormat.WrapText = ExcelDefaultableBoolean.True;

  1. Environment.NewLine

  2. 确保你的细胞被包裹

    dataSheet.Rows[i].Cells[j].CellFormat.WrapText = ExcelDefaultableBoolean.True;