lunes, 22 de octubre de 2018

Crear archivo

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim txt As New List(Of String)

        Dim config() As String = {"10,4,3,33",
                                  "10,10,10,2018-12-12",
                                  "2,8,20,venta al x mayor",
                                  "5,4,13,otra línea",
                                  "7,1,5,razón social",
                                  "8,7,7, dirección",
                                  "1,9,12,correo electrónico"}

        Dim linea As String = ""
        Dim fila As Integer = 0
        Dim columna As Integer = 0
        Dim largo As Integer = 0
        Dim dato As String

        For Each x In config
            fila = x.Split(",")(0)
            columna = x.Split(",")(1)
            largo = x.Split(",")(2)
            dato = x.Split(",")(3)

            x = x.Split(",")(0)

            ' Crear línea
            Dim n As Integer = txt.Count
            For i As Integer = n To x - 1
                txt.Add("")
            Next


            ' Crear dato
            Dim c As Char = Nothing
            Dim tmp_dato As String = ""
            If txt(x - 1) = "" Then

                dato = dato.PadRight(largo)

                For i As Integer = 0 To largo - 1
                    tmp_dato = tmp_dato & dato(i)
                Next


                For j As Integer = 0 To columna - 1
                    linea = linea & " "
                Next

                linea = linea & tmp_dato

           


                txt(x - 1) = linea
            Else

                'Dim largo_linea As Integer = linea.Length
                Dim existe As Boolean = False
                For i As Integer = 0 To linea.Length - 1
                    If i = columna Then
                        existe = True
                    End If
                Next

                If existe = False Then

                End If
                Dim pause As String = ""
            End If
        Next
        txt.Clear()
    End Sub
End Class


versión2
Imports System.Text

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim txt As New List(Of String)

        Dim config() As String = {"1,1,5,33",
                                  "2,8,7,98545456",
                                  "3,2,10,2018-10-22",
                                  "5,4,13,otra línea",
                                  "7,1,5,razón social",
                                  "8,7,7,dirección",
                                  "9,9,21,correo electrónico",
                                  "14,56,100,esto es una prueba",
                                  "100,1,4,fin"}

        ' "9,91,20,correo electrónico",


        Dim linea As String = ""
        Dim fila As Integer = 0
        Dim columna As Integer = 0
        Dim largo As Integer = 0
        Dim dato As String

        For Each x In config
            fila = x.Split(",")(0)
            columna = x.Split(",")(1)
            largo = x.Split(",")(2)
            dato = x.Split(",")(3)

            x = x.Split(",")(0)

            ' Crear línea
            Dim n As Integer = txt.Count
            For i As Integer = n To x - 1
                txt.Add("")
            Next


            ' Crear dato
            Dim c As Char = Nothing
            Dim tmp_dato As String = ""
            If txt(x - 1) = "" Then

                dato = dato.PadRight(largo)

                For i As Integer = 0 To largo - 1
                    tmp_dato = tmp_dato & dato(i) ' parte de 1,2...
                Next


                If columna = 1 Then
                    linea = linea & ""
                Else
                    For j As Integer = 0 To columna - 1
                        linea = linea & " "
                    Next

                End If


                Dim tmp As String = ""
                Dim k As Integer = 0
                If columna = 1 Then
                    tmp = linea & tmp_dato
                Else
                    For Each c In linea
                        If k = columna - 1 Then
                            tmp = tmp & tmp_dato
                            Exit For
                        Else
                            tmp = tmp & c
                        End If
                        k += 1
                    Next
                End If


                'Dim bytes As Byte() = System.Text.Encoding.Default.GetBytes(tmp)
                'tmp = System.Text.Encoding.UTF8.GetString(bytes)

                'tmp = Encoding.GetEncoding("ISO-8859-1").GetString(Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("ISO-8859-1"), Encoding.UTF8.GetBytes(tmp)))

                Dim n1 As Integer = tmp.Length
                n1 = New System.Globalization.StringInfo(tmp).LengthInTextElements







                'linea = linea & tmp_dato
                linea = tmp
                txt(x - 1) = linea
                linea = ""
            Else
                linea = txt(x - 1)
                Dim total As Integer = columna + largo
                Dim largo_linea As Integer = linea.Length

                If largo_linea < total Then
                    For i As Integer = largo_linea To total - 1
                        linea = linea & " "
                    Next

                    dato = dato.PadRight(largo)
                    For i As Integer = 0 To largo - 1
                        tmp_dato = tmp_dato & dato(i)
                    Next
                    ' Agregar dato
                    Dim tmp As String = Nothing
                    Dim k As Integer = 0
                    For Each c In linea

                        If k = columna - 1 Then
                            tmp = tmp & tmp_dato
                            Exit For
                        Else
                            tmp = tmp & c
                        End If
                        k += 1
                    Next
                    txt(x - 1) = tmp

                End If

                linea = ""


                Dim pause As String = ""




            End If
        Next
        ' archivo
        System.IO.File.WriteAllLines("archivo.txt", txt.ToArray, System.Text.Encoding.UTF8)
        'System.IO.File.WriteAllLines("archivo.txt", txt.ToArray, System.Text.Encoding.GetEncoding("ISO-8859-1"))
        txt.Clear()

        MessageBox.Show("Fin!")

    End Sub
    'https://stackoverflow.com/questions/14057434/how-can-i-transform-string-to-utf-8-in-c
    'https://stackoverflow.com/questions/11055122/counting-special-utf-8-character
End Class

versión 3
Imports System.Text

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim txt As New List(Of String)

        Dim config() As String = {"1,1,20,TipoDTE:33",
                                  "2,1,10,Folio  :5778",
                                  "3,1,20,FchEmis:2018-10-22",
                                  "4,1,20,FchVenc:2018-10-22",
                                  "5,4,13,otra línea",
                                  "7,1,5,razón social",
                                  "8,7,7,dirección",
                                  "9,9,21,correo electrónico",
        "10,34,10,******",
        "11,56,100,esto es una prueba",
        "12,36,45,juan perez",
        "13,1,4,fin",
        "14,105,10,Descuento",
        "14,119,10,precio",
                                  "15,23,14,."}

        '(fila,columna,largo,dato,True/False,)
        Dim linea As String = ""
        Dim fila As Integer = 0
        Dim columna As Integer = 0
        Dim largo As Integer = 0
        Dim dato As String

        For Each x In config
            fila = x.Split(",")(0)
            columna = x.Split(",")(1)
            largo = x.Split(",")(2)
            dato = x.Split(",")(3)

            x = x.Split(",")(0)

            ' Crear línea
            Dim n As Integer = txt.Count
            For i As Integer = n To x - 1
                txt.Add("")
            Next


            ' Crear dato
            Dim c As Char = Nothing
            Dim tmp_dato As String = ""
            If txt(x - 1) = "" Then

                dato = dato.PadRight(largo)

                For i As Integer = 0 To largo - 1
                    tmp_dato = tmp_dato & dato(i) ' parte de 1,2...
                Next


                If columna = 1 Then
                    linea = linea & ""
                Else
                    For j As Integer = 0 To columna - 1
                        linea = linea & " "
                    Next

                End If


                Dim tmp As String = ""
                Dim k As Integer = 0
                If columna = 1 Then
                    tmp = linea & tmp_dato
                Else
                    For Each c In linea
                        If k = columna - 1 Then
                            tmp = tmp & tmp_dato
                            Exit For
                        Else
                            tmp = tmp & c
                        End If
                        k += 1
                    Next
                End If
                'linea = linea & tmp_dato
                linea = tmp
                txt(x - 1) = linea
                linea = ""
            Else
                linea = txt(x - 1)
                Dim total As Integer = columna + largo
                Dim largo_linea As Integer = linea.Length

                If largo_linea < total Then
                    For i As Integer = largo_linea To total - 1
                        linea = linea & " "
                    Next

                    dato = dato.PadRight(largo)
                    For i As Integer = 0 To largo - 1
                        tmp_dato = tmp_dato & dato(i)
                    Next
                    ' Agregar dato
                    Dim tmp As String = Nothing
                    Dim k As Integer = 0
                    For Each c In linea

                        If k = columna - 1 Then
                            tmp = tmp & tmp_dato
                            Exit For
                        Else
                            tmp = tmp & c
                        End If
                        k += 1
                    Next
                    txt(x - 1) = tmp

                End If

                linea = ""
                Dim pause As String = ""
            End If
        Next
        ' archivo
        System.IO.File.WriteAllLines("archivo.txt", txt.ToArray, System.Text.Encoding.GetEncoding(1252))
        'System.IO.File.WriteAllLines("archivo.txt", txt.ToArray, System.Text.Encoding.GetEncoding("ISO-8859-1"))
        txt.Clear()

        MessageBox.Show("Fin!")

    End Sub
    'https://stackoverflow.com/questions/14057434/how-can-i-transform-string-to-utf-8-in-c
    'https://stackoverflow.com/questions/11055122/counting-special-utf-8-character
End Class

versión4
Imports System.Text

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim txt As New List(Of String)

        Dim config() As String = {"1,30,10,TipoDTE:33",
                                  "2,5,20,Folio  :5778",
                                  "1,1,20,FchEmis:2018-10-22",
                                  "4,108,40,hola",
                                  "4,98,10,chao",
                                  "4,1,97,."}
        '"4,1,20,FchVenc:2018-10-22",
        '"5,4,13,otra línea",
        '"7,1,5,razón social",
        '"8,7,7,dirección",
        '"9,9,21,correo electrónico",
        '"10,34,10,******",
        '"11,56,100,esto es una prueba",
        '"12,36,45,juan perez",
        '"13,1,4,fin",
        '"14,105,10,Descuento",
        '"14,119,10,precio",
        '"15,23,14,."}

        '(fila,columna,largo,dato,True/False,)
        Dim linea As String = ""
        Dim fila As Integer = 0
        Dim columna As Integer = 0
        Dim largo As Integer = 0
        Dim dato As String

        For Each x In config
            fila = x.Split(",")(0)
            columna = x.Split(",")(1)
            largo = x.Split(",")(2)
            dato = x.Split(",")(3)

            x = x.Split(",")(0)

            ' Crear línea
            Dim n As Integer = txt.Count
            For i As Integer = n To x - 1
                txt.Add("")
            Next


            ' Crear dato
            Dim c As Char = Nothing
            Dim tmp_dato As String = ""
            If txt(x - 1) = "" Then

                dato = dato.PadRight(largo)

                For i As Integer = 0 To largo - 1
                    tmp_dato = tmp_dato & dato(i) ' parte de 1,2...
                Next


                If columna = 1 Then
                    linea = linea & ""
                Else
                    For j As Integer = 0 To columna - 1
                        linea = linea & " "
                    Next

                End If


                Dim tmp As String = ""
                Dim k As Integer = 0
                If columna = 1 Then
                    tmp = linea & tmp_dato
                Else
                    For Each c In linea
                        If k = columna - 1 Then
                            tmp = tmp & tmp_dato
                            Exit For
                        Else
                            tmp = tmp & c
                        End If
                        k += 1
                    Next
                End If
                'linea = linea & tmp_dato
                linea = tmp
                txt(x - 1) = linea
                linea = ""
            Else
                linea = txt(x - 1)
                Dim total As Integer = columna + largo
                Dim largo_linea As Integer = linea.Length

                If largo_linea < total Then
                    For i As Integer = largo_linea To total - 1
                        linea = linea & " "
                    Next

                    dato = dato.PadRight(largo)
                    For i As Integer = 0 To largo - 1
                        tmp_dato = tmp_dato & dato(i)
                    Next
                    ' Agregar dato
                    Dim tmp As String = Nothing
                    Dim k As Integer = 0
                    For Each c In linea

                        If k = columna - 1 Then
                            tmp = tmp & tmp_dato
                            Exit For
                        Else
                            tmp = tmp & c
                        End If
                        k += 1
                    Next
                    txt(x - 1) = tmp
                Else
                    ' ya existe una línea con suficiente espacio
                    dato = dato.PadRight(largo)
                    For i As Integer = 0 To largo - 1
                        tmp_dato = tmp_dato & dato(i)
                    Next

                    Dim tmp As String = Nothing
                    'Dim k As Integer = 0
                    'Dim j As Integer = 0
                    Dim z As Integer = 0
                    Dim tmp_linea() As Char = linea.ToCharArray
                    For i As Integer = 0 To linea.Length - 1

                        If i = columna - 1 Then

                            For j As Integer = i To i + largo - 1
                                tmp_linea(j) = tmp_dato(z)
                                z += 1
                            Next
                        End If
                        If z = largo Then
                            Exit For
                        End If


                    Next
                    txt(x - 1) = New String(tmp_linea)

                    Dim pause2 As String = ""

                End If

                linea = ""
                Dim pause As String = ""
                End If
        Next
        ' archivo
        System.IO.File.WriteAllLines("archivo.txt", txt.ToArray, System.Text.Encoding.GetEncoding(1252))
        'System.IO.File.WriteAllLines("archivo.txt", txt.ToArray, System.Text.Encoding.GetEncoding("ISO-8859-1"))
        txt.Clear()

        MessageBox.Show("Fin!")

    End Sub
    'https://stackoverflow.com/questions/14057434/how-can-i-transform-string-to-utf-8-in-c
    'https://stackoverflow.com/questions/11055122/counting-special-utf-8-character
End Class

No hay comentarios.:

Publicar un comentario