使用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);

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM