可以选择性导出excel数据
js代码:
function subSearch(){
var field = "";
var wheres = "";
var startDate=$("#startDate").val();
var endDate=$("#endDate").val();
/* var agentName=$("#agentName").val();
var deviceMac=$("#deviceMac").val(); */
var companyName=$("#companyName").val();
var sale=$("#sale").val();
var agentProperty=$("#agentProperty").val();
var s='';
$('input[name="exportStr"]:checked').each(function(){
s+=$(this).val()+',';
});
window.location.href="router-store!execute.do?field="+encodeURI(encodeURI(s)) +"&routerAgent.startDate="+startDate+"&routerAgent.endDate="+endDate+
"&routerAgent.companyName="+companyName+
"&routerAgent.sale="+sale+"&routerAgent.agentProperty="+agentProperty;
hide();
}
body代码:
<div style="margin-top: 25px;margin-left: 45px;">
选择你要下载的项目类型
</div>
<div style="margin-top: 35px;margin-left: 100px;">
<input name="exportStr" type="checkbox" value="注册时间" checked="checked" />注册时间
<input name="exportStr" type="checkbox" value="注册名" checked="checked" />注册名
<input name="exportStr" type="checkbox" value="门店名称" checked="checked" />门店名称
<input name="exportStr" type="checkbox" value="联系人" checked="checked" />联系人
<input name="exportStr" type="checkbox" value="状态" checked="checked" />状态
<input name="exportStr" type="checkbox" value="店面性质" checked="checked" />店面性质
<input name="exportStr" type="checkbox" value="装机量" checked="checked" />装机量
<input name="exportStr" type="checkbox" value="下载量" checked="checked" />下载量
<input name="exportStr" type="checkbox" value="激活量" checked="checked" />激活量
</div>
<div style="margin-top: 50px;margin-left: 350px;">
<input value="确认下载" type="button" class="common_button"
οnclick="javascript:if(window.confirm('确认下载吗?')){subSearch();hide();}hide();">
</div>
action代码:
这里需要导入jxl.jar包,可以去网上下载
public String execute() throws Exception {
String fields = "";
fields = this.getField();
fields = java.net.URLDecoder.decode(fields,"UTF-8");
String[] newFile = fields.split(",");
try {
StringBuffer date = new StringBuffer(StrUtils.getNow(0)).append("store.xls");
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename="
+ date.toString());
excelStream = response.getOutputStream();
wwb = Workbook.createWorkbook(excelStream);
WritableSheet ws = wwb.createSheet("信息", 0);
Integer colwidth[] = new Integer[] {30,30,30,30,30,30,30,30,30};
for (int i = 0; i < newFile.length; i++) {
Label expressComm = new Label(i, 0, newFile[i]);
ws.addCell(expressComm);
ws.setColumnView(i, colwidth[i]);
}
List<RouterAgent> list = routerAgentMapper.exportStoreList(routerAgent);
int row = 1;
for (RouterAgent ra : list) {
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String Create_Time=(s.format(ra.getCreateTime()));//时间格式转换
for (int i = 0; i < newFile.length; i++) {
if (newFile[i].contains("注册时间")) {
ws.addCell(new Label(i, row, Create_Time));
continue;
}
if (newFile[i].contains("注册名")) {
ws.addCell(new Label(i, row, ra.getName()));
continue;
}
if (newFile[i].contains("门店名称")) {
ws.addCell(new Label(i, row, ra.getCompanyName()));
continue;
}
if (newFile[i].contains("联系人")) {
ws.addCell(new Label(i, row, ra.getLinkman()));
continue;
}
if (newFile[i].contains("状态")) {
if (ra.getStatus() == 1) {
ws.addCell(new Label(i, row, "启用"));
} else {
ws.addCell(new Label(i, row, "限制登录"));
}
continue;
}
if (newFile[i].contains("店面性质")) {
ws.addCell(new Label(i, row, ra.getCityProperty()));
continue;
}
if (newFile[i].contains("装机量")) {
String Phone_Installedsum = String.valueOf(ra
.getPhone_Installedsum());
ws.addCell(new Label(i, row, Phone_Installedsum));
continue;
}
if (newFile[i].contains("下载量")) {
String Soft_Installedsum = String.valueOf(ra
.getSoft_Installedsum());
ws.addCell(new Label(i, row, Soft_Installedsum));
continue;
}
if (newFile[i].contains("激活量")) {
String Soft_Validsum = String.valueOf(ra
.getSoft_Validsum());
ws.addCell(new Label(i, row, Soft_Validsum));
continue;
}
}
row++;
}
wwb.write();
} catch (Exception e) {
setMsg("导出失败。");
return "failed";
} finally {
try {
if (null != wwb) {
wwb.close();
wwb = null;
}
if (null != excelStream) {
excelStream.close();
excelStream = null;
}
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}