Solved

Format Excel column date to MM/DD/YYYY in Visual Studio vb.net

Posted on 2016-08-03
5
287 Views
Last Modified: 2016-08-03
Hi all.

I'm exporting a datagridview control to Excel and I would like to format one of the columns to MM/DD/YYYY once it gets to Excel because the data is exporting to Excel as YYYY-MM-DD

In my code below I tried doing it but it's not changing the date format to MM/DD/YYYY as I would like: oXLSheet.Range("E:E").NumberFormat = "MM/DD/YYYY"

Thank you in advance.

 Dim oXLApp As Excel.Application 'Declare the object variables
        Dim oXLBook As Excel.Workbook
        Dim oXLSheet As Excel.Worksheet

        oXLApp = New Excel.Application  'Create a new instance of Excel
        oXLBook = oXLApp.Workbooks.Add

        oXLSheet = oXLBook.Worksheets(1)
        oXLApp.Visible = True

        Dim sql As String = "" & BuilderFilter() & ""   'The SQL Select statement used to populate the datagridview control when the search button is clicked

        Dim cn As New ADODB.Connection()
        Dim rs As New ADODB.Recordset()
        Dim cnStr As String
        Dim cmd As New ADODB.Command()

        cnStr = "Provider=SQLOLEDB;Data Source=myServer;Initial Catalog=myDB;Integrated Security=SSPI;"

        cn.Open(cnStr)
        cmd.ActiveConnection = cn
        cmd.CommandText = sql
        rs = cmd.Execute
        oXLSheet.Range("A1").CopyFromRecordset(rs)
        rs.Close()
        cn.Close()

        'Insert column headers
        oXLSheet.Rows(1).Insert()
        oXLSheet.Cells(1, 1).Value = "Blanket Order No"
        oXLSheet.Cells(1, 2).Value = "Customer Blanket Order No"
        oXLSheet.Cells(1, 3).Value = "Status"
        oXLSheet.Cells(1, 4).Value = "Account"
        oXLSheet.Cells(1, 5).Value = "Blanket Order Date"
        oXLSheet.Cells(1, 6).Value = "Blanket Order End Date"
        oXLSheet.Cells(1, 7).Value = "Blanket Create Date"
        oXLSheet.Cells(1, 8).Value = "Blanket Closed Date"
        oXLSheet.Cells(1, 9).Value = "Item Number"
        oXLSheet.Cells(1, 10).Value = "Quantity"
        oXLSheet.Cells(1, 11).Value = "Minimum Stock Qty"
        oXLSheet.Cells(1, 12).Value = "Item Status"
        oXLSheet.Cells(1, 13).Value = "Item Closed Date"
        oXLSheet.Cells(1, 14).Value = "Notes"

        oXLSheet.UsedRange.Borders.LineStyle = 1
        oXLApp.Columns.AutoFit()

        oXLSheet.Range("A1:N1").Font.Bold = True
        oXLSheet.Range("E:E").NumberFormat = "MM/DD/YYYY"


        oXLBook = Nothing   'Disconnect from Excel (let the user take over)
        oXLApp = Nothing
        oXLSheet = Nothing

Open in new window

0
Comment
Question by:printmedia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 41740611
Hi,
pls try

mm\/dd\/yyyy

Open in new window

Regards
0
 

Author Comment

by:printmedia
ID: 41740620
Thank you for your reply.

I tried mm\/dd\/yyyy

Did not work.

But I did notice that if I go into each cell and double click the cell, it changes it to mm/dd/yyyy (I saw this before applying your code suggestion). And when I go to the column "Format" it says "Custom".
0
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 41740627
one way to solve it is to change your sql to give back numbers as dates you can then format them to dates or to change the sql to give back the formatted text of the date
0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 41740642
Maybe you could change the type of the data  with something like this
for each c as oXLApp.Range in oXLApp.Range(oXLSheet.Range("E2"), oXLSheet.Range("E1048576").End(oXlApp.XlDirection.xlUp))
    c.Value = CInt(DateValue(c.Value))
Next

Open in new window

0
 

Author Comment

by:printmedia
ID: 41740643
I changed the sql to give back the formatted text of the date as you suggested and it correctly exported the date as MM/DD/YYY to Excel. I didn't need to use mm\/dd\/yyyy

Here's what I did in the sql statement:

CONVERT(VARCHAR,BlanketOrderDate,101) AS BlanketOrderDate

Open in new window


Thanks for your help.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question