2009-01-14 17 views
5

Existe un archivo .csv que nos gustaría distribuir a nuestros clientes, contiene entradas de varias líneas (es decir, entradas con líneas nuevas). Dependiendo de la configuración de idioma de los clientes, el archivo puede o no importarse correctamente en Excel. Normalmente, sugerimos usar Importar el archivo, pero parece que hay algún error con las entradas de líneas múltiples, por lo que se "separarán" en líneas separadas (curiosamente, esto no ocurre cuando el archivo se abre directamente).Importación de archivos csv de varias líneas a Excel internacionalmente

En algunos idiomas (por ejemplo, inglés), se abre correctamente un csv con comas, pero no un archivo con punto y coma. Con otros idiomas (por ejemplo, alemán), se puede abrir directamente un csv con punto y coma, pero no un archivo con comas.

La importación no ayuda con las entradas de línea múltiple.

(líneas 2 CSV) archivo csv

muestra:

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

importación correcta (2 líneas con una entrada de varias líneas):

A B (some 
stuff) C 
1 2 (another line) 3 

importación incorrecto (3 líneas):

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

Hay otra posibilidad de intervenir: para seleccionar una columna y presionar Texto en Columnas bajo Datos. Esto divide las líneas ordenadamente en base a un separador, pero aún no pasa por alto las nuevas líneas.

¿Es posible importar un archivo csv, para que siempre se reconozcan las entradas de varias líneas?

Respuesta

1

Su pregunta no es del todo clara, pero creo que esto es lo que quiere: Nota: Por algún motivo, la parte de error no se pegó correctamente. Lo siento

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 
2

Probablemente lo encuentres abierto bien en openoffice. Yo si.

En cuyo caso, puede guardarlo como una hoja de Excel y distribuir ambos archivos a sus clientes.

Cuestiones relacionadas