码迷,mamicode.com
首页 > 编程语言 > 详细

Java-Excel写与读

时间:2018-11-08 10:55:14      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:int   encode   excel   mat   gets   for   print   i++   taf   

 public HSSFWorkbook generateSheet(HSSFWorkbook wb, String sheetName, List<StaffDeclareInfoDO> list) {
    
        /*** 总记录数 **/
        int listSize = list == null ? 0 : list.size();
        
        HSSFSheet sheet = wb.createSheet(sheetName);
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell((short) 0);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue("序号");
        cell = row.createCell((short) 1);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue("姓名");
        cell = row.createCell((short) 2);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue("性别");
        cell = row.createCell((short) 3);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue("手机");
        cell = row.createCell((short) 4);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue("身份证号");
        cell = row.createCell((short) 5);
       
        
        
        for (int i = 0; i < listSize; i++) {
            row = sheet.createRow(i + 1);
            StaffDeclareInfoDO data = list.get(i);
            cell = row.createCell((short) 0);
            cell.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell.setCellValue(data.getId());
            cell = row.createCell((short) 1);
            cell.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell.setCellValue(data.getName());
            cell = row.createCell((short) 2);
            cell.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell.setCellValue(data.getSex());
            cell = row.createCell((short) 3);
            cell.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell.setCellValue(data.getMobile());
            cell = row.createCell((short) 4);
            cell.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell.setCellValue(data.getCertificate());
        }
        
        return wb;
        
    }


 public void export(HSSFWorkbook wb, HttpServletResponse response) {
    
        try {
           
            String tip = DateUtil.format(new Date(), DateUtil.shortFormat) + "";
            String name = "xxx" + tip;
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(name, "utf-8") + ".xls");
            OutputStream out = response.getOutputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            wb.write(baos);
            byte[] xlsBytes = baos.toByteArray();
            out.write(xlsBytes);
            out.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        
    }

 

 

Java-Excel写与读

标签:int   encode   excel   mat   gets   for   print   i++   taf   

原文地址:https://www.cnblogs.com/qcxdoit/p/9927560.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!