在 vb6 中将 adodb 记录集导出到 CSV 时,如何从 adodb 记录集中删除特殊字符(精确地双引号)?
我的要求是在使用以下代码导出到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要转义双引号,这是通过将它们加倍来完成的。
You need to escape the double quotes, which is done by doubling them up.
您可以使用默认值通过使用字符作为分隔符来导入带双引号的数据
Change:
To:
And
To:
并且您可能必须将删除最后字符的最后一行更改为
EDIT -如果您确实认为必须从数据中删除双引号,则可以首先使用不同的分隔符来实现 - 说波形图“~”,然后在一步中替换双引号,并在下一步中替换波形图
如:
最后一行将是
当然,请记住在列标题循环中使用“~”
You could just use the defaults to import the data with double quotes by using the character as the delimiter
Change:
To:
And
To:
And you might have to change the last line that removes those last characters to
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:
Last lines would then be
And of course remember to use the "~" in your column header loop