I have cell which contains value
7.11.2014
in custom format
dd/mm/yyyy
so in excel it looks like
07/11/2014
when i load it via EPPLus
cellValue = sheet.Cells[row, column].Value.ToString();
it loads the General format value, which is
41950
How can i load the Value 07/11/2014 or other date format with i can work ?
Thank you
Looking over some similar code that I've done before, all you'll need to do is to use DateTime.Parse() and it should come out exactly how you want it.
As I mentioned in the comments Excel saves the actual dates as being a number of days from its epoch date of 1/1/1900 (hence why you keep seeing 41950). .NET code will accept that, and will convert it to the correct date.
OK i got it, this code works
long serialDate = long.Parse(sheet.Cells[r, c].Value.ToString());
cellValue = DateTime.FromOADate(serialDate).ToString("dd-MM-yyyy");
Thank you for help :)
Related
I'm relatively new to C# and I'm still learning a lot. I also searched stackoverflow for similar problems but got to a point where I'm stuck.
In my VSTO Excel project I'd like to output dynamic data coming from a database with a predefined number format per column also coming from the database. The user can define his own layout of the report. Everything is working just fine, I have just problems formatting the date value.
When it's about to output a date column, the format should be automatically be determined by the program instead of predefining it in the database as well. The date value gets already inserted in the Excel sheet as the decimal number it should be. Everything okay here. When I manually format it in Excel itself, it is displayed correctly.
But I want to format the date with C# like it would be formatted when I do it manually by clicking on the short date format in Excel. And it should work in every country, regardless in which language the office package was installed.
I'm working with a German installation and I expect the format to be "TT.MM.JJJJ". When formatting the column with this fixed string, it works perfectly (at least in Germany I guess).
It makes no difference if formatting it with
CurrentWorksheet.Columns[queryColumn.Position].NumberFormat = "TT.MM.JJJJ";
or
CurrentWorksheet.Columns[queryColumn.Position].NumberFormatLocal = "TT.MM.JJJJ";
Both end up in the same (correct) result. That the first line is working as well is the first thing which was unexpected for me, but that's not the main point. I tried to format it dynamically by replacing "TT.MM.JJJJ" with the following lines:
// Leads to dd.MM.yyyy
System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
// Fixed German culture still leads to dd.MM.yyyy
CultureInfo.CreateSpecificCulture("de-DE").DateTimeFormat.ShortDatePattern;
// MM/dd/yyyy - also not helpful
CultureInfo.InvariantCulture.DateTimeFormat.ShortDatePattern;
When using the first or second ShortDatePattern the date 27.08.2014 is shown as "dd.08.yyyy". Excel simply doesn't unterstand "dd" or "yyyy". I thought at least when using NumberFormat instead of NumberFormatLocal it would work, because it's the global format, but it doesn't. It feels like Microsoft has implemented the date formatting more complicated that it could be.
How would I determine the correct date pattern here?
The answer (it still is a kind of workaround) is pretty much found in another question:
How to set localized short-date format for a cell in Excel with C#?
There's just a very small difference I had to make in the method for constructing the ShortDatePattern (the Application parameter for DateFormatComponentCodes).
public static string ConstructExcelShortDatePattern()
{
var systemDateComponentCodes = new DateFormatComponentCodes();
var excelDateComponentCodes = new DateFormatComponentCodes(Globals.ThisAddIn.Application);
string systemShortDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
string excelShortDatePattern = systemShortDatePattern.Replace(systemDateComponentCodes.Year, excelDateComponentCodes.Year).Replace(systemDateComponentCodes.Month, excelDateComponentCodes.Month).Replace(systemDateComponentCodes.Day, excelDateComponentCodes.Day);
return excelShortDatePattern;
}
Thanks to Heinzi for finding a great answer and Jordan for providing such a useful class!
I am having a problem with parsing excel content to datetime type.
In my country, there are just two cultureinfo to use: "vi-VN" (dd/MM/yyyy) and "us-US" (MM/dd/yyyy).
For some cases I test, both DateTime.Parse with cultureinfo above throwing error "String is not recognized as a valid datetime".
I want to use DateTime.ParseExact instead of above approach. In order to do this, I must get a string like "12/31/2018". But when I use Epplus, it reads the content of the cell as a number 43465. This way, I cannot parse use DateTime.ParseExact.
Any here know how to read the excel as you see on the screen ?
Thank you.
DateTime.FromOADate(Double) Method
var dateTime = DateTime.FromOADate(43465);
I'm trying to read Excel 2007+ files in c# but all the libraries I have tried so far (OpenXML, ClosedXML and NPOI) seem unable to parse a cell with the time format correctly.
In Excel the data is formatted as Number > Time and uses '*hh:mm:ss' as it's type.
When I look at the raw value in the libraries it is appearing as 0.0416666666666667. I've followed advice from other posts which suggest using DateTime.FromOADate which (correctly) results in '30/12/1899 01:00:00'.
What I'm really stuck on is how to display the datetime object {30/12/1899 01:00:00} as it is displayed in Excel: '01:00:00'. I can see the Style.DateFormat is set to '[$-F400]h:mm:ss\ AM/PM' but how can I use this to format the DateTime object in C# as a string? The ToString() method doesn't recognise it as a valid format.
A DateTime by definition always has a date and a time. To only have the time you would have to use a TimeSpan. Here is a quick way you can get that.
DateTime originalDateTime = DateTime.Now;
TimeSpan hoursMinutesSeconds = originalDateTime.TimeOfDay;
Given a certain date, I want to set the value of a cell with a DateTime object, but without the "Time" information. For example, for today the value would just be "29/06/2012" and not "29/06/2012 16:54:36".
Concerning the display, for today's date I want it to be written like this (it is in french, I don't know how it would be in english): "29 juin" and not "29/06/2012".
How can I achieve this?
EDIT : I just took a look at the display formatting I need in Excel, it is "jj mmmm" ("dd mmmm" in C#). But the cell, though taking the value, does not take the formatting. Here is a piece of code:
cell.Value = string.Format("{0:dd/MM/yyyy}", DateTime.Now);
cell.NumberFormat = "jj mmmm";
I also tried:
cell.Value = DateTime.Now;
cell.NumberFormat = "jj mmmm";
In that case, the display formatting is OK, but the cell value contains the Time information, which is not OK.
See this StackOverflow question, and my answer to it. It will allow you to set the custom format for the cell directly in the excel sheet, from there you just need to research what the different date format strings are.
That is only helpful if you are using excel automation though.
Otherwise, formatting the DateTime.ToString output will be better. Once again, researching the different DateStringFormatting options will be helpful to you.
Final option. Directly change the template, not using automation. Goto the cell and manually change the cell format until the display is what you want. This does not change the actual data, just its display.
UPDATE
There are two specific issues to deal with.
Issue one is making sure excel recognizes that this IS a date. To that end, make certain that the date data itself is formatted en-US. Why, because Microsoft is dumb, and doesn't recognize international date formats.
Issue two is the display format for the cell. For a 3 letter abbreviation of the month, use the string d mmm. If you want the full month name, use d-mmmm.
I was testing and noted that excel refused to treat 29/06/2012 16:54:36 as a date, but it accepted 06/29/2012 16:54:36 without an issue. As I said, microsoft is dumb.
just change DateTime.Now to DateTime.Date and the time will be zero'd out
DateTime has a method called ToShortDateString
// Displays Fri 29 Aug
Console.WriteLine(date1.ToString("ddd d MMM", CultureInfo.CreateSpecificCulture("fr-FR")));
This would be a good resource for you: msdn custom date and time format strings
I’m building a application that monitors information on a website.
The website allows you to save stuff off it as a CSV.
My problem arises when I try to pull time and date information from Excel.
For those who want to see what spreadsheet I am working on:
http://ets.aeso.ca/ets_web/ip/Market/Reports/CSMPriceReportServlet?contentType=html
if you want the spreadsheet:
change the html in the link to csv
I’m having trouble with A6 downward and B6 downward column.
When I pull out the A6 column, the dynamic type is string.
My question regarding this part is:
Is there any way I can parse the string into a DateTime so I can format it in a standard way?
When I pull out the B6 column, the dynamic type for the time (HH:MM:SS AM/PM) becomes double.
Is there any way I can parse the double into a DateTime so I can format it in a standard way as well?
Thanks so much for your time you guys!
Cheers,
-Jeremiah Tantongco
Get .Text rather than .Value
Thanks for the responses!
I did some more digging and was able to solve both my issues.
For parsing a custom time formatted string into a datetime in C#, use:
DateTime.ParseExact(yourString, formatString, null);
format string is the scheme of your custom time formatted string
Ex:
"09/17/2009 14" becomes "MM'/'dd'/'yyyy HH"
For converting datetime from excel that is stored as a double, use:
DateTime.FromOADate(yourDouble);
if the file format is static then you can pull the data as string in first place say a5 + ":" + a6 (09/16/2009 16:15:00) reformate it as per your requirements and save is as datatime