如何在 C# 中以编程方式在 Excel 单元格中插入新行?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
事实上,这真的很简单。
您可以编辑 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....
你试过“\n”我猜,它应该有效。
Have you tried "\n" I guess, it should work.
来自 Aspose Cells 论坛: 如何在单元格中使用换行符?
提供文本后,应将单元格的 IsTextWrapped 样式设置为 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
如果您从数据库获取文本,则:
If you are getting the text from DB then:
您需要插入 Excel 使用的字符代码,IIRC 为 10(十)。
编辑:好的,这是一些代码。 请注意,通过创建一个包含以下内容的单元格,我能够确认所使用的字符代码确实是 10:
...然后选择它并在 VBA 立即窗口中执行此操作:
因此,您需要将该值插入到 VBA 中另一个单元格的代码为:(
因为 vbLf 是字符代码 10)。
我知道您正在使用 C#,但我发现如果您首先在 VBA 中执行此操作,那么更容易弄清楚该怎么做,因为您可以“交互式”尝试它,而无需编译任何内容。 无论您在 C# 中做什么,都只是复制您在 VBA 中所做的事情,因此几乎没有任何区别。 (请记住,C# 互操作内容只是使用与 VBA 相同的底层 COM 库)。
无论如何,C# 的做法是:
找出差异 :-)
编辑 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:
...and then selecting it and executing this in the VBA immediate window:
So, the code you need to insert that value into another cell in VBA would be:
(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:
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.
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.如果有人对基础设施解决方案感兴趣,就在这里。
使用
Environment.NewLine
确保你的单元被包裹
dataSheet.Rows[i].Cells[j].CellFormat.WrapText = ExcelDefaultableBoolean.True;
If anyone is interested in the Infragistics solution, here it is.
Use
Environment.NewLine
Make sure your cell is wrapped
dataSheet.Rows[i].Cells[j].CellFormat.WrapText = ExcelDefaultableBoolean.True;
SpreadsheetGear for .NET 是这样实现的:
注意“WrapText = true” - Excel 不会换行文本没有这个。 我假设 Aspose 有类似的 API。
免责声明:我拥有 SpreadsheetGear LLC
SpreadsheetGear for .NET does it this way:
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
“\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.
使用 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.您可以使用 Chr(13)。 然后将整个内容包装在 Chr(34) 中。 Chr(34) 是双引号。
You can use Chr(13). Then just wrap the whole thing in Chr(34). Chr(34) is double quotes.
对我有用的:
我的问题是 \r\n 被转义了。
What worked for me:
My issue was that the \r\n came escaped.