Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

Text property of date cell formatted as mm/dd/yyyy is read incorrectly #333

Open
allenking1 opened this issue Oct 15, 2018 · 11 comments
Open
Assignees

Comments

@allenking1
Copy link

How to reproduce: Create an xlsx file. Format a cell as Date in mm/dd/yyyy format and enter any date value. Suppose row =2, column =3. Cell value 9/13/2018.

EPPlus code:

Dim entrydate As String = worksheet.Cells(2, 3).Text
entrydate is '13' instead of '9/13/2018'

If cell date format is changed to yyyy-mm-dd, the read is fine. It appears "/" and EPPlus has issues in cell texts.

@andreluizsecco
Copy link

andreluizsecco commented Oct 18, 2018

Same problem here!
The workaround is to use this package: https://www.nuget.org/packages/EPPlus.Core/

@albertmata
Copy link

Same problem here, after updating from 4.1.1 to 4.5.3. Downgrading again....

@allenking1
Copy link
Author

allenking1 commented Feb 22, 2019 via email

@telefrancisco
Copy link

Same here but with dd/mm/yyyy format. Tested with 4.5.3.1.

@dbloch3643
Copy link

Guessing this may have been introduced when fixing #196

@JanKallman JanKallman self-assigned this Mar 7, 2019
@JanKallman
Copy link
Owner

I can't reproduce this in 4.5.3.1.

            using (var package = new ExcelPackage())
            {
                var ws = package.Workbook.Worksheets.Add("TextBug");
                ws.Cells["A1"].Value = new DateTime(2019, 3, 7);
                ws.Cells["A1"].Style.Numberformat.Format = "mm/dd/yyyy";

                Assert.AreEqual("03/07/2019", ws.Cells["A1"].Text);
            }

Can you provide some code to reproduce this bug.
Which Operating system, Regional Settings etc

@drabell
Copy link

drabell commented Mar 29, 2019

Hi Jan,

Reported bug (i.e. incorrect Text property of date cell) seems to be specific to the Short Date format applied to Excel cells containing date value. In order to reproduce it:

  1. Create Excel file (.xlsx) and enter sample date value (e.g. 3/14/2001) into the cell "A1"
  2. Select aforementioned cell, click on Number Format dropdown menu and select "Short Date" list item
  3. Save the file, open it with EPPlus package and read Text property of said cell: you will get erroneous valuelike "14" instead of entered 3/14/2001.
  4. Looking further into this issue: open the Number dialog box and see the sample Date format corresponding to Short Date shown as "3/14/2001". The note below stated: "...Date formats that begins with an asterisk () respond to changes in regional date and time settings that are specified for the operating system". At the same time, the Style.Numberformat.Format cell property in said EPPLus test case appeared as "mm-dd-yy"

Solution
Quick workaround was found by applying different date format to said cell originally formatted w/Short Date, like in the code snippet shown below:

if (String.Equals (ws.Cells["A1"].Style.Numberformat.Format, "mm-dd-yy", StringComparison.InvariantCultureIgnoreCase)) { ws.Cells["A1"].Style.Numberformat.Format = "mm/dd/yyyy"; }

After applying the aforementioned patch, the EPPlus package returns the correct Text property value of said cell, i.e. "03/14/2001".

Note: the solution works in the OS/Excel settings pertinent to the US localization; it is not guaranteed to be universally applicable to other locales.

Best rgds,
Alex

PS. It's relevant to mention that in order to strictly conform to the Excel date format specs, the month should be entered as "MM" instead of "mm" (the latter is usually interpreted as minutes).

JanKallman pushed a commit that referenced this issue Apr 10, 2019
@JanKallman
Copy link
Owner

Thanks, found the problem and added a fix. Please give it a try

@jburley01
Copy link

@JanKallman I just upgraded to version 4.5.3.1 and it is still happening for me.

The cell format is a Date with the type '*3/14/2012' selected in the spreadsheet. When I put a date of '3/25/2019' in the cell, the cell.Text property is '25' instead of the full date. If I put an single quote in front of the date in the excel spreadsheet it pulls in the correct value in the cell.Text property.

Hope this helps figure out the issue.

@nbsoftware
Copy link

Thanks, found the problem and added a fix. Please give it a try

I don't think this commit is in the 4.5.3.1 release.
@JanKallman do you have a beta version that we can test against?

Or else, when do you plan to release this fix?

@belall-shaikh
Copy link

Hi, the issue still persists.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants