温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

WebWork实现数据怎么导出CSV文本

发布时间:2021-12-18 15:49:54 来源:亿速云 阅读:122 作者:柒染 栏目:编程语言

今天就跟大家聊聊有关WebWork实现数据怎么导出CSV文本,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

以往,在WebWork中实现导出CSV文本时,采用的是之前自己实现的csv result,但是这个实现有个缺陷就是对象嵌套不支持,而且需要构造dto遍历获取列表进行列表字段显示的封装,且在WebWork配置文件要罗列出需要导出的字段,过于繁琐。

于是产生了采用直接用freemarker作为导出CSV文本文件模板的想法,这样后台只做查询列表数据的获取,WebWork配置文件只需要定位到指定freemarker模板,而且导出排版可以在freemarker中更灵活的得到控制。

其中,WebWork在导出CSV文本过程中,中文乱码问题上尤其需要注意的是,一定要设置模板文件的编码。

以下是实现实例:

1 public String sampelExport()throws Exception{  2     samplelist = SampleService.getAll();  3     return SUCCESS;  4}

//samplelist的getter不要忘记哦。

SampleObject具有name属性,类型为String,createDate属性,类型为java.util.date,emails属性,类型为java.util.list,为一个email封装对象列表,status属性,类型为Enum,提供按locale获取显示内容方法。

1 〈action name="sampleexport" method="sampleExport" 2      class="org.hook.webwork.SampleExportAction"〉  3      〈result type="freemarker" name="success"〉  4          template/default/sampleexport.ftl  5      〈result〉  6 〈/action〉
1 〈#ftl encoding="GBK"〉   2 〈#if response?exists〉${response.setHeader  ("Content-Disposition","attachment;   filename=sample_export.csv")}${response.setHeader  ("Cache-  Control", "cache")}${response.setContentType  ("text/plain; charset=GBK")}${action.getText  ('org.hook.sampleobject.name')},  ${action.getText('org.hook.sampleobject.createdate')},  ${action.getText('org.hook.sampleobject.emails')},  ${action.getText('org.hook.sampleobject.status')}〈/#if〉  3 〈#if samplelist?exists〉  4 〈#list samplelist as sampleobject〉"${sampleobject.name}",  "${sampleobject.createDate?date}",  "〈#list sampleobject.emailss as email〉${email.fullinfo}   〈/#list〉",  〈#if sampleobject.status?exists&&sampleobject.status.  getValue(locale)?exists〉  ${sampleobject.status.getValue(locale)}〈/#if〉  5 〈/#list〉  6 〈/#if〉

看完上述内容,你们对WebWork实现数据怎么导出CSV文本有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI