ClosedXML negative duration - c#

I am creating report avaible to downlad as excel file. In the report I have durations as string HH:MM. Everything looks fine untill durations comes as "-HH:MM" For non negative durations excel works great but for negative it shows like ########
Here is what I am doing in C#:
worksheet.Cell("F" + _freeFrom).Value = totalDuration;
worksheet.Cell("F" + _freeFrom).Style.NumberFormat.Format = "H:mm";
Anyone have idea how to solve that problem ?

That is a "problem" in Excel itself which does not show negative time values in the default date system. You can switch to the 1904 date system with ClosedXML like this:
workbook.SetUse1904DateSystem(true);
For more information about the Excel date systems see here.

Related

Reading Time HH:mm:ss from excel cell in c#

I need to extract the time from an excel file. The time in excel is expressed in hours:minutes:seconds. The c# code i have that reads the time is:
DateTime dt = DateTime.Parse(worksheet.Cells[row, 3].Value.ToString());
string GetTime = String.Format("{0:t}", dt);
This code works perfect with one file but when i insert another similar file it does not reads the time. Does anyone know why this happens.
Excel table that DOES read the time:
Id
Date
Time
1
18/11/2022
11:51:00
Excel table that DOES NOT read the time:
Id
Date
Time
1
08/08/2022
06:54:00
Excel supports dates natively. Dates are stored in binary form (specifically a floating point number), not as text. They have no format. How they're displayed depends on the cell's numeric format and the end user's locale settings, but the actual value remains binary.
Even when you see a time text in a field, the underlying value is a DateTime whose numeric style shows only the time part. You can test that by changing the cell's numeric style to a full date time or number.
All Excel libraries will load Excel dates as .NET DateTime values. If the Excel sheet contains actual dates, Value is already a DateTime. To get its time part use the DateTime.TimeOfDay property.
For example :
TimeSpan time = ((DateTime)worksheet.Cells[row, 3].Value).TimeOfDay;
I found the solution you have to use FromOADate to get the correct format.
DateTime dt = DateTime.FromOADate((double)worksheet.Cells[row, 3].Value);

Reading dates from an excel file in SSIS. SSIS reads dates in a different format

An excel file has all the dates formatted as MM/dd/yy, I read it in SSIS via c# script, but while looping through the records I notice that some dates are read as dd/MM/yy. Somehow either Excel or SSIS are messing with the date format. Since some of the dates are correctly formatted as MM/dd/yy I am not able to find out which are correctly formatted and which are not. Is there a way to ensure the Excel sheet is read in a specific date format?
I have tried changing the configuration of IMEX to 1 in the connection string.
Also tried in the code to identify the dates, but it is impossible since, 01-02-2019 could be January 02 or Feb 01.
The connection string I am using looks like this:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + ";Extended Properties=" + "\"" + "EXCEL 12.0 XML; HDR=NO; IMEX=1" + "\"";
I open the excel file in the script and loop through the rows.
The excel has dates like these:
01/31/19
01/02/19
08/19/19
07/06/19
When I read it in SSIS I get these results:
01/31/19
02/01/19
19/08/19
06/07/19
As you can see the date format changes, for some rows.
Is there a way to ensure the format in all read dates?
Excel is mostly the culprit. It forces its own data types on SSIS. If you can convert that column to text, it is a good start. (but mostly it is user input right? so probably not)
The problem could be that if the Regional Settings on machines (server/users) is not the same, the format will not be the same as Excel is very much Regional setting driven. (But not so much relevant if this is all on the same machine)
The second problem I have seen is SSIS converting to a DATE is not consistent. In most of my Excel date columns in SSIS, I convert the column to Unicode and then build it up with in expression using SUBSTRING and +. (I though it was stupid until it resolved the issue most of the time. So I have been sticking to it.)
Thus my suggestion, build up the Date you want with String Functions.

C# convert Date to text in cell in Excel (interop)

I have a pretty specific problem with the "Automatic data conversions" that Excel is doing. First I'll try to explain what exactly I'm doing and how Excel is making fun of me ;)
My program is creating a Charts for reports from temperature sensors. In the reports the first Column is the date of the reading and the second column is the reading of the sensors for that date. The problem is the automatic selection of the "XAxis series" that Excel is doing, if the first column is in DateTime format:
Here is the example for what Excel is doing with the Charts if it "smells" Date format...
And Here is how the chart looks if the first column is in Text format.
I tried some variants to convert the values in the first column but everything is too slow or messes up the values.
So far the "fastest" way that I found is:
foreach(Range cell in firstCol.Cells)
{
if (cell.Value is DateTime && !(cell.Value).ToString().Contains("\'"))
{
cell.Value = "\'" + cell.Value;
}
}
But that's not fast enough. Imagine a 100 sheets (1 sheet for each sensor) and each sheet with between 2500 and 4000 rows.
I tried things like:
usedRange.NumberFormat = "#";
as it was suggested in a couple of places on internet but that doesn't work for Date formats. It converts this:
01/12/2016 00:30 like this: 42705.02083.
So my question is if someone can suggest a better or faster way to convert the dates or to make Excel read the DateTime as text without messing up anything.
I'm sure there's a better way by fixing the X axis itself, but if all your looking for is to get the date in text format, the quick fix would be something like:
=TEXT(DATE(2016,1,12)+TIME(0,30,0),"MM/dd/yyyy hh:mm")

display a timespan in excel

I have a timespan stored as a varchar in sql in the format of totalhours-totalminutes-totalseconds.
Now I have to export that information into excel.
The problem I'm having is that I can not for the life of me find a format within excel that will display this information correctly. Is there a way in which this information can be displayed correctly? I am using XLS in C# to output this info.
Sample input: 48:00:11
Sample output: 1900/02/17 12:11:00 AM or 48.0076388888889
For formatting, you need to set a custom time format:
[h]:mm:ss
...which will show numbers of hours for durations longer than 24 hours. If you type "48:00:11" into an Excel cell, it will create that custom format for you (at least, it did in Excel 2007 on my machine just now). Removing formatting shows the value to be the expected double (which all non-strings are): 2.000127, which is the number of days (with time as fraction) for the duration.
Putting that string value into a worksheet from C# may have a different outcome: I suspect so from the 48.007638... value you obtained. Since you have C# at your disposal I would be inclined to calculate the timespan as a number of days prior to pushing the value to Excel, then formatting as above.

Excel Shows "#########" when i try to write DateTime.Now.tostring() to one of the cell in Excel File

I have created a application in c# , it reads excel file and after checking some conditions, it select a row to be written in another Excel File.
Everything is working fine, but i need to end the file with the DateTime.Now.ToString().
string date = DateTime.Now.ToString();
ExcelWorkSheet2.Cells[newFileRow, 1] = date;
When I see the file created, it shows "########" symbol instead of actual date. When I select that cell , it changes to correct date format.
What may be going wrong?
##### is typically shown by Excel when the value in the cell is too wide. What happens if you try to expand the column width a little bit?
You need to expand the size of the cell to make it display properly. This is most easily done by double clicking on the column header to make it expand to fit all data.
Just try widening the column display a little.
As the others have said, the column isn't wide enough for your datetime value.
Without putting anything in the brackets, DateTime.Now.ToString() will return every detail of the current date.
You can cut this down by putting some speech marks in between the brackets, followed by some formatting information.
dd for current date
MM for current month
yyyy for current year
hh for current hour
mm for current minute (notice the lowercase)
ss for current second
So for example, if you just wanted the date without any time information, you would put the following which would return 2012-03-25:
var thisString = DateTime.Now.ToString("yyyy-MM-dd");
Thanks
I think you might want to shorten DateTime format and/or widen your cell a bit.
Please refer to this information on DateTime formatting.
More:
DateTime.Now.ToString() usually produces long string, something like "3/25/2012 7:26:26 PM". If this is something you would like to display, then the only option for you is to make your cell/column wider.
Otherwise consider one of many formats that suit you well.
Example: DateTime.Now.ToString("MM-dd-yy")

Categories

Resources