poi导出Excel
前端代码
<button type="button" id="export">导出</button>
<script type="text/javascript">
$("#export").click(
function(){
window.location.href="/rest/food/exportExcel?ids="+ids;
}
);
</script>
后台代码
@RequestMapping(value ="/exportExcel", method = RequestMethod.GET)
@ResponseBody
public String exportExcel(@RequestParam Integer[] ids, HttpServletResponse response) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet=wb.createSheet("菜品表");
HSSFRow row1=sheet.createRow(0);
HSSFCell cell=row1.createCell(0);
cell.setCellValue("xxx商户菜品一览表");
sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
HSSFRow row2=sheet.createRow(1);
row2.createCell(0).setCellValue("菜品编号");
row2.createCell(1).setCellValue("菜品名称");
row2.createCell(2).setCellValue("菜品分类");
row2.createCell(3).setCellValue("条形码");
HSSFRow row3=sheet.createRow(2);
row3.createCell(0).setCellValue("李明");
row3.createCell(1).setCellValue("As178");
row3.createCell(2).setCellValue(87);
row3.createCell(3).setCellValue(78);
ServletOutputStream out =null;
String fileName="xxx商户菜品表";
response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"),"ISO-8859-1") + ".xls");
response.setContentType("application/msexcel;charset=GBK");
out = response.getOutputStream();
try {
wb.write(out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}