Results 1 to 14 of 14

Thread: [RESOLVED] [Excel] Need to read date data as date problem

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Resolved [RESOLVED] [Excel] Need to read date data as date problem

    So I get this excel sheet (*.xls) from another company and reading it in to our system, no problems until I get to a few specific cells where they have dates. The dates are not date formatted cells nor text. When I look (right click . cell formatting... ) it doesnt show any formatting at all. When I read in the cells they read in as NULL. I cannot manually reformat the cells to date format as its all for an automated process dealing with many files every day.

    Anyone have any tips on reading date data that reads in as null?

    Thanks

    Ps, I am connecting to it via OleDb in C# 2015 for a website project and the sheet is read in to a datatable.


    Code:
    order.DateLienFiled = string.IsNullOrEmpty(dt.Rows[7].ItemArray[13].ToString()) ? 
        (DateTime?)null :
                Convert.ToDateTime(dt.Rows[7].ItemArray[13].ToString());
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Excel] Need to read date data as date problem

    can you post a sample?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [Excel] Need to read date data as date problem

    You mean attach a sample excel workbook? Sure, I'll purge out the sensitive data and leave a couple date fields.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [Excel] Need to read date data as date problem

    Ok all the red date cells need reading but contain dates that read in as null. There is no formatting specified on those cells. Im sure using the EOM they probably could be read but since this is a ASP.NET project I highly doubt I can install anything on the server.
    Attached Files Attached Files
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [Excel] Need to read date data as date problem

    You can see it reads in as a DBNull when I manually changed the formatting to Date with mm/dd/yyyy formatting and that still didnt help. Its array element 13 for that cell.

    Something about the OleDb provider seems it can only read text?

    When I manually format the cell as TEXT then it works but I cannot change the formatting with an OleDb provider connection

    Edit: I may see if I can have teh provider append an apostrophe preceding the date data to automatically format the cell as TEXT.
    Attached Images Attached Images  
    Last edited by RobDog888; Jan 31st, 2019 at 07:14 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: [Excel] Need to read date data as date problem

    EDIT**** nvm i just saw in your last post you changed it to this format......

    well i check these cells i get a custom formatting, (Excel 2016) (mm/dd/yyyy) and removing the formatting i get the usual internal date (43160.## ...), maybe the DB is expecting something else?
    Last edited by GBeats; Jan 31st, 2019 at 08:41 PM.
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  7. #7
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: [Excel] Need to read date data as date problem

    just saw somewhere converting tthe value to an OADate might work.

    https://forums.asp.net/t/2139081.asp...ate+correctly+
    Last edited by GBeats; Jan 31st, 2019 at 08:48 PM.
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Excel] Need to read date data as date problem

    it is a bit interesting

    in excel i could see formatting for the dates rows 13 to 16 as mm/dd/yy, but the dates in rows 9 and 10 were general
    some of the dates are dd/mm/yyyy and others mm/dd/yyyy

    not only were the dates in rows 13 to 16 returning null, but also all $ values in those rows

    i sometimes got dates returned for the rows 8 to 10, depending on my connection string

    EDIT: on further testing, if i limit the range for the query to a1316, all dates are returned
    i am not sure if that will help you as i guess you do not know the length of the data, but it would appear you can use any arbitrary row for end of range, so maybe that can work for you
    Last edited by westconn1; Feb 1st, 2019 at 05:01 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [Excel] Need to read date data as date problem

    Quote Originally Posted by GBeats View Post
    EDIT**** nvm i just saw in your last post you changed it to this format......

    well i check these cells i get a custom formatting, (Excel 2016) (mm/dd/yyyy) and removing the formatting i get the usual internal date (43160.## ...), maybe the DB is expecting something else?
    Yea when removed formatting the date goes to a number that Excel uses to represent the date. If you then change the formatting to TEXT then it re-shows the date. I wish I could just manually change the formatting and be done but every day we will get a new set of workbooks for importing.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [Excel] Need to read date data as date problem

    I found this connection type which is supposed to make unknown date formats to be read as text basically. I ust tried it and it didnt change a thing,

    ImportMixedTypes=Text

    https://docs.microsoft.com/en-us/off...t-excel-driver


    Im thinking after wesconn1's post that maybe I can connect to the first half range to read in the basic info and then reconnect to just the invoice section to get the GuessRows to determine the proper date data type in Excel.

    The issue will still persist for the first half as its truely mixed data and I cant separately connect to each cell as probably wont be great on performance.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Excel] Need to read date data as date problem

    I cant separately connect to each cell as probably wont be great on performance.
    it would possibly work well enough if you read rows 1 to 8, 9 to 12 then the rest

    brief testing returned all the dates
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,268

    Re: [Excel] Need to read date data as date problem

    Does it help telling you, that i opened your sample-file on my Linux-Machine in Libre-Office Calc?
    Rows 8 - 10 show specific cell formating "dd.mm.yyyy" (german format?!?!?!)
    And rows 13 - 16 show specific cell formating "mm.dd.yyyy"
    So, in Calc i cannot recreate your statement "it doesnt show any formatting at all"
    Eh, and don't mind the delimiter. It's a dot in Germany....
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  13. #13

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [Excel] Need to read date data as date problem

    Quote Originally Posted by westconn1 View Post
    it is a bit interesting

    in excel i could see formatting for the dates rows 13 to 16 as mm/dd/yy, but the dates in rows 9 and 10 were general
    some of the dates are dd/mm/yyyy and others mm/dd/yyyy

    not only were the dates in rows 13 to 16 returning null, but also all $ values in those rows

    i sometimes got dates returned for the rows 8 to 10, depending on my connection string

    EDIT: on further testing, if i limit the range for the query to a13:p16, all dates are returned
    i am not sure if that will help you as i guess you do not know the length of the data, but it would appear you can use any arbitrary row for end of range, so maybe that can work for you
    Ok changing the query to a range for the invoicing table worked, Thanks!

    I query the entire sheet as original but when I get to the invoicing table I create a second datatable querying just the range. I read the range for my values and then on to the next table etc.

    Also, I changed the connectionstring around and added ImportMixedTypes and changed headers to NO. Everything seems to be working now upon initial testing. Seems the sampling rows of the standard 8 was causing havoc on the invoicing table as excel didnt know how to translate those data types. Also the header designation removal seems to help too from what I had read on the intewebs.

    So bottom line is that the bad design/layout of the spreadsheet was causing issues reading it with a standard connection string.

    Thanks guys!!


    Code:
    public string connectionString = "Provider=Microsoft.{0}.OLEDB.{1};Data Source={2};Extended Properties =\"Excel {3};ImportMixedTypes=Text;HDR=NO;IMEX=1\"";
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [Excel] Need to read date data as date problem

    Quote Originally Posted by Zvoni View Post
    Does it help telling you, that i opened your sample-file on my Linux-Machine in Libre-Office Calc?
    Rows 8 - 10 show specific cell formating "dd.mm.yyyy" (german format?!?!?!)
    And rows 13 - 16 show specific cell formating "mm.dd.yyyy"
    So, in Calc i cannot recreate your statement "it doesnt show any formatting at all"
    Eh, and don't mind the delimiter. It's a dot in Germany....
    Yea the dates are formatted two different ways and a third where theres no formatting at all, not even General or Text.

    I just posted the issue/solution, appreciate your time in looking at my sieeu
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width