在 vb6 中将 adodb 记录集导出到 CSV 时,如何从 adodb 记录集中删除特殊字符(精确地双引号)?

发布于 2025-01-09 21:27:03 字数 1366 浏览 1 评论 0原文

我的要求是在使用以下代码导出到 VB6 中的 CSV 时,从 adodb 记录集中删除特殊字符,尤其是双引号。

如果存在任何双引号,则双引号后的值将移至下一列。

具体来说,双引号出现在 rsData.Fields(K).Value 中,而不是出现在 rsData.Fields(K).Name 中。我不确定如何删除 rsData.Fields(K).Value 中的双引号。

任何解决此问题的帮助将不胜感激。提前致谢。

'Converting the recordset 

Public Function RecordsetToCSV(rsData As ADODB.Recordset, Optional ShowColumnNames As Boolean = True, Optional NULLStr As String = "") As String
    Dim K As Long, RetStr As String
    
    If ShowColumnNames Then
        For K = 0 To rsData.Fields.Count - 1
            RetStr = RetStr & ",""" & rsData.Fields(K).Name & """"
        Next K
        
        RetStr = Mid(RetStr, 2) & vbNewLine
    End If
    
    RetStr = RetStr & """" & rsData.GetString(adClipString, -1, """,""", """" & vbNewLine & """", NULLStr)
    RetStr = Left(RetStr, Len(RetStr) - 3)
    
    RecordsetToCSV = RetStr
End Function 

'Creating CSV file
    Dim CSVData As String
    CSVData = RecordsetToCSV(rsData, True)

    Open "C:\test.csv" For Binary Access Write As #1
        Put #1, , CSVData
    Close #1

参考: https://www.vbforums.com/showthread.php?481705-VB6-Save-Recordset-to-CSV-format

My requirement is to remove the special characters especially double quotes from the adodb recordset while exporting to CSV in VB6 using the below code.

if any double quotes is present the value after double quotes is moved to next column.

Specifically, the double quotes are present in rsData.Fields(K).Value not in rsData.Fields(K).Name. I'm not sure how to remove the double quotes in rsData.Fields(K).Value.

Any help to fix this issue will be greatly appreciated. Thanks in advance.

'Converting the recordset 

Public Function RecordsetToCSV(rsData As ADODB.Recordset, Optional ShowColumnNames As Boolean = True, Optional NULLStr As String = "") As String
    Dim K As Long, RetStr As String
    
    If ShowColumnNames Then
        For K = 0 To rsData.Fields.Count - 1
            RetStr = RetStr & ",""" & rsData.Fields(K).Name & """"
        Next K
        
        RetStr = Mid(RetStr, 2) & vbNewLine
    End If
    
    RetStr = RetStr & """" & rsData.GetString(adClipString, -1, """,""", """" & vbNewLine & """", NULLStr)
    RetStr = Left(RetStr, Len(RetStr) - 3)
    
    RecordsetToCSV = RetStr
End Function 

'Creating CSV file
    Dim CSVData As String
    CSVData = RecordsetToCSV(rsData, True)

    Open "C:\test.csv" For Binary Access Write As #1
        Put #1, , CSVData
    Close #1

reference : https://www.vbforums.com/showthread.php?481705-VB6-Save-Recordset-to-CSV-format

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

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

发布评论

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

评论(2

蔚蓝源自深海 2025-01-16 21:27:03

您需要转义双引号,这是通过将它们加倍来完成的。

RetStr = RetStr & ",""" & Replace(rsData.Fields(K).Name, """", """""") & """"

You need to escape the double quotes, which is done by doubling them up.

RetStr = RetStr & ",""" & Replace(rsData.Fields(K).Name, """", """""") & """"
相权↑美人 2025-01-16 21:27:03

您可以使用默认值通过使用字符作为分隔符来导入带双引号的数据

Change:

RetStr = RetStr & ",""" & rsData.Fields(K).Name & """"

To:

RetStr = RetStr & "," & vbTab & rsData.Fields(K).Name & vbTab

And

RetStr = RetStr & """" & rsData.GetString(adClipString, -1, """,""", """" & vbNewLine & """", NULLStr)

To:

RetStr = RetStr & rsData.GetString(adClipString)  

并且您可能必须将删除最后字符的最后一行更改为

RetStr = Left(RetStr, Len(RetStr) - 2)

EDIT -如果您确实认为必须从数据中删除双引号,则可以首先使用不同的分隔符来实现 - 说波形图“~”,然后在一步中替换双引号,并在下一步中替换波形图

如:

RetStr = RetStr & "~" & rsData.GetString(adClipString, -1, "~")

最后一行将是

RetStr = Replace(RetStr,"""","")    
RecordsetToCSV = Replace(RetStr,"~","""")

当然,请记住在列标题循环中使用“~”

RetStr = RetStr & ",~" & rsData.Fields(K).Name & "~"

You could just use the defaults to import the data with double quotes by using the character as the delimiter

Change:

RetStr = RetStr & ",""" & rsData.Fields(K).Name & """"

To:

RetStr = RetStr & "," & vbTab & rsData.Fields(K).Name & vbTab

And

RetStr = RetStr & """" & rsData.GetString(adClipString, -1, """,""", """" & vbNewLine & """", NULLStr)

To:

RetStr = RetStr & rsData.GetString(adClipString)  

And you might have to change the last line that removes those last characters to

RetStr = Left(RetStr, Len(RetStr) - 2)

EDIT - If you really truly feel the double quotes must be removed from your data, you can do it by using a different delimiter initially - say the squiggle "~" and then replace double quotes in one step, and the squiggle in next step

As in:

RetStr = RetStr & "~" & rsData.GetString(adClipString, -1, "~")

Last lines would then be

RetStr = Replace(RetStr,"""","")    
RecordsetToCSV = Replace(RetStr,"~","""")

And of course remember to use the "~" in your column header loop

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