CÓMO EXPORTAR UNA HOJA DE EXCEL A FORMATO CSV ENTRECOMILLADO
Excel exporta a formato de texto o csv con el separador de campos definido por defecto en el pc. Pero Excel no dispone de una opción de formateo de salida de los campos para que, por ejemplo, se exporten los campos entrecomillados.
Para conseguir la exportación de una hoja en formato csv estándar, es decir, todos los campos entrecomillados con comillas dobles (“) y separados con punto y coma (;) indicamos un procedimiento empleando una macro de excel.
CREAR LA MACRO EN EXCEL
Desarrollador – Visual basic – Insertar – Módulo – <pegar macro> – Guardar
EJECUTAR LA MACRO EN EXCEL
- En la hoja se marca el bloque a exportar
- Acceder a Vista – Macros – Seleccionar la macro QuoteCommaExport
- Teclear la unidad, carpeta y fichero csv de exportación
CÓDIGO FUENTE DE LA MACRO QuoteCommaExport
Sub QuoteCommaExport()
‘ Dimension all variables.
Dim DestFile As String
Dim FileNum As Integer
Dim ColumnCount As Integer
Dim RowCount As Integer
‘ Prompt user for destination file name.
DestFile = InputBox(«Enter the destination filename» _
& Chr(10) & «(with complete path):», «Quote-Comma Exporter»)
‘ Obtain next free file handle number.
FileNum = FreeFile()
‘ Turn error checking off.
On Error Resume Next
‘ Attempt to open destination file for output.
Open DestFile For Output As #FileNum
‘ If an error occurs report it and end.
If Err <> 0 Then
MsgBox «Cannot open filename » & DestFile
End
End If
‘ Turn error checking on.
On Error GoTo 0
‘ Loop for each row in selection.
For RowCount = 1 To Selection.Rows.Count
‘ Loop for each column in selection.
For ColumnCount = 1 To Selection.Columns.Count
‘ Write current cell’s text to file with quotation marks.
Print #FileNum, «»»» & Selection.Cells(RowCount, _
ColumnCount).Text & «»»»;
‘ Check if cell is in last column.
If ColumnCount = Selection.Columns.Count Then
‘ If so, then write a blank line.
Print #FileNum,
Else
‘ Otherwise, write a comma.
Print #FileNum, «;»;
End If
‘ Start next iteration of ColumnCount loop.
Next ColumnCount
‘ Start next iteration of RowCount loop.
Next RowCount
‘ Close destination file.
Close #FileNum
End Sub
OBSERVACIONES
Para dejar la macro permanente y disponible para todas las hojas abiertas consulta la documentación de tu versión de excel, al objeto de crear una plantilla de macro reutilizable. De lo contrario, la macro se almacenará solo en la hoja activa.
FUENTE
Microsoft: https://support.microsoft.com/en-us/kb/291296