How to leave cells empty in Date column instead of writing DateTime.Min in EPPlus - epplus

I need to write a formatted date to Excel using EPPlus, leaving empty cells where there are no dates to write. I have tried the following:
Writing a date, and then formatting it. This works unless I have no date, in which case the minimum value is written:
Formatting the date as a string (passing an empty string when there is no date), then assigning a custom format. The problem with this is that Excel doesn't see the type as a date, therefore a downstream system cannot use it:
How can I write dates to Excel, using EPPlus, where the dates are recognised as date types (not strings), but where missing date values are not written at all?

Make sure the data you bind to the date column is of type DateTime? (nullable).
Only if you provide a null value, an empty column will be rendered.
For example:
// Date format on first column
sheet.Column(1).Style.Numberformat.Format = "yyyy-mm-dd";
// Some date values
var columnValues = new List<DateTime?> {
DateTime.Now,
null,
DateTime.Now.AddDays(1) };
// Bind values to column
sheet.Cells[1, 1].LoadFromArrays(columnValues.Select(v => new object[] { v }));
Result:

Related

What are the benefits to convert text to date in SQLite

I want to store some dates into a table, why should I use date function to pass dates instead of inserting dates , simply, as text format.
CREATE TABLE dates (
Birthday Date /* date standart format YYYY-MM-DD */
);
INSERT INTO dates (Birthday)
VALUES (date('2005-12-30'));
Instead of
CREATE TABLE dates (
Birthday text
);
INSERT INTO dates (Birthday)
VALUES ('2005-12-30');
There is no DATE data type.
Declaring a column as DATE happens to use numeric affinity, but that does not matter when you store date strings in such a column.
SQLite's date() function can convert other date formats into a yyyy-mm-dd string, and apply modifiers.
If the input already is a string in the yyyy-mm-dd format, and if you are not using any modifiers, then the result of the function is guaranteed to be the same as the input, and the function call is pointless.
It is completely doable by storing it in a text format. But you will then be restricted to ordering and filtering the column as a text instead of a date. Meaning, if you need to order or filter the data by date I strongly recommend you to use one of the date/time formats. Also DBMS can optimize the queries, knowing it is a date...

PHPExcel - set cell format to "Date (short)" and insert date from string

I have an .xlsx file where cells of column C have format Date (short). In the cells is visible a date in this format: 30.1.2017.
If you switch the cells format to General you will see numbers like this: 42765.
I need to insert new line, set format Date (short) on C cell of new column and insert the date that I have in string. Cannot find approriate examples. How could it be done?
MS Excel stores dates as a serialized timestamp value, a count of the number of days since 1st January 1900 (or 1st January 1904 if created on a Mac)... that's what your 42765 value is.
PHPExcel provides a number of functions for date handling, which can be found in the PHPExcel_Shared_Date class.
You can convert a unix timestamp or a PHP DateTime object to an MS Excel serialized timestamp using the PHPToExcel() method. If you have a string value for your date, then convert it to a PHP DateTime object first.
Then you can set the cell value to the resulting timestamp value, and apply a number format mask to tell Excel how that date should be displayed. The 02types.php example in the Examples folder demonstrates this:
$dateTimeNow = time();
$objPHPExcel->getActiveSheet()
->setCellValue('C9', PHPExcel_Shared_Date::PHPToExcel( $dateTimeNow ));
$objPHPExcel->getActiveSheet()
->getStyle('C9')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
And it is also explained in the PHPExcel Documentation

How to create a Date variable in Elm

I want to hardcode a date in a record in elm. The record signature is
type alias Record = { .., startDate : Date, .. }
On my code I am doing
record = { .., startDate = Date.fromString "2011/1/1", .. }
The problem is that the Record type expects a Date type but Date.fromString signature is
String -> Result.Result String Date.Date
How can I create the Date to use on the Record type?
You're getting the Result because there is a chance that parsing the string to a date failed. You can handle it one of 2 ways.
Ignore it
If you want to just say "I know this string will be valid date and I'm not worried that I may have messed it up" then you can just provide a default date
Date.fromString "2011/1/1" |> Result.withDefault (Date.fromTime 0)
This will leave you with a Date but will default to the unix epoch if the parse fails.
Use it
Think about what you would want to happen if the parse were to fail and handle it where the date is used. Ex. if you're displaying it as a string you could display the date or if the parse failed display "TBA".
Note: You may have noticed that Date.fromTime just returns a Date not a Result (because an Int can always be parsed to a Date). If you don't mind converting your dates to unix timestamps you could hardcode the timestamp and use that without having to deal with Results

Sharepoint 2010 dates and calculated fields

For reasons I don't pretend to understand calculated fields that are set to return a date return the value in code in this format:
"datetime;#2015-04-25 00:00:00"
so dim myDate as datetime = oMasterItem("Contract End Date") fails, and you can't cast the value either.
How do I convert that to a real date format without doing string manipulation ?
(or am I missing something obvious?)
Many thanks!
Check this out:
SharePoint - get value of calculated field without manual parsing
You can do it by:
SPFieldCalculated cf = (SPFieldCalculated)myItem.Fields["CIDandTitle"];
string value = cf.GetFieldValueForEdit(myItem["CIDandTitle"]);
or
string value = cf.GetFieldValueAsText(myItem["CIDandTitle"]);

how to get excel to treat a date as a date not a string when doing CopyFromRecordset

I have an SQL query from SQL Server which returns dates as a string in the format "YYYY-MM-DD".
If I enter a date in this format into a cell, it's recognised as a date.
But when I populate a worksheet with CopyFromRecordset, it seems to be treated as a string.
Any formula which uses the cell converts it to a date first. For example, if my dates are in col A and I make a new column B filled with a formula =A1 + 0
the formula returns my date, as a date.
The problem:
I use the Recordset data for a few things, one of them being a pivot table.
The pivot table does not see my dates as dates. I can't group as dates, for example. My hack is to make a new column which is basically =A1 + 0
I'm going to change my macro to automate this adding a zero, but I wonder if there's a way to get it right from the moment the CopyFromRecordset is performed.
The easiest way would be to do the conversion on the SQL server e.g.
SELECT CAST(date_text AS DATE) FROM TestExcelDates;
CopyFromRecordset is well known for causing data type / cell formatting issues in Excel.
I think I remember reading somewhere this is because the datatype of the recordset is ignored and Excel attempts to work out the format of each column itself based on a subset of the data in the recordset.
The best way round this is to set the cell formatting in the destination range before performing the CopyFromRecordset.
I had this problem after I had changed a view on my SQL Server database. I had changed the data type to DATE; formerly it was on an older version which didn't support DATE so I had used DATETIME. I suspect Excel doesn't always recognize the Date datatype through the SQLOLEDB provider, but it does recognize DATETIME. The field of interest is meas_date. So I altered the view by changing this to a cast SELECT CAST(meas_date AS DATETIME) AS meas_date, ... and refreshed the query in Excel. Worked!
Use the CDate() function when populating cells with dates from the recordset. This will convert the string to a date value.
Edit
That works for setting individual cell values. For using CopyFromRecordset I think you need to do the conversino in the SQL query, so the column returned by the query is a date type rather than a string.
I had this problem too importing data from Teradata, and got around it by first formatting the date columns with NumberFormat = "m/d/yy h:mm;#" (24 hr date) then stepping through the date fields afterwards with VBA and doing ws.cells(iRow, iCol).value = ws.cells(iRow, iCol).value, it forces Excel to reevaluate the string into a date/time field.
This probably will not be the answer but will surely helps you finding the right solution for your problem
String stringCellValue = myCell.toString();
here myCell has datatype as CELL which I've converted to String format.
If u want it in desired Date format, then u can try this-
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
myCellDate = sdf.parse(stringCellValue );
Hope it helps in solving your problem...