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

发布于 2024-07-29 16:07:31 字数 166 浏览 6 评论 0原文

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

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

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

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.

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.

How do I insert a new line programmatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(13

泛滥成性 2024-08-05 16:07:32
E.Run runForBreak = new E.Run();

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

E.Text textForBreak = new E.Text() { Space = SpaceProcessingModeValues.Preserve };
textForBreak.Text = "\n";
runForBreak.Append(textForBreak);
sharedStringItem.Append(runForBreak);
香橙ぽ 2024-08-05 16:07:32

事实上,这真的很简单。

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

尝试一下......

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

Have a try....

前事休说 2024-08-05 16:07:32

你试过“\n”我猜,它应该有效。

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

逆流 2024-08-05 16:07:31

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

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

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

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

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

worksheet.Cells[0, 0].Style.WrapText = true;
执妄 2024-08-05 16:07:31
cell.Text = "your firstline<br style=\"mso-data-placement:same-cell;\">your secondline";

如果您从数据库获取文本,则:

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

If you are getting the text from DB then:

cell.Text = textfromDB.Replace("\n", "<br style=\"mso-data-placement:same-cell;\">");
等数载,海棠开 2024-08-05 16:07:31

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


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

一个

B

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

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

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

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

因为 vbLf 是字符代码 10)。

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

无论如何,C# 的做法是:

oCell.Value = "A\nB";

找出差异 :-)


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

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


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:

A

B

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

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

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

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

(since vbLf is character code 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. 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).

Anyway, the C# for this would be:

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.

放低过去 2024-08-05 16:07:31

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

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.

萌辣 2024-08-05 16:07:31

如果有人对基础设施解决方案感兴趣,就在这里。

  1. 使用

    Environment.NewLine

  2. 确保你的单元被包裹

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

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

  1. Use

    Environment.NewLine

  2. Make sure your cell is wrapped

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

无远思近则忧 2024-08-05 16:07:31

SpreadsheetGear for .NET 是这样实现的:

        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);

注意“WrapText = true” - Excel 不会换行文本没有这个。 我假设 Aspose 有类似的 API。

免责声明:我拥有 SpreadsheetGear LLC

SpreadsheetGear for .NET does it this way:

        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.

Disclaimer: I own SpreadsheetGear LLC

靖瑶 2024-08-05 16:07:31

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

"\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.

悲欢浪云 2024-08-05 16:07:31

使用 PEAR 'Spreadsheet_Excel_Writer' 和 'OLE':

我能让“\n”工作的唯一方法是使单元格 $format->setTextWrap(); 然后使用“\n”就可以了。

Using PEAR 'Spreadsheet_Excel_Writer' and 'OLE':

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

听你说爱我 2024-08-05 16:07:31

您可以使用 Chr(13)。 然后将整个内容包装在 Chr(34) 中。 Chr(34) 是双引号。

  • VB.Net 示例:
  • TheContactInfo = ""
  • TheContactInfo = Trim(TheEmail) & chr(13)
  • 联系人信息 = 联系人信息 & 修剪(ThePhone)和 chr(13)
  • TheContactInfo = Chr(34) & 联系信息& 铬(34)

You can use Chr(13). Then just wrap the whole thing in Chr(34). Chr(34) is double quotes.

  • VB.Net Example:
  • TheContactInfo = ""
  • TheContactInfo = Trim(TheEmail) & chr(13)
  • TheContactInfo = TheContactInfo & Trim(ThePhone) & chr(13)
  • TheContactInfo = Chr(34) & TheContactInfo & Chr(34)
眼中杀气 2024-08-05 16:07:31

对我有用的:

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

我的问题是 \r\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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文