Excel 表格 导出 (jar包看上篇)
前台:
工具栏toolbar{
id : 'button-export',
text : '导出',
iconCls : 'icon-undo',
handler : doExport
}
function doExport(){
window.location.href="${pageContext.request.contextPath }/bcSubarea/export.action";
}
后台:
/**
* 导出excel 表格
*/
@RequestMapping(value = "export.action")
public void export(HttpServletRequest request, HttpServletResponse response) throws IOException {
List<BcSubarea> bcSubareas = this.bcSubareaI.findAll();
List<BcSubareaVo> bcSubareaVos = new ArrayList<>(bcSubareas.size());
for (BcSubarea bcSubarea : bcSubareas) {
BcRegion bcRegion = this.bcRegionServiceI.findById(bcSubarea.getRegionId());
BcSubareaVo bcSubareaVo = new BcSubareaVo();
BeanUtils.copyProperties(bcSubarea, bcSubareaVo);
bcSubareaVo.setBcRegion(bcRegion);
bcSubareaVos.add(bcSubareaVo);
}
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.createRow(0);
row.createCell(0).setCellValue("分区id");
row.createCell(1).setCellValue("区域id");
row.createCell(2).setCellValue("分区关键字");
row.createCell(3).setCellValue("省市区");
for (BcSubareaVo bcSubareaVo : bcSubareaVos) {
row = sheet.createRow(sheet.getLastRowNum() + 1);
row.createCell(0).setCellValue(bcSubareaVo.getId());
row.createCell(1).setCellValue(bcSubareaVo.getBcRegion().getId());
row.createCell(2).setCellValue(bcSubareaVo.getAddresskey());
row.createCell(3).setCellValue(bcSubareaVo.getBcRegion().getProvince() + bcSubareaVo.getBcRegion().getCity());
}
String fileName = URLEncoder.encode("分区数据.xls", "UTF-8");
response.setContentType(request.getSession().getServletContext().getMimeType(fileName));
response.setHeader("content-disposition", "attchment;filename=" + fileName);
workbook.write(response.getOutputStream());
}