Jump to content
Ricardo Goncalves

Exportar ficheiro com UTF8 BOM

Recommended Posts

Ricardo Goncalves

Bom dia,

 

Precisava da vossa ajuda no sentido de perceber o que poderá estar mal no meu codigo que coloco em baixo.

                If CheckBD = "0" Then
                    With mycommand
                        .Connection = con
                        .CommandText = "CREATE TABLE EnviosChrono (NumConta int,NumCliente int,Cliente nvarchar(254),Morada nvarchar(50),CodPostal int,Cidade nvarchar(50)," + _
                                        "Pais nvarchar(5),Contacto Nvarchar(254),Telefone int,Peso int,Referencia int not null,Volumes int,CriadoPor nvarchar(50),data datetime)"
                        .ExecuteNonQuery()
                    End With
                Else
                    With mycommand
                        .Connection = con
                        For i As Integer = 0 To DataGridView1.RowCount - 1
                            .CommandText = "insert into EnviosChrono values('" & DataGridView1.Rows(i).Cells(0).Value & "','" & DataGridView1.Rows(i).Cells(1).Value & "','" & DataGridView1.Rows(i).Cells(2).Value & "'," + _
                                "'" & DataGridView1.Rows(i).Cells(3).Value & "','" & DataGridView1.Rows(i).Cells(4).Value & "','" & DataGridView1.Rows(i).Cells(5).Value & "','" & DataGridView1.Rows(i).Cells(6).Value & "'," + _
                                "'" & DataGridView1.Rows(i).Cells(7).Value & "','" & DataGridView1.Rows(i).Cells(8).Value & "','" & DataGridView1.Rows(i).Cells(13).Value & "','" & DataGridView1.Rows(i).Cells(14).Value & "'" + _
                                ",'" & DataGridView1.Rows(i).Cells(15).Value & "','" & Departamento & "',GETDATE())"
                            .ExecuteNonQuery()
                        Next
                    End With
                End If
            Catch Ex As Exception
                MsgBox(Ex.Message, MsgBoxStyle.Critical)
            End Try
            Try
                Dim enc As Encoding = New UTF8Encoding(True)
                Dim Caminho As String = LeINI("Exportação", "Caminho", "")
                Dim data As String = DateTime.Now.ToString("yyyyMMdd_HHmmss")
                Dim filePath As String = " " & Caminho & "Chrono" & data & ".csv"
                Dim delimeter As String = ";"
                Dim sb As New StringBuilder
                For i As Integer = 0 To DataGridView1.Rows.Count - 1
                    Dim array As String() = New String(DataGridView1.Columns.Count - 1) {}
                    'If i.Equals(0) Then
                    '    For j As Integer = 0 To DataGridView1.Columns.Count - 1
                    '        array(j) = DataGridView1.Columns(j).HeaderText
                    '    Next
                    '    sb.AppendLine(String.Join(delimeter, array))
                    'End If
                    For j As Integer = 0 To DataGridView1.Columns.Count - 1
                        If Not DataGridView1.Rows(i).IsNewRow Then
                            array(j) = DataGridView1(j, i).Value.ToString
                        End If
                    Next
                    If Not DataGridView1.Rows(i).IsNewRow Then
                        sb.AppendLine(String.Join(delimeter, array))
                    End If
                Next
                File.WriteAllText(filePath, sb.ToString, enc)
                'Opens the file immediately after writing
                'Process.Start(filePath)
                Dim AckTime As Integer, InfoBox As Object
                InfoBox = CreateObject("WScript.Shell")
                AckTime = 1
                Select Case InfoBox.Popup("Ficheiros Criados Com Sucesso!!!", _
                AckTime, "Mensagem", 0)
                    Case 1, -1
                End Select
                DataGridView1.DataSource = Nothing
                DataGridView1.Rows.Clear()
            Catch Ex As Exception
                MsgBox(Ex.Message, MsgBoxStyle.Critical)
            End Try
        End If

O que se está a passar é que a pessar de ter o encoder do utf8 para sair com o BOM(Byte Order Marking)   " Dim enc As Encoding = New UTF8Encoding(True)"  o mesmo está sempre a sair sem BOM, com se tivesse o encoding a false no utf8.

Conseguem me dizer o que poderei estar a fazer de errado?

Obrigado!

 

 

 

 

Share this post


Link to post
Share on other sites
Ricardo Goncalves

O mais estranho é que mesmo trocando o encoder para ANSII 

Dim enc As Encoding = New ASCIIEncoding

ele continua a exportar o texto com UTF8.

 

Share this post


Link to post
Share on other sites
Ricardo Goncalves

Boas,

 

Podem fechar está resolvido!

 

Obrigado

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.