none
VB.NET 2015: Date format column on export to excel file RRS feed

  • Question

  • Hello, firstly thanks for your attemption.
    Some time ago i asked about the fastest way to export to excel from VB.net form. I receibed the response y it works propertly. 

    https://social.msdn.microsoft.com/Forums/en-US/84df5ab3-f67f-42e8-8400-cb58e3b02411/fastest-way-to-export-a-datagridview-vbnet-2015-to-ms-excel-2007?forum=vbgeneral

    About the speed to export the data with this metho is awesome.

    Dim sheetIndex As Integer
                Dim Ex As Object
                Dim Wb As Object
                Dim Ws As Object
                Ex = CreateObject("Excel.Application")
                Wb = Ex.workbooks.add
    
                Dim col, row As Integer
                Dim rawData(tb_registros.Rows.Count, tb_registros.Columns.Count - 1) As Object
    
    
                For col = 0 To tb_registros.Columns.Count - 1
                    rawData(0, col) = tb_registros.Columns(col).ColumnName.ToUpper
                Next
    
    
                Dim NroCol As Integer = 0
                For col = 0 To tb_registros.Columns.Count - 1
                    fila = 0
                    For row = 0 To tb_registros.Rows.Count - 1
                        rawData(row + 1, col) = tb_registros.Rows(row).ItemArray(col)
                        fila = fila + 1
                    Next
                    BackgroundWorker1.ReportProgress(NroCol * 100 / tb_registros.Columns.Count)
                    NroCol = NroCol + 1
    
                    columna = columna + 1
                Next
    
                Dim finalColLetter As String = String.Empty
                finalColLetter = ExcelColName(tb_registros.Columns.Count)
    
    
                sheetIndex = sheetIndex + 1
                Ws = Wb.Worksheets(sheetIndex)
                'Ws.name = lb_vista.Text
                Dim excelRange As String = String.Format("A1:{0}{1}", finalColLetter, tb_registros.Rows.Count + 1)
    
                Ws.Range(excelRange, Type.Missing).Value2 = rawData
                Ws = Nothing
    
    
                Ex.Application.Visible = True
                GC.Collect()

    The problem with this method is that exports all columns on string format, this means that the date format columns, showes like a number.


    I would be grateful if someone could helpme.

    Regards.

    Monday, February 26, 2018 12:25 PM

Answers

  • Use NumberFormat. Simple example

    workRange.NumberFormat = "MM/DD/YYYY"


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, February 26, 2018 1:26 PM
    Moderator

All replies