[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.
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
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
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.
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.
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
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
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
Re: [Excel] Need to read date data as date problem
Originally Posted by GBeats
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.
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.
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
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
Re: [Excel] Need to read date data as date problem
Originally Posted by westconn1
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.
Re: [Excel] Need to read date data as date problem
Originally Posted by Zvoni
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.