Retroceder   Foros del Web > Programación para sitios web > .NET > win forms

Respuesta
 
Herramientas Desplegado
Antiguo 10-dic-2007, 04:50   #1 (permalink)
Expinete ha deshabilitado el karma
 
Avatar de Expinete
 
Fecha de Ingreso: abril-2006
Mensajes: 181
exportar csv a excel

Hola a todos, alguien sabe alguna manera de exportar automáticamente un fichero con formato csv a formato excel?

Gracias por adelantado.
Expinete está desconectado   Responder Citando
Antiguo 12-dic-2007, 09:19   #2 (permalink)
Cnunezm ha deshabilitado el karma
 
Fecha de Ingreso: marzo-2007
Mensajes: 16
Re: exportar csv a excel

mmm, ambos son ficheros generados por excel, solo q con diferente extension, si lo q quieres es q aparezca en excel, cambiale la extension, .csv a .xls

Saludos
Cnunezm está desconectado   Responder Citando
Antiguo 12-dic-2007, 13:48   #3 (permalink)
Analyzer está en el buen camino
 
Fecha de Ingreso: febrero-2005
Mensajes: 21
Re: exportar csv a excel

Código:
Private Sub Command1_Click()

Dim CVS As String
Dim XLS As String

With CommonDialog1
     'Para Abrir el CSV
     .Filter = "Archivo CSV|*.csv|Todos los archivos|*.*"
     .DialogTitle = " Seleccionar el archivo CSV"
     .ShowOpen
     CVS = .FileName
     'Para guardar el XLS
     .Filter = "Archivo Xls|*.xls"
     .FileName = ""
     .DialogTitle = " Escriba l nombre del archivo Xls "
     
     .ShowSave
     XLS = .FileName
End With

If CVS = "" Or XLS = "" Then
   Exit Sub
Else
   Call Exportar_CSV_XLS(CVS, XLS)
End If
End Sub

Private Sub Exportar_CSV_XLS(path_Cvs As String, path_Xls As String)

On Error GoTo ErrSub

Dim obj_Excel As Object
Dim Fila As Integer, Columna As Integer
Dim Contenido As String, Lineas As Variant
Dim datos As Variant, MC As Integer


    'Lee el contenido del CSV y lo almacena en la variable
    Open path_Cvs For Input As #1
    Contenido = Input$(LOF(1), #1)
    Close
    
    'Nuevo objeto Excel
    Set obj_Excel = CreateObject("Excel.Application")
    
    With obj_Excel
    'Agrega un libro
    .Workbooks.Add
    
    ' Obtiene el número de líneas del Csv con la función split
    Lineas = Split(Contenido, vbCrLf)
    
    For Fila = 0 To UBound(Lineas)
        
        'Separa los datos de la linea
        datos = Split(Lineas(Fila), ",")
        
        'Recorre los datos de esta fila que corresponden a cada campo
        For Columna = 0 To UBound(datos)
            ' Agrega el dato a la celda de la hoja activa
            .ActiveSheet.Cells(Fila + 1, Columna + 1) = datos(Columna)
        Next
        If MC < Columna Then
           MC = Columna
        End If
    Next
    
    'Selecciona toda la hoja
    .ActiveSheet.UsedRange.Select
    'Autoajusta las columnas
    .Selection.Columns.AutoFit
    
    
    'Selecciona el encabezado
    .ActiveSheet.Range(.ActiveSheet.Cells(1, 1), .ActiveSheet.Cells(1, MC)).Select
        
    End With
        
    ' Aplica atributos a la fuente a la selección anterior ( los encabezados )
    With obj_Excel.Selection.Font
        .Name = "Verdana"
        .FontStyle = "Bold"
        .Size = 14
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Underline = xlUnderlineStyleNone
    End With

    ' Guarda el documento Xls
    obj_Excel.ActiveWorkbook.SaveAs _
        FileName:=path_Xls, _
        FileFormat:=xlNormal, _
        Password:="", _
        WriteResPassword:="", _
        ReadOnlyRecommended:=False, _
        CreateBackup:=False

    obj_Excel.ActiveWorkbook.Close False

    'Cierra el archivo y elimina la variable
    obj_Excel.Quit
    Set obj_Excel = Nothing
    
    'Fin
    MsgBox "Archivo Xls guardado ", vbInformation
    
    Exit Sub
    
'Error
ErrSub:
    MsgBox Err.Description
    On Error Resume Next
    If Not obj_Excel Is Nothing Then
       obj_Excel.Quit
      Set obj_Excel = Nothing
    End If
End Sub
Analyzer esta en línea ahora   Responder Citando
Antiguo 04-abr-2008, 11:10   #4 (permalink)
Manhy no se puede cailificar en este momento
 
Fecha de Ingreso: diciembre-2007
Ubicación: En Lima - Perú
Mensajes: 55
Enviar un mensaje por MSN a Manhy Enviar un mensaje por Yahoo  a Manhy
Exclamación Re: exportar csv a excel

Cita:
Iniciado por Analyzer Ver Mensaje
Código:
Private Sub Command1_Click()

Dim CVS As String
Dim XLS As String

With CommonDialog1
     'Para Abrir el CSV
     .Filter = "Archivo CSV|*.csv|Todos los archivos|*.*"
     .DialogTitle = " Seleccionar el archivo CSV"
     .ShowOpen
     CVS = .FileName
     'Para guardar el XLS
     .Filter = "Archivo Xls|*.xls"
     .FileName = ""
     .DialogTitle = " Escriba l nombre del archivo Xls "
     
     .ShowSave
     XLS = .FileName
End With

If CVS = "" Or XLS = "" Then
   Exit Sub
Else
   Call Exportar_CSV_XLS(CVS, XLS)
End If
End Sub

Private Sub Exportar_CSV_XLS(path_Cvs As String, path_Xls As String)

On Error GoTo ErrSub

Dim obj_Excel As Object
Dim Fila As Integer, Columna As Integer
Dim Contenido As String, Lineas As Variant
Dim datos As Variant, MC As Integer


    'Lee el contenido del CSV y lo almacena en la variable
    Open path_Cvs For Input As #1
    Contenido = Input$(LOF(1), #1)
    Close
    
    'Nuevo objeto Excel
    Set obj_Excel = CreateObject("Excel.Application")
    
    With obj_Excel
    'Agrega un libro
    .Workbooks.Add
    
    ' Obtiene el número de líneas del Csv con la función split
    Lineas = Split(Contenido, vbCrLf)
    
    For Fila = 0 To UBound(Lineas)
        
        'Separa los datos de la linea
        datos = Split(Lineas(Fila), ",")
        
        'Recorre los datos de esta fila que corresponden a cada campo
        For Columna = 0 To UBound(datos)
            ' Agrega el dato a la celda de la hoja activa
            .ActiveSheet.Cells(Fila + 1, Columna + 1) = datos(Columna)
        Next
        If MC < Columna Then
           MC = Columna
        End If
    Next
    
    'Selecciona toda la hoja
    .ActiveSheet.UsedRange.Select
    'Autoajusta las columnas
    .Selection.Columns.AutoFit
    
    
    'Selecciona el encabezado
    .ActiveSheet.Range(.ActiveSheet.Cells(1, 1), .ActiveSheet.Cells(1, MC)).Select
        
    End With
        
    ' Aplica atributos a la fuente a la selección anterior ( los encabezados )
    With obj_Excel.Selection.Font
        .Name = "Verdana"
        .FontStyle = "Bold"
        .Size = 14
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Underline = xlUnderlineStyleNone
    End With

    ' Guarda el documento Xls
    obj_Excel.ActiveWorkbook.SaveAs _
        FileName:=path_Xls, _
        FileFormat:=xlNormal, _
        Password:="", _
        WriteResPassword:="", _
        ReadOnlyRecommended:=False, _
        CreateBackup:=False

    obj_Excel.ActiveWorkbook.Close False

    'Cierra el archivo y elimina la variable
    obj_Excel.Quit
    Set obj_Excel = Nothing
    
    'Fin
    MsgBox "Archivo Xls guardado ", vbInformation
    
    Exit Sub
    
'Error
ErrSub:
    MsgBox Err.Description
    On Error Resume Next
    If Not obj_Excel Is Nothing Then
       obj_Excel.Quit
      Set obj_Excel = Nothing
    End If
End Sub
Y este codigo donde lo pongo ????
Manhy está desconectado   Responder Citando
Respuesta
No hay votos aún.


Herramientas
Desplegado

Normas de Publicación
No puedes crear nuevos temas
No puedes responder temas
No puedes subir archivos adjuntos
No puedes editar tus mensajes

BB code is Activado
Caritas están Activado
[IMG] está Activado
Código HTML está Desactivado


La Zona horaria es GMT -6. Ahora son las 03:05.


Message Board Statistics

LinkBacks Enabled by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93