详细讲解 poi 导出excel

response.setContentType("octets/stream");
        response.addHeader("Content-Disposition","attachment;filename=test.xls");
// 第一步,创建一个webbook,对应一个Excel文件  
        HSSFWorkbook wb = new HSSFWorkbook();  
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet  
        HSSFSheet sheet = wb.createSheet("飞DAY调查结果");  
     // 第四步,创建单元格,并设置值表头 设置表头居中  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
        //创建第一行
        HSSFRow row = sheet.createRow(0);
        //创建第一行的第一个单元格
        HSSFCell cells= row.createCell(0);
        //设置单元格的值
        cells.setCellValue("飞DAY调查结果");
        cells.setCellStyle(style);
        //合并单元格 0,0,0,3 第一行开始,第一行结束,第一列开始,3表示多少列结束
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
        //创建标题栏
        row = sheet.createRow(1);
        HSSFCell cell = row.createCell(0);  
        cell.setCellValue("学号");  
        cell.setCellStyle(style);  
        cell = row.createCell(1);  
        cell.setCellValue("姓名");  
        cell.setCellStyle(style);  
        cell = row.createCell(2);  
        cell.setCellValue("年龄");  
        cell.setCellStyle(style);  
        cell = row.createCell(3);  
        cell.setCellValue("生日");  
        cell.setCellStyle(style);  
  
        // 第五步,写入实体数据 实际应用中这些数据从数据库得到,  
        List<Student> list = new ArrayList<Student>();
        list.add(new Student(1, "张三", 20, new Date()));
        list.add(new Student(1, "张三", 20, new Date()));
        list.add(new Student(1, "张三", 20, new Date()));
        list.add(new Student(1, "张三", 20, new Date()));
        list.add(new Student(1, "张三", 20, new Date()));
        for (int i = 0; i < list.size(); i++)  
        {  
            row = sheet.createRow((int) i + 1);  
            Student stu = (Student) list.get(i);  
            // 第四步,创建单元格,并设置值  
            HSSFCell cell1=row.createCell(0);
            HSSFCell cell2=row.createCell(1);
            HSSFCell cell3=row.createCell(2);
            cell1.setCellStyle(style);
            cell1.setCellValue((double) stu.getId());
            cell2.setCellStyle(style);
            cell2.setCellValue(stu.getName());
            cell3.setCellStyle(style);
            cell3.setCellValue((double) stu.getAge());
            cell = row.createCell(3);  
            cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu  
                    .getBirth()));
            cell.setCellStyle(style);
        }  
        // 第六步,将文件存到指定位置  
        try  
        {  
//Java 服务器端
            wb.write(response.getOutputStream());  

//客户端版
FileOutputStream os = new FileOutputStream("D:\\report\\workbook.xls");  
wb.write(os);  
os.close(); 

        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }finally{
       
        }  
return null;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值