Cannot mapping to word

2 views
Skip to first unread message

Quang Bảo

unread,
Apr 28, 2025, 2:56:48 PMApr 28
to xdocreport

Method I'm using in my project to generate Word

public String generateWordFromTemplate(String templatePath, Map<String,Object> data) throws IOException, XDocReportException {

InputStream templateFile = getClass().getResourceAsStream(templatePath);

if(templateFile == null) throw new FileNotFoundException("Template not found" + templatePath);


IXDocReport report = XDocReportRegistry.getRegistry().loadReport(templateFile,TemplateEngineKind.Freemarker);

IContext context = report.createContext();

data.forEach((t, u) -> context.put(t, u));

File wordFile = File.createTempFile("xdoc", ".docx");

try(OutputStream out = new FileOutputStream(wordFile)){

report.process(context, out);

}

byte[] fileBytes = Files.readAllBytes(wordFile.toPath());

return Base64.getEncoder().encodeToString(fileBytes);

} Inside my template

[#list questions as q]

Câu hỏi ${q.id} : ${q.question}

A. ${q.optionA}

B. ${q.optionB}

C. ${q.optionC}

D. ${q.optionD}

Đáp án đúng: ${q.answer}

[/#list]


This is my data to mapping


questionMap.put("id", q.getId() != 0 ? q.getId() : "");

questionMap.put("question", q.getQuestion() != null ? q.getQuestion() : "");

questionMap.put("optionA", q.getAnswer().getA() != null ? q.getAnswer().getA() : "");

questionMap.put("optionB", q.getAnswer().getB() != null ? q.getAnswer().getB() : "");

questionMap.put("optionC", q.getAnswer().getC() != null ? q.getAnswer().getC() : "");

questionMap.put("optionD", q.getAnswer().getD() != null ? q.getAnswer().getD() : "");

questionMap.put("answer", q.getAnswer().getCorrect() != null ? q.getAnswer().getCorrect() : "");

It seems working if I remove the ${q.id} So what i gonna do now? Thank you

Reply all
Reply to author
Forward
0 new messages