从web页面将数据库的数据读取出来,填充到Excel(2007+)表格里面,并讲表格下载到本地。
思路: 后台读取数据库 ---> 填充到Excel表格 ---> Excel表格存储到临时文件夹 ---> 从临时文件夹下载
jsp代码
<a class="btn btn-primary" type="button" id="download-excel">Download Excel Menu</a>
JavaScript代码
$('#download-excel').click(function(){
$.ajax({
url: 'createTempExcel',
type: 'post',
dataType: 'json',
success: function(data){
if(data.status == 'fail'){
alert('error');
}else{
window.location.href = "downloadExcel?fileName="+data.fileName;
}
}
});
});
@RequestMapping(value = "/createTempExcel", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
@ResponseBody
public Map<String, Object> createTempExcel(HttpServletRequest request) {
Map<String, Object> result = new HashMap<String, Object>();
// --- 数据库读取数据 存储到Excel保存到临时文件夹 ---
String tempPath = request.getServletContext().getInitParameter("tempPath");
File dir