【问题标题】:Import multiline csv files into Excel internationally在国际范围内将多行 csv 文件导入 Excel
【发布时间】:2009-01-14 14:44:17
【问题描述】:

我们想要分发给我们的客户的 .csv 文件,它包含多行条目(即带有换行符的条目)。根据客户的语言设置,文件可能会或可能不会正确导入 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 文件,以便始终识别多行条目?

【问题讨论】:

    标签: excel csv


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-27
        • 1970-01-01
        • 1970-01-01
        • 2018-11-25
        • 1970-01-01
        • 1970-01-01
        • 2019-03-10
        • 2015-02-15
        相关资源
        最近更新 更多