Jump to content

Troca de Data


Ruben_Barbosa
 Share

Recommended Posts

Ola,

O meu problema é o seguinte:

A minha data é um DataTimerP1 que exporta a informação para excel com string e nao como Data.

Agora o meu Problema é o Seguinte quando faco um exporta de informação de VB.NET para excel o que me acontece é

Se a data for inferior a actual ele escreve-me MM/DD/AAAA no  Excel

Se a data for igual ou superior ele Escreve-me DD/MM/AAAA no Excel

Tambem pensei por o meu office ser um 2007 em portugues e o VB.net em ingles mas dps de meter a informação como string devia comer tudo  certo ?.

É esse o meu problema é  Se a data for inferior a actual ele escreve-me MM/DD/AAAA no  Excel e quero que escreve DD/MM/AAAA

Link to comment
Share on other sites


  'EXTRAIR INFORMACÃO PARA EXCEL
        Try
            Dim objApp As Object
            objApp = CreateObject("Excel.Application")
            Dim xlw As Microsoft.Office.Interop.Excel.Workbook
            Dim i As Integer = 0
            Dim j As Integer = 0
            Dim oldCI As System.Globalization.CultureInfo = _
              System.Threading.Thread.CurrentThread.CurrentCulture
            System.Threading.Thread.CurrentThread.CurrentCulture = _
                New System.Globalization.CultureInfo("en-US")

            xlw = objApp.Workbooks.Add

            xlw.Sheets(1).Select()

            objApp.ActiveSheet.Cells(1, 1) = "ID FOLHAS"
            objApp.ActiveSheet.Cells(1, 2) = "Departamento"
            objApp.ActiveSheet.Cells(1, 3) = "Utilizador"
            objApp.ActiveSheet.Cells(1, 4) = "Data Requisição"
            objApp.ActiveSheet.Cells(1, 5) = "Quantidade Unitária"
            objApp.ActiveSheet.Cells(1, 6) = "Observação"

            objApp.Columns.AutoFit()

            For i = 0 To DataGridView1.Rows.Count - 1       'Numero de Linhas para extrair informação
                objApp.Columns.AutoFit()
                For j = 0 To DataGridView1.Columns.Count - 1 'Numero de Colunas Com os Nomes para a BD
                    xlw.Application.Cells(i + 2, j + 1).value = DataGridView1.Rows(i).Cells(j).Value
                Next
            Next
            'Nex
            objApp.Visible = True
        Catch
            MsgBox("O EXCEL não pode ser iniciado!", vbCritical)
        End Try
Link to comment
Share on other sites

System.Globalization.CultureInfo("en-US")

DataGridView1.Rows(i).Cells(j).Value

E aposto que o teu campo da base de dados é DateTime.

Estás-lhe a dar uma "culture" diferente da que queres empregar e estás a usar o Value da célula.

Ou seja, com a culture errada, ele vai apanhar a data nesse formato. Depois o value contém o respectivo data type e não string.

Ou alteras o culture info e deve ficar OK, ou tens de adicionar lá em baixo no ciclo uma verificação para ver os nomes das colunas e se o nome da coluna for o campo de data, convertes o value para data e vais extraír os elementos com a ordem que pretendes.

Sérgio Ribeiro


"Great coders aren't born. They're compiled and released"
"Expert coders do not need a keyboard. They just throw magnets at the RAM chips"

Link to comment
Share on other sites

Então troca

DataGridView1.Rows(i).Cells(j).Value

para

DataGridView1.Rows(i).Cells(j).Value.ToString

e usa

Debug.Print(DataGridView1.Rows(i).Cells(j).Value.ToString)

para veres o que realmente vem da base de dados, para entendermos onde é que a conversão se está a dar.

Sérgio Ribeiro


"Great coders aren't born. They're compiled and released"
"Expert coders do not need a keyboard. They just throw magnets at the RAM chips"

Link to comment
Share on other sites

'EXTRAIR INFORMACÃO PARA EXCEL
        Try

            Dim objApp As Object
            objApp = CreateObject("Excel.Application")
            Dim objBook As Microsoft.Office.Interop.Excel.Workbook

            Dim i As Integer = 0
            Dim j As Integer = 0
            Dim oldCI As System.Globalization.CultureInfo = _
              System.Threading.Thread.CurrentThread.CurrentCulture
            System.Threading.Thread.CurrentThread.CurrentCulture = _
                New System.Globalization.CultureInfo("en-US")

            objBook = objApp.Workbooks.Add

            objBook.Sheets(1).Select()
            objApp.ActiveSheet.Cells(1, 1) = "ID FOLHAS"
            objApp.ActiveSheet.Cells(1, 2) = "Departamento"
            objApp.ActiveSheet.Cells(1, 3) = "Utilizador"
            objApp.ActiveSheet.Cells(1, 4) = "Data Requisição"
            objApp.ActiveSheet.Cells(1, 5) = "Quantidade Unitária"
            objApp.ActiveSheet.Cells(1, 6) = "Observação"

            objApp.Columns.AutoFit()
            For i = 0 To DataGridView1.Rows.Count - 1       'Numero de Linhas para extrair informação
                objApp.Columns.AutoFit()
                For j = 0 To DataGridView1.Columns.Count - 1 'Numero de Colunas Com os Nomes para a BD
                    TextBox1.Text = DataGridView1.Rows(i).Cells(j).Value
                    objBook.Application.Cells(i + 2, j + 1).value = TextBox1.Text
                Next
            Next
            'Nex
            objApp.Visible = True
            objApp = Nothing
            objBook = Nothing
        Catch
            MsgBox("O EXCEL não pode ser iniciado!", vbCritical)
        End Try

resolvi assim 😛 e ocultei a textbox 🙂 e deu 😄

Link to comment
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
 Share

×
×
  • 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.