【发布时间】:2017-10-20 16:28:36
【问题描述】:
我做了一些研究,但找不到解决方法。当用户在客户端单击“导出没有 mailId 的记录”时,我想提供将我的数据库中的一些记录导出到 CSV 的可能性。实现这一目标的最佳方法是什么?在我的研究中,我发现人们只使用 Servlet 作为示例,但对我而言,我不使用 Servlet。
这是我的控制器:
@RequestMapping(value = "/eblnotif/exportMailId", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody List<EblNotifResource> exportMailIdCsv(@RequestBody Filters filters)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
SecurityException, IOException {
List<EblNotif> eblnotif_list = accountSservice.exportMailIdCsv(filters);
List<EblNotifResource> eblnotifres = new ArrayList<>();
for (EblNotif eblNotif : eblnotif_list) {
EblNotifResource eblres = new EblNotifResource();
eblres.setFormName(eblNotif.getFormName());
eblres.setSendingDate(eblNotif.getSendingDate());
eblres.setMode(eblNotif.getMode());
eblres.setLanguage(eblNotif.getLanguage());
eblres.setGpart(eblNotif.getGpart());
eblres.setEmail(eblNotif.getEmail());
eblres.setMailId(eblNotif.getMailId());
eblnotifres.add(eblres);
}
return eblnotifres;
}
}
并将这个json格式返回给客户端(JSP):
[
{
"formName":"FormName1",
"mode":"S",
"language":"F",
"sendingDate":"2017-04-03",
"gpart":"555",
"email":"",
"mailId":"96318"
},
{
"formName":"FormName2",
"mode":"S",
"language":"F",
"sendingDate":"2017-04-03",
"gpart":"444",
"email":"",
"mailId":"96325"
}
]
csv 中所需的输出将类似于:
formName;mode;language;sendingDate;gpart;email;mailId
FormName1;S;F;2017-04-03;555;testing@test;96318
FormName2;S;F;2017-04-03;444;testing@test;96325
【问题讨论】:
-
为什么不在控制器方法中将其转换为 CSV 并将生成的 CSV 字符串作为响应正文返回?
-
您好,感谢您的回答。因为我无法做到。如果我立即在我的控制器中转换它也很好。您能否参考我的链接或示例如何实现它?谢谢
标签: java spring csv spring-boot