JAVA Excle报表

 //excel 报表
    @ResponseBody
    @RequestMapping("/excel")
    public Object excel() throws IOException {

        List<Users> list = userService.getAllUsers();
        //第一步,创建一个workbook对应一个excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        //第二部,在workbook中创建一个sheet对应excel中的sheet
        HSSFSheet sheet = workbook.createSheet("用户表");
        //第三部,在sheet表中添加表头第0行,老版本的poi对sheet的行列有限制
        HSSFRow row = sheet.createRow(0);
        //第四步,创建单元格,设置表头
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("用户昵称");
        cell = row.createCell(1);
        cell.setCellValue("用户姓名");
        cell = row.createCell(2);
        cell.setCellValue("用户年龄");
        cell = row.createCell(3);
        cell.setCellValue("用户状态");
        cell = row.createCell(4);
        cell.setCellValue("所属部门");
       /* cell = row.createCell(4);
        cell.setCellValue("用户姓名");
        cell = row.createCell(5);
        cell.setCellValue("用户姓名");
*/
        for (int i = 0; i < list.size(); i++) {
            HSSFRow row1 = sheet.createRow(i + 1);
            Users users1 = list.get(i);
            //创建单元格设值
            row1.createCell(0).setCellValue(users1.getUserName());
            row1.createCell(1).setCellValue(users1.getUserNickname());
            row1.createCell(2).setCellValue(users1.getUserAge());
            row1.createCell(3).setCellValue(users1.getUserStatus());
            row1.createCell(4).setCellValue(users1.getDeptName());
        }
        //将文件保存到指定的位置
        File file=File.createTempFile("orderExcel",".xls");
        file.deleteOnExit();
        try {

            FileOutputStream fos = new FileOutputStream(file);
            workbook.write(fos);
            System.out.println(file);
            System.out.println("写入成功");
            fos.close();
            return 1;
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值