简体   繁体   English

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

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

I'm using the Aspose library to create an Excel document.我正在使用 Aspose 库来创建 Excel 文档。 Somewhere in some cell I need to insert a new line between two parts of the text.在某个单元格的某处,我需要在文本的两个部分之间插入一个新行。

I tried "\\r\\n" but it doesn't work, just displays two square symbols in cell.我试过 "\\r\\n" 但它不起作用,只是在单元格中显示两个方形符号。 I can however press Alt+Enter to create a new line in that same cell.但是,我可以按 Alt+Enter 在同一个单元格中创建一个新行。

How do I insert a new line programmatically?如何以编程方式插入新行?

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

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

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

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 much easier 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.我知道您使用的是 C#,但我发现如果您首先在 VBA 中执行它,那么弄清楚要做什么容易得多,因为您可以“交互式”地尝试它而无需编译任何东西。 Whatever you do in C# is just replicating what you do in VBA so there's rarely any difference.无论您在 C# 中做什么,都只是复制您在 VBA 中所做的,因此几乎没有任何区别。 (Remember that the C# interop stuff is just using the same underlying COM libraries as VBA). (请记住,C# 互操作只是使用与 VBA 相同的底层 COM 库)。

Anyway, the C# for this would be:无论如何,用于此的 C# 将是:

oCell.Value = "A\nB";

Spot the difference :-)指出不同 :-)


EDIT 2 : Aaaargh!编辑 2 :啊啊啊! I just re-read the post and saw that you're using the Aspose library.我刚刚重新阅读了这篇文章,发现您正在使用 Aspose 库。 Sorry, in that case I've no idea.对不起,在那种情况下我不知道。

Internally Excel uses U+000D U+000A (CR+LF, \\r\\n ) for a line break, at least in its XML representation. Excel 在内部使用 U+000D U+000A (CR+LF, \\r\\n ) 作为换行符,至少在其 XML 表示中。 I also couldn't find the value directly in a cell.我也无法直接在单元格中找到该值。 It was migrated to another XML file containing shared strings.它被迁移到另一个包含共享字符串的 XML 文件。 Maybe cells that contain line breaks are handled differently by the file format and your library doesn't know about this.也许文件格式对包含换行符的单元格的处理方式不同,而您的库不知道这一点。

SpreadsheetGear for .NET does 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.请注意“WrapText = true” - 如果没有这个,Excel 将不会包装文本。 I would assume that Aspose has similar APIs.我假设 Aspose 有类似的 API。

Disclaimer: I own SpreadsheetGear LLC免责声明:我拥有 SpreadsheetGear LLC

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;

"\\n" works fine. "\\n" 工作正常。 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.如果输入来自多行文本框,新行字符将是“\\r\\n”,如果将其替换为“\\n”,它将起作用。

You can use Chr(13).您可以使用 Chr(13)。 Then just wrap the whole thing in Chr(34).然后将整个内容包装在 Chr(34) 中。 Chr(34) is double quotes. Chr(34) 是双引号。

  • VB.Net Example: VB.Net 示例:
  • TheContactInfo = "" TheContactInfo = ""
  • TheContactInfo = Trim(TheEmail) & chr(13) TheContactInfo = Trim(TheEmail) & chr(13)
  • TheContactInfo = TheContactInfo & Trim(ThePhone) & chr(13) TheContactInfo = TheContactInfo & Trim(ThePhone) & chr(13)
  • TheContactInfo = Chr(34) & TheContactInfo & Chr(34) TheContactInfo = Chr(34) & TheContactInfo & Chr(34)

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();我可以让“ \\n ”工作的唯一方法是让单元格$format->setTextWrap(); and then using " \\n " would work.然后使用“ \\n ”会起作用。

What worked for me:什么对我有用:

worksheet.Cells[0, 0].Style.WrapText = true;
worksheet.Cells[0, 0].Value = yourStringValue.Replace("\\r\\n", "\r\n");

My issue was that the \\r\\n came escaped.我的问题是 \\r\\n 逃脱了。

E.Run runForBreak = new E.Run();

E.Text textForBreak = new E.Text() { Space = SpaceProcessingModeValues.Preserve };
textForBreak.Text = "\n";
runForBreak.Append(textForBreak);
sharedStringItem.Append(runForBreak);

Actually, it is really simple.其实,这真的很简单。

You may edit an xml version of excel.您可以编辑 xml 版本的 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;稍后您可以在编辑器中打开该文件,然后您会看到一个新行由&#10;表示&#10;

Have a try....试试....

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM