如何让Excel忽略单元格开头的撇号

发布于 2024-08-02 17:57:00 字数 1020 浏览 10 评论 0原文

我正在编写一个将简单数据库与 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 技术交流群。

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

发布评论

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

评论(4

绿光 2024-08-09 17:57:00

恐怕撇号 ' 对于 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.

夏至、离别 2024-08-09 17:57:00

查看与该单元格对应的 Range 对象的 PrefixCharacter 属性

来自帮助:

如果 TransitionNavigKeys 属性是
False,此前缀字符将为 '
用于文本标签,或空白。如果
TransitionNavigKeys 属性为 True
这个字符将是 ' 对于 a
左对齐标签,“对于
右对齐标签,^ 表示 a
居中标签,\ 表示重复
标签或空白。

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 the Range object which corresponds to that cell

From the help:

If the TransitionNavigKeys property is
False, this prefix character will be '
for a text label, or blank. If the
TransitionNavigKeys property is True,
this character will be ' for a
left-justified label, " for a
right-justified label, ^ for a
centered label, \ for a repeated
label, or blank.

The TransitionNavigKeys part relates to Lotus 1-2-3 compatibility so it's more than likely going to be False

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 back PrefixCharacter plus Value and concatenate. As long as TransitionNavigKeys is False, that is

赠我空喜 2024-08-09 17:57:00

尝试使用 targetcell.Value 代替。 .Formula 是在公式栏中看到的公式,而 .Value 是单元格的计算值。

所以,我猜测您也会在原始代码中使用 .Formula 。改变这一点应该有效。

编辑:好吧,它不起作用(尴尬)。

Excel 特别对待起始单引号。如此特别,甚至模糊的单元格/范围属性也无权访问。我能找到的唯一解决方法基本上与您最初的想法相同。这里是:

If VarType(cell) = 8 And Not cell.HasFormula Then
 GetFormulaI = "'" & cell.Formula
Else
 GetFormulaI = cell.Formula
End If

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:

If VarType(cell) = 8 And Not cell.HasFormula Then
 GetFormulaI = "'" & cell.Formula
Else
 GetFormulaI = cell.Formula
End If
虫児飞 2024-08-09 17:57:00

您可以尝试在查询中的文本字段( '''' + dbField )前添加单引号,以便对于嵌入单引号的字段,您的查询将返回:

"''stuff in single quotes'"

当放入 Excel 单元格时将转换为:

"'stuff in single quotes'"

对于字符如果不在引号中,您会得到:

"'stuff that wasn't in quotes"

当放入 Excel 单元格时,它会转换为:

"stuff that wasn't in quotes"

值得一试。 :-)

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:

"''stuff in single quotes'"

which when placed in an Excel cell would convert to:

"'stuff in single quotes'"

for characters that weren't in quotes you would get:

"'stuff that wasn't in quotes"

which when placed in an Excel cell would convert to:

"stuff that wasn't in quotes"

Worth a shot. :-)

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