Java-Excel写与读

Posted 熊猫儿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java-Excel写与读相关的知识,希望对你有一定的参考价值。

 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写与读的主要内容,如果未能解决你的问题,请参考以下文章

错题集06

JAVA-Excel导出

python目录判断与创建

transient与序列化

transient与序列化

2 尽量不要写与平台相关的代码