java导出excel

public void infoToExcel(HttpServletResponse response, UserInfoDao userInfoDao ,MyGroupDao groupDao) {
        // 创建HSSFWorkbook对象(excel的文档对象)
        HSSFWorkbook wb = new HSSFWorkbook();
        // 建立新的sheet对象(excel的表单)
        HSSFSheet sheet = wb.createSheet("textMsg");
        // 时间戳转换为日期对象
        TimeStapToDate tsd = new TimeStapToDate();

        HSSFRow row;
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("昵称");
        row.createCell(1).setCellValue("备注名");
        row.createCell(2).setCellValue("性别");
        row.createCell(3).setCellValue("国家");
        row.createCell(4).setCellValue("省份");
        row.createCell(5).setCellValue("城市");
        row.createCell(6).setCellValue("关注时间");
        row.createCell(7).setCellValue("分组名");

        List<UserInfo> list = userInfoDao.findAll();//实体对象的集合

        UserInfo user;
        for (int i = 0; i < list.size(); i++) {
            user = list.get(i);
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(user.getNickName());
            row.createCell(1).setCellValue(user.getRemark());
            row.createCell(2).setCellValue(user.getSex());
            row.createCell(3).setCellValue(user.getCountry());
            row.createCell(4).setCellValue(user.getProvince());
            row.createCell(5).setCellValue(user.getCity());
            String stamp = user.getSubscribeTime()+"";
            row.createCell(6).setCellValue(tsd.timeStampToDateYear(stamp));
            row.createCell(7).setCellValue(groupDao.findById(user.getGroupId()).getGroupName());
        }

        // 输出Excel文件
        OutputStream output;
        try {
            output = response.getOutputStream();
            response.reset();
            response.setHeader("Content-disposition", "attachment; filename=userInfo.xls");
            response.setContentType("application/msexcel");
            wb.write(output);
            output.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值