使用poi进行excel下载

进行信息表excel导出

@RequestMapping("exportExl")
public ResponseEntity<byte[]> exportExl() throws Exception {
    List<Customer> clist = cs.getList();
    HSSFWorkbook book = new HSSFWorkbook();
    HSSFSheet sheet = book.createSheet("客户信息表");
    sheet.setDefaultColumnWidth(15);
    //表头行创建
    HSSFRow header = sheet.createRow(0);
    header.createCell(0).setCellValue("职工序号");
    header.createCell(1).setCellValue("联系人姓名");
    header.createCell(2).setCellValue("公司名称");
    header.createCell(3).setCellValue("添加时间");
    header.createCell(4).setCellValue("联系电话");
    //clist数据写入单元格
    for (int i = 0; i < clist.size(); i++) {
        Customer cus = clist.get(i);
        HSSFRow row = sheet.createRow(i + 1);
        row.createCell(0).setCellValue(cus.getId());
        row.createCell(1).setCellValue(cus.getCompanyperson());
        row.createCell(2).setCellValue(cus.getComname());
        row.createCell(3).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(cus.getAddtime()));

        row.createCell(4).setCellValue(cus.getComphone());
    }
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    book.write(bos);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentDispositionFormData("attachment", new String("客户列表.xls".getBytes("GBK"), "ISO-8859-1"));
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    return new ResponseEntity<byte[]>(bos.toByteArray(), headers, HttpStatus.OK);

 

 

posted @ 2020-04-03 18:53  钟。意  阅读(2120)  评论(0编辑  收藏  举报