Generic dataset - date formating


We have created a report template on similar lines as Generic Reports 2.template.xls provided in the samples - except the formatting part. In the sample file the formatting for date is applied based on the column name - but in our case since the report template is used across different datasets we cannot have condition based formatting i.e the <#if(<>="OrderDate";<#format cell(longdate)>;)> is not possible. 

When we generate the report now it displays date as numbers (and the excel column type is general). Is there a way to get the column type so we can apply the format conditions? Or is there any other method we can use to get the date format working.

The same is the case for currency (but less critical).



Thanks for the reporting this. Indeed you are right, there is no easy way to get the datatype of the value you are entering. But there is a simple workaround: Use a user-defined function.

In the Generic Reports 2 demo you can add this class:

   public class DataTypeImp: TFlexCelUserFunction
        public override object Evaluate(object[] parameters)
            if (parameters == null || parameters.Length != 1)
                throw new Exception("DataType must be called with 1 parameter.");

            switch (parameters[0])
                case double d:
                    return "double";
                case DateTime dt:
                    return "datetime";
                    return "";

Then, add the line:

               Report.SetUserFunction("datatype", new DataTypeImp());

                Report.Run(DataPath + "Generic Reports 2.template.xls", saveFileDialog1.FileName);

And in the template use:
<#if(<#datatype(<#val>)>="datetime";<#format cell(longdate)>;)>

I've tried it here and it works as expected. I am undecided yet if this would be a good thing to add directly to FlexCel or not; the main problem being that the datatypes are unlimited. (In this example I just report doubles and datetimes, but I won't report for example an int).

But I'll see how we can make other cases like this simpler: At least we should change the Generic Reports 2 demo to include the UDF.