为什么Excel会将逗号分隔为逗号? [英] Why does Excel treat double spaces as a comma?
问题描述
我在vb.net应用程序中将导出导出到CSV文件,然后将其导出到Outlook。
我遇到的问题是,正在写入CSV文件,我的代码正在检查当前字段中的逗号,但在执行此操作时,它也会错误一个双空格逗号或空格后按Enter键(对于多行文本框) / p>
例如,如果在客户的notes部分,有4行文本,一个结束在一个空格 - 用户然后按enter键去下一行,然而程序正在下一行文本并创建一个新的记录,因为它认为它是一个逗号...
原因是什么为了这?这意味着数据必须在被导出之前被超级验证(即检查没有双倍空间等),这是非常耗时的。
希望这有意义
这是代码:
Dim result As Boolean = True
Try
Dim sb As New StringBuilder()
Dim separator As String =,
Dim group As String =
Dim newLine As String = Environment.NewLine
对于每个列作为DataColumn In dtable.Columns
sb.Append(wrapValue(column.ColumnName,group,separator)& separator)
Next
sb.Append(newLine)
对于每行As DataRow In dtable.Rows
对于每个col作为DataColumn In dtable.Columns
sb.Append(wrapValue(row(col).ToString(),group,separator)& separator)
Next
sb.Append(newLine)
Next
wrapValue的代码
函数wrapValue(value As String,group As String,separator As String)As String
如果value.Contains(separator)then
如果value.Contains组)然后
value = value.Replace(group,group + group)
结束如果
value = group&价值&组
结束如果
返回值
结束函数
基于它缩短了430行的事实,我建议这是一个事实,你添加一个负载之前和之后的值变量。
如果它在开始删除一个值,那么它将删除一个第一列标题之前。导入一个记录,如你在评论中提到的,我不完全确定,但是,我建议问题在于你的 wrapValue
代码。
value = group& value& group
到
= value
看看是否有改变?
I wrote an export to CSV file in my vb.net application, and I then exported it into Outlook.
The issue I've got, is that when the CSV file is being written, my code is checking for a comma in the current field, but while doing this, it also mistakes a double space for a comma, or space followed by 'Enter' key being pressed (for multiline textboxes)
An example would be if in the notes section of the customer, there is 4 lines of text, and one ends in a space - The user has then pressed enter to go to the next line, however the program is taking the next line of text and creating a new record for it, as it thinks it's a comma...
What is the reason for this? This means that data has to be super validated (ie checking for no double spaces etc) before it can be exported, which is far too time consuming.
Hopefully this makes sense!
This is the code:
Dim result As Boolean = True
Try
Dim sb As New StringBuilder()
Dim separator As String = ","
Dim group As String = """"
Dim newLine As String = Environment.NewLine
For Each column As DataColumn In dtable.Columns
sb.Append(wrapValue(column.ColumnName, group, separator) & separator)
Next
sb.Append(newLine)
For Each row As DataRow In dtable.Rows
For Each col As DataColumn In dtable.Columns
sb.Append(wrapValue(row(col).ToString(), group, separator) & separator)
Next
sb.Append(newLine)
Next
The code for wrapValue
Function wrapValue(value As String, group As String, separator As String) As String
If value.Contains(separator) Then
If value.Contains(group) Then
value = value.Replace(group, group + group)
End If
value = group & value & group
End If
Return value
End Function
Based on the fact that it's shortening it by 430 lines, I'd suggest it's something to do with the fact you're adding a load of "" before and after the value variable.
If it's removing a value at the start, then it will be removing a " before the first column header. As to why it's importing one record as you mentioned in the comments, I'm not entirely sure, however, I would suggest the issue lies in your wrapValue
code.
Can you try changing
value = group & value & group
to
value = value
and see if that changes anything?
这篇关于为什么Excel会将逗号分隔为逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!