将多行CSV文件导入到Excel中

有一个.csv文件,我们想分发给我们的客户,它包含多行条目(即换行条目)。 根据客户的语言设置,文件可能正确或不正确地导入到Excel中。 通常情况下,我们会build议使用导入文件,但多行条目似乎存在一些错误,所以它们会“分离”成单独的行(好奇地,当文件直接打开时,这不会发生)。

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

导入对多行条目没有帮助。

示例csv文件(2 csv行):

A; B; "some stuff"; C; 1; 2; "another line"; 3; 

正确导入(2行与多行条目):

 AB (some stuff) C 1 2 (another line) 3 

错误的导入(3行):

 A; B; C; "some stuff";D; 1; 2; "another line"; 3; 

有另一种可能性介入 – select一列,然后按数据下的文本到列。 这基于分隔符整齐地分割线,但仍然没有绕过换行符。

是否有可能导入一个csv文件,以便总是可以识别多行条目?

你可能会发现它在openoffice中可以打开。 我做了。

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

你的问题不太清楚,但我认为这是你想要的:注意:出于某种原因,错误捕捉部分没有正确粘贴。 抱歉

 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