In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
今天就跟大家聊聊有关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文本有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.