简体   繁体   English

通过OleDb c在Excel中插入回车符#

[英]Insert carriage returns in Excel via OleDb c#

I am inserting text into a Memo field in a Excel cell via a insert statement using OleDb command object. 我通过使用OleDb命令对象的插入语句将文本插入Excel单元格中的备注字段。

When I try to insert a carriage return, char 10, what is displayed is a black square (MS Sans Serif). 当我尝试插入回车符号10时,显示的是黑色方块(MS Sans Serif)。 When I look at the top edit cell (don't know the offical name) the text is formatted correctly including carriage returns. 当我查看顶部编辑单元格(不知道官方名称)时,文本格式正确,包括回车。

I am trying to duplicate what happens when a user presses Alt + Enter in a cell. 我试图复制用户在单元格中按Alt + Enter时发生的情况。

I have tried \\n , \\r , \\r\\n and char.ConvertFromUtf32(10) . 我试过\\n\\r\\r\\nchar.ConvertFromUtf32(10)

Nothing seems to change the text of the cell. 似乎没有什么能改变细胞的文本。

I'm assuming you meant \\n and \\r\\n? 我假设你的意思是\\ n和\\ r \\ n?

Also have you tried Environment.Newline? 还试过Environment.Newline吗?

Try expanding the height of the row in excel. 尝试在excel中扩展行的高度。 I was beating my head against the wall over this as well, and finally got it to work using \\n. 我也在头上撞到墙上,最后用\\ n开始工作。 But then one day it stopped working for no reason other than the cell was too smal to display all lines of data. 但是有一天,除了电池太小而无法显示所有数据线之外,它还没有任何理由停止工作。

Got it to work as well via: 通过以下方式工作:

  1. Insert the newline character \\n inside the string 在字符串中插入换行符\\n
  2. Set the column to autofit. 将列设置为自动调整。 The EntireColumn property can also be used to set the width of the column or to force the column to Autofit. EntireColumn属性还可用于设置列的宽度或强制列为自动调整。

public void AutoFitColumn(Worksheet ws, int col) {
    ((Range)ws.Cells[1, col]).EntireColumn.AutoFit();
}

See this link if more info desired. 如果需要更多信息,请参阅此链接

I had the same same blank squares hou had. 我有同样的空白方块。 I managed to make it work by inserting a \\n character and by activating the option "Wrap text" in the format of the Excel cell. 我设法通过插入\\ n字符并激活Excel单元格格式的“自动换行”选项来使其工作。

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

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