在国际上将多行csv文件导入Excel

时间:2009-01-14 14:44:17

标签: excel csv

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

对于某些语言(例如英语),正确打开带逗号的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文件,以便始终识别多行条目?

2 个答案:

答案 0 :(得分:2)

您可能会发现它在openoffice中打开正常。我做到了。

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

答案 1 :(得分:1)

您的问题不太清楚,但我认为这就是您想要的: 注意:由于某种原因,错误捕获部分未正确粘贴。遗憾

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