如何让Excel忽略单元格开头的撇号
我正在编写一个将简单数据库与 Excel 工作表同步的工具。数据库表中的每一项对应于工作表中的一行。我使用 C# 和 Excel interop com 接口将 Excel 工作表读入工具中,然后在同步后比较项目的值(即 Excel 工作表中的一列),以确保它们相等。
昨天我发现了一个比较不正确的情况:
"'<MedalTitle>' Medal - <MedalDescription>"
"<MedalTitle>' Medal - <MedalDescription>"
第二个是我从 Excel 中读入的,正如您所看到的,它跳过了第一个撇号。有没有办法告诉 Excel 将单元格视为文本(不,仅设置单元格的格式没有帮助)?
我什至尝试在 VBA 中复制单元格的值( 'hello' ),如下所示:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Target.Offset(1, 0).Value = Target.Worksheet.Range("b2").Value
Target.Offset(2, 0).Value = Target.Worksheet.Range("b2").Formula
Target.Offset(3, 0).Formula = Target.Worksheet.Range("b2").Formula
Target.Offset(4, 0).Formula = Target.Worksheet.Range("b2").Value
End Sub
结果是目标单元格的值始终为 hello'
如果有没办法,我必须做一些丑陋的事情,比如
if (dbitem.value[0] == ''' )
{
// stuff
}
else
{
// regular comparison
}
I'm writing a tool that syncs a simple database with Excel sheets. Each item in a table in the database corresponds to one row in the worksheet. I read the Excel sheet into the tool using C# and the Excel interop com interface, then compared the items' values (i.e. one of the columns in the excel sheet) after the sync just to make sure that they are equal.
Yesterday I found a case where the comparison wasn't true:
"'<MedalTitle>' Medal - <MedalDescription>"
"<MedalTitle>' Medal - <MedalDescription>"
The second is the one I've read in from Excel, and as you can see it's skipped the first apostrophe. Is there a way to tell Excel to treat the cell as just text (no, just setting the cell's formatting doesn't help)?
I even tried to copy the value ( 'hello' ) of a cell in VBA like this:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Target.Offset(1, 0).Value = Target.Worksheet.Range("b2").Value
Target.Offset(2, 0).Value = Target.Worksheet.Range("b2").Formula
Target.Offset(3, 0).Formula = Target.Worksheet.Range("b2").Formula
Target.Offset(4, 0).Formula = Target.Worksheet.Range("b2").Value
End Sub
The result was that the value of target cell is always hello'
If there is no way, I'll have to do something ugly like
if (dbitem.value[0] == ''' )
{
// stuff
}
else
{
// regular comparison
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
恐怕撇号
'
对于 Excel 来说是一个特殊字符,当它作为您找到的单元格中的第一个字符出现时。它告诉 Excel 将字符串的其余部分视为文本,以便您可以在单元格中输入类似'34.2
的内容,它会将其视为字符串而不是数字(用于格式化和很快)。我建议执行与您建议类似的操作,不同之处在于您将其放入 Excel 时,检查第一个字符,并添加一个额外的
'
(如果已有)。或者,您可以在所有值前面加上撇号 - 如果您希望它们全部作为文本。这样您就不需要额外的第一个字符检查。
I'm afraid the apostrophe
'
is a special character for Excel when it appears as the first character in a cell as you've found. It tells Excel to treat the rest of the string as text, so that you can enter something like'34.2
in the cell, and it'll treat it as the string instead of the number (for formatting and so on).I suggest doing something similar to what you've suggested, except that where you're putting it into Excel, check the first character, and add an extra
'
if there's one there already.Alternatively, you could prepend an apostrophe to all values - if you want them all as text that is. That way you don't need the extra first character check.
查看与该单元格对应的
Range
对象的PrefixCharacter
属性来自帮助:
TransitionNavigKeys
部分与 Lotus 1-2-3 兼容性相关,因此它很可能是False
基于文章的答案:
http://excel.tips.net/Pages/T003332_Searching_for_Leading_Apostropes.html
(警告:可能会出现稍微烦人的弹出窗口)
< em>编辑:实际上这可能没有任何用处,因为
PrefixCharacter
是只读的:(edit2:我第一次是对的。
PrefixCharacter仅当添加到单元格的值以 ' 开头时,
才会被填充,因此只需读回PrefixCharacter
加上Value
并连接,只要TransitionNavigKeys
即可。 code> 为False
,即Look at the
PrefixCharacter
property of theRange
object which corresponds to that cellFrom the help:
The
TransitionNavigKeys
part relates to Lotus 1-2-3 compatibility so it's more than likely going to beFalse
Answer based on article at:
http://excel.tips.net/Pages/T003332_Searching_for_Leading_Apostrophes.html
(warning: slightly annoying pop-up may appear)
edit: actually this probably isn't going to be any use because
PrefixCharacter
is read-only :(edit2: I was right the first time.
PrefixCharacter
only gets populated if the value added to the cell started with ' so just read backPrefixCharacter
plusValue
and concatenate. As long asTransitionNavigKeys
isFalse
, that is尝试使用
targetcell.Value
代替。.Formula
是在公式栏中看到的公式,而.Value
是单元格的计算值。所以,我猜测您也会在原始代码中使用
.Formula
。改变这一点应该有效。编辑:好吧,它不起作用(尴尬)。
Excel 特别对待起始单引号。如此特别,甚至模糊的单元格/范围属性也无权访问。我能找到的唯一解决方法基本上与您最初的想法相同。这里是:
try
targetcell.Value
instead..Formula
is the formula seen in the formula bar while.Value
is the evaluated value of the cell.So, I am guessing that you would have used
.Formula
in your original code as well. Changing that should work.EDIT: Ok, it did not work (embarrassed).
Excel treats the starting single quote specially.. so specially that even obscure cell / range properties do not have access. The only workaround I could find is essentially the same as what you thought initially. Here goes:
您可以尝试在查询中的文本字段( '''' + dbField )前添加单引号,以便对于嵌入单引号的字段,您的查询将返回:
当放入 Excel 单元格时将转换为:
对于字符如果不在引号中,您会得到:
当放入 Excel 单元格时,它会转换为:
值得一试。 :-)
You might try pre-pending a single quote to your text fields ( '''' + dbField ) in your query so that for fields with embedded single quotes your query would return:
which when placed in an Excel cell would convert to:
for characters that weren't in quotes you would get:
which when placed in an Excel cell would convert to:
Worth a shot. :-)