Can i add customerizing formatter to the linq report engine?

Hello team,

Can i add customerizing formatter to the linq report engine?

I hope to compete the flowing tasks( like the accounting number format in excel) by adding a formatter:

  1. if the value is not numberic, convert it to a number
  2. if the number is null or zero, format it as “-”
  3. if the number is positive, round to the nearest hundredth. For example, 111.113 will be formatted as 111.11
  4. else, round to the nearest hundredth and bracket it. For example, -222 will be formatted as (222.00)

@Doraemon You can implement your own custom formatter and use it in your code. For example see the following code:

ArrayList values = new ArrayList();
values.add("1234");
values.add(111.113d);
values.add(-222d);
values.add(null);

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("<<foreach [v in values]>><<[CustomFormatter.formatValue(v)]>>");
builder.write("<</foreach>>");
ReportingEngine engine = new ReportingEngine();
engine.getKnownTypes().add(CustomFormatter.class);
engine.buildReport(doc, values, "values");
doc.save("C:\\Temp\\out.docx");
public class CustomFormatter {
    public static String formatValue(Object val) throws Exception
    {
        if(val == null)
            return "-";
        
        double doubleVal = (val instanceof Double) ? (double)val : Double.parseDouble((String)val);
        if(doubleVal>=0)
            return Double.toString(Math.round(doubleVal * 100.0) / 100.0);
        else
            return "("+ Double.toString(-Math.round(doubleVal * 100.0) / 100.0) + ")";
    }
}

@alexey.noskov
I got it.

Should the KnownType‘s simpleNames be unique?
If I have com.a.CustomerFormatter and com.b.CustomerFormatter, should I rename one of them?

Can i have two format methods in one formatter ? Like this.

public class Formatters {

    public void accountingNumber(Object value){
        

    }
    
    
    public void otherFormat(Object value){
        
    }

}

<<[Formatters.accountingNumber(v)]>>
<<[Formatters.otherFormat(v)]>>

@Doraemon Sure, you can have as many methods in one class as it is required. In the template you simply use the method the same way as you use method in simple java code.

No, it is not required. Simply use a fully qualified names in the template.

@alexey.noskov
Thank you for your support. It’s great. That’s actually what i want.

1 Like