java 导出 TXT,CSV
程序员文章站
2024-03-20 21:05:16
...
导出这两个就比导出EXCel简单多了,主要是不用考虑一个一个对齐的问题,只要能取出来,就可以了,只要注意下action 的处理就可以了。
1:先看页面部分
<a href="#" onclick="javascript:sllToTxtOrCsv(0);">Txt </a>|
<a href="#" onclick="javascript:sllToTxtOrCsv(1);"> Csv</a>
2:页面中的JS部分
function sllToTxtOrCsv(txtOrCsv)
{
if(checkall()){
sd=document.getElementById("startDate").value;
ed=document.getElementById("endDate").value;
window.location.href="sllToTxtOrCsv.action?startDate="+sd+"&endDate="+ed+"&txtOrCsv="+txtOrCsv;
}
}
3:在看spring 中的代码
<action name="sllToTxtOrCsv" class="bjyearcountAction" method="sllToTxtOrCsv">
<result name="success1" type="dispatcher">/default/tjfx/sll_txt_result.jsp</result>
<result name="success2" type="dispatcher">/default/tjfx/sll_csv_result.jsp</result>
<result name="error" type="dispatcher">/default/error.jsp</result>
<interceptor-ref name="defaultStack"/>
</action>
4:Java中的action
public String sllToTxtOrCsv() {
org_id = super.getLoginUser().getUser_organid();
try {
ServletActionContext.getResponse().reset();
ServletActionContext.getResponse().setContentType(
"application/octet-stream");
if(txtOrCsv==0)
ServletActionContext.getResponse().setHeader("Content-Disposition",
"attachment; filename=slltj.txt");
else
ServletActionContext.getResponse().setHeader("Content-Disposition",
"attachment; filename=slltj.csv");
shouliList = birtReportService.getShouliBanjianList(startDate, endDate,
org_id, getLoginInfo().getUser().getUser_isadmin());
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
if(txtOrCsv==0)
return "success1";
else return "success2";
}
上一个是在存储过程中就处理好顺序了,下面这个列子是直接在action 中处理顺序,用MAP
public String sbjToTxtOrCsv() {
result = new ArrayList();
org_id = super.getLoginUser().getUser_organid();
try {
ServletActionContext.getResponse().reset();
ServletActionContext.getResponse().setContentType(
"application/octet-stream");
if (txtOrCsv == 0)
ServletActionContext.getResponse().setHeader(
"Content-Disposition", "attachment; filename=sbj.txt");
else
ServletActionContext.getResponse().setHeader(
"Content-Disposition", "attachment; filename=sbj.csv");
SimpleDateFormat simpledateformat = new SimpleDateFormat(
"yyyy-MM-dd");
sbjList = birtReportService.getSbjList(simpledateformat
.parse(this.startDate), simpledateformat
.parse(this.endDate), super.getLoginUser()
.getUser_organid(), super.getLoginUser().getUser_isadmin());
if (sbjList.size() > 0) {
Hashtable m = (Hashtable) sbjList.get(0);
Hashtable crj = new Hashtable();
Hashtable jbj = new Hashtable();
Hashtable spj = new Hashtable();
Hashtable hj = new Hashtable();
crj.put("bjlx", "承诺件");
crj.put("sj", m.get("CHENGNUOJIAN1"));
crj.put("bj", m.get("CHENGNUOJIAN2"));
jbj.put("bjlx", "即办件");
jbj.put("sj", m.get("JIBANJIAN1"));
jbj.put("bj", m.get("JIBANJIAN2"));
spj.put("bjlx", "审批件");
spj.put("sj", m.get("SHENPIJIAN1"));
spj.put("bj", m.get("SHENPIJIAN2"));
hj.put("bjlx", "合计");
hj.put("sj", m.get("TOTAL1"));
hj.put("bj", m.get("TOTAL2"));
result.add(crj);
result.add(jbj);
result.add(spj);
result.add(hj);
}
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
if (txtOrCsv == 0)
return "success1";
else
return "success2";
}
这里就不把实现层中的写出来了
让我们直接看页面中的代码
5:
5.1:sll_txt_result.jsp中的代码
<%@ page contentType="text/html;charset=gb2312"%>
<%@ taglib uri="webwork" prefix="ww" %>
<ww:iterator value="shouliList">
<ww:property value="ORGNAME"/> <ww:property value="name1"/> <ww:property value="name2"/> <ww:property value="name3"/> <ww:property value="name4"/> <ww:property value="name5"/> <ww:property value="name6"/> <ww:property value="name7"/> <ww:property value="name8"/> <ww:property value="name9"/>
</ww:iterator>
5.2:sll_csv_result.jsp中的代码
<%@ page contentType="text/html;charset=gb2312"%>
<%@ taglib uri="webwork" prefix="ww" %>
<ww:iterator value="shouliList">
<ww:property value="ORGNAME"/>,<ww:property value="name1"/>,<ww:property value="name2"/>,<ww:property value="name3"/>,<ww:property value="name4"/>,<ww:property value="name5"/>,<ww:property value="name6"/>,<ww:property value="name7"/>,<ww:property value="name8"/>,<ww:property value="name9"/>
</ww:iterator>
上面的是webwork的迭代方式,其他的框架用其他的迭代方式就可以了。
好了~~这里是不是比导出EXcel要简单多了?呵呵,希望各位能学到,下面我就用JS来实现导出Excel