js:
function exportExcel(){
var url = $basePath+'/basedata/person/exportExcel';
location.href = url;
}
controller:
@RequestMapping(value="/exportExcel", method =
RequestMethod.GET, produces = "application/json;charset=UTF-8")
@ResponseBody
public void exportExcel(HttpServletResponse
response,PersonFilter filter) throws BusinessException{
try {
filter = setFilter();
personService.exportExcel(response,filter);
} catch (Exception e) {
e.printStackTrace();
}
}
serviceimpl:
public void exportExcel(HttpServletResponse response,
PaginationFilter paramPaginationFilter) throws BusinessException
{
// TODO Auto-generated method stub
//Map paramMap = new HashMap();
try {
List personList = personMapper.query(paramPaginationFilter);
ExportExcel.exportForPerson(personList,
response);
} catch(Exception e) {
e.printStackTrace();
throw new BusinessException(e.getMessage());
}
}
ExportExcel工具类:
public static void exportForPerson(List personList,
HttpServletResponse response) {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet hssfSheet = wb.createSheet("员工信息");
hssfSheet.setColumnWidth(0,
WIDTH4000);
hssfSheet.setColumnWidth(1,
WIDTH6000);
hssfSheet.setColumnWidth(2,
WIDTH6000);
CellStyle cenStyle =
wb.createCellStyle();
cenStyle.setAlignment(CellStyle.ALIGN_CENTER);
// 创建表头
HSSFRow titleRow =
hssfSheet.createRow(0);
titleRow.createCell(0).setCellValue("员工编号");
titleRow.createCell(1).setCellValue("员工姓名");
titleRow.createCell(2).setCellValue("启用状态");
HSSFRow hssfRow;
if(null != personList
&& !personList.isEmpty()){
for(int i=0;i
Person person =
personList.get(i);
hssfRow =
hssfSheet.createRow(1 + i);
hssfRow.createCell(0).setCellValue(person.getBillNumber());
hssfRow.createCell(1).setCellValue(person.getName());
hssfRow.createCell(2).setCellValue(String.valueOf(person.getIsEnable()));
}
}
Date date =
new Date();
String
filename =
"Pserson".concat(simpleDateFormat.format(date)).concat(".xls");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-disposition",
"attachment;filename=" + filename);
OutputStream ouputStream;
try {
ouputStream =
response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
} catch
(IOException e) {
LOGGER.error(""+e);
ouputStream = null;
}
}