将多行csv文件导入Excel

将多行csv文件导入Excel,excel,csv,Excel,Csv,我们希望向客户分发一个.csv文件,其中包含多行条目,即带有换行符的条目。根据客户的语言设置,文件可能会正确导入Excel,也可能不会正确导入Excel。通常情况下,我们建议使用导入文件,但多行条目似乎存在一些缺陷,因此它们会“分开”成单独的行。奇怪的是,直接打开文件时不会发生这种情况 对于某些语言(如英语),可以正确打开带有逗号的csv,但不能打开带有分号的文件。 对于其他语言(如德语),可以直接打开带有分号的csv,但不能打开带有逗号的文件 导入对多行条目没有帮助 示例csv文件2 csv行

我们希望向客户分发一个.csv文件,其中包含多行条目,即带有换行符的条目。根据客户的语言设置,文件可能会正确导入Excel,也可能不会正确导入Excel。通常情况下,我们建议使用导入文件,但多行条目似乎存在一些缺陷,因此它们会“分开”成单独的行。奇怪的是,直接打开文件时不会发生这种情况

对于某些语言(如英语),可以正确打开带有逗号的csv,但不能打开带有分号的文件。 对于其他语言(如德语),可以直接打开带有分号的csv,但不能打开带有逗号的文件

导入对多行条目没有帮助

示例csv文件2 csv行:

A; B; "some
stuff"; C;
1; 2; "another line"; 3;
正确导入具有多行条目的2行:

A B (some
stuff) C
1 2 (another line) 3
错误导入3行:

A; B; C; "some
stuff";D;
1; 2; "another line"; 3;
还有另一种干预的可能性——选择一列,然后按文本键将数据下的列删除。这会根据分隔符整齐地分割行,但仍然无法绕过换行符


是否可以导入csv文件,以便始终识别多行条目?

您的问题不太清楚,但我认为这正是您想要的: 注意:由于某些原因,错误捕捉部分没有正确粘贴。对不起

Public Sub ReadCSV()
'' Assumes all records:
''   Have 5 fields
''   The fields are delimited by a ;
''   The Last field ends in a ;

Dim FileName As String
Dim ExcelRow As Long
Dim WorkSheetName As String
Dim FieldValue(5) As String
Dim FieldNo As Integer
Dim StringPosition As Integer
Dim LineFromFile As String
Dim CharFromLine As String

WorkSheetName = "Sheet1"
ExcelRow = 2  '' Assumes Row 1 is a heading that you should not delete

''   Get the FileName and Open the file
If Application.FileDialog(msoFileDialogOpen).Show <> -1 Then Exit Sub
FileName = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)

On Error GoTo OpenError
Open FileName For Input As #1

''   Clear old data from the workbook
sRange = WorkSheetName + "!A2:E65536"
Range(sRange).ClearContents '' Preserves formatting

''   Read The file one line at a time
On Error GoTo ReadError
While Not EOF(1)
    Line Input #1, LineFromFile
    Debug.Print LineFromFile
    FieldNo = 1
    While FieldNo <= 5  '' 5 is the number of fields in a record
        DoEvents
        FieldValue(FieldNo) = ""
        StringPosition = 1
        ''   Examine each character from the line
        While StringPosition <= Len(LineFromFile)
            CharFromLine = Mid(LineFromFile, StringPosition, 1)
            If CharFromLine = ";" Then '' ";" is the delimitor in the delimited file
                FieldNo = FieldNo + 1
                If FieldNo < 6 Then FieldValue(FieldNo) = ""
            Else
                FieldValue(FieldNo) = FieldValue(FieldNo) + CharFromLine
            End If
            ''   Test to see if we're at the end of a line, but haven't got all the fields yet
            If StringPosition = Len(LineFromFile) And FieldNo < 6 Then
                FieldValue(FieldNo) = FieldValue(FieldNo) + Chr(10) ''   Add a LineFeed to represent the new line
                Line Input #1, LineFromFile
                StringPosition = 0
            End If
            StringPosition = StringPosition + 1
        Wend
    Wend
    ''   Put the Data in the Workbook
    For FieldNo = 1 To 5
        Sheets(WorkSheetName).Cells(ExcelRow, FieldNo).Value = FieldValue(FieldNo)
    Next FieldNo
    ExcelRow = ExcelRow + 1
Wend
Exit Sub
OpenError:
    Call MsgBox("Error Opening File:" + FileName, vbCritical, "File Open Error")
    Exit Sub
ReadError:
    Call MsgBox("Error Reading File:" + FileName, vbCritical, "File Read Error")
End Sub

您可能会发现它在openoffice中可以很好地打开。是的

在这种情况下,您可以将其另存为Excel工作表,并将这两个文件分发给您的客户机