kotlinxdocreport

Docx TableWith FieldsMetadata and Velocity


I'm trying to make a docx table with FieldsMetadata and Velocity but in the output file, each cell of the table contains the list itself and not the value of the cell.

Here my code :

// Build Docx
private fun buildDocxReport(): SystemFile {
    try {
        val resource = this.javaClass.classLoader.getResourceAsStream("reports/code.docx")

        val report = XDocReportRegistry.getRegistry().loadReport(resource, TemplateEngineKind.Velocity)

        val metadata = report.createFieldsMetadata()
        metadata.load("codes", Code::class.java, true)
       

        val context = report.createContext()
        context.put("codes", getAllCodes()); // getAllCodes() : return Listof<Code>

        val tempFile = File.createTempFile("${DateUtil.format(Date(), "yyyyMMdd")}_${IDs.short()}", ".docx")
        val fos = FileOutputStream(tempFile)

        // convert to pdf
        //val options = Options.getTo(ConverterTypeTo.PDF).via(ConverterTypeVia.XWPF)
        //report.convert(context, options, fos)

        report.process(context, fos)
        fos.close()

        return SystemFile(tempFile).attach(tempFile.name)

    } catch (e: Throwable) {
        e.printStackTrace()
        throw FunctionalException("Impossible de gérer le document. Une erreur s'es produite.")
    }

}

My class Code :

data class Code (
val code: String,
val libelle: String,
val base: String,
val taux: String? = "",
val gain: String? = "",
val retenue: String? = "")

Here my template : code.docx

And here my outpu docx :

listeCode.docx


Solution

  • It's because the cells aren't typed MergedField in the template File.