前台代码
<form action="" id="dc" method="post" style="width: 51px;height: 30px" >
<input type="submit" id="excel" class="btn btn-info" οnclick="excelTrend();" value="导出">
</form>
js代码:
<script type="text/javascript">
function excelTrend(){
var url=null;
var start_time = $("#start_time").attr("value");
var end_time = $("#end_time").attr("value");
var start_hour = $("#cstart").val();
var end_hour = $("#cend").val();
var serverip=$("#serverip").val();
var client_ip=$("#client_ip").val();
url="/serve_count_user/ExcelTrend?start_time="+start_time;
document.getElementById("dc").action=url;
$("#dc").submit();
}
</script>
java 代码:
将你用sql查询的数据放在一个list集合里面,然后遍历:如:
List<Count_user> fileformat_= Count_user.me.find(table_sb.toString());//查询出来的结果集
List<Count_user> count_user_data= new ArrayList<>(); //Count_user是一个javabeen
for(int i=0;i<fileformat_.size();i++){
SerchResultCount_user country_data = new SerchResultCount_user();
country_data.setPass(fileformat_.get(i).getStr("pass"));
country_data.setName(fileformat_.get(i).getStr("name"));
country_data.setLevel(fileformat_.get(i).getStr("level"));
count_user_data.add(country_data);
}
Map<String,String> titleMap = new LinkedHashMap<String,String>();
titleMap.put("name", "姓名"); //name 是javabeen
titleMap.put("pass", "密码");
titleMap.put("level", "等级");
//导出方法
String sheetName = "用户信息";
String fname=sheetName+sdff.format(new Date())+".xls";
String path= ExportExcel.excelExport(count_user_data, titleMap, sheetName);
ExcelServlet sel=new ExcelServlet();
HttpServletResponse re= getResponse();
re.reset();
sel.download(path,re,fname);
在将需要的jar包导进去就OK了
效果: