java实现excel表格导出

这里是一个小demo!

        int pageNum = 1;
        int pageSize = 2;
        int reportStatus = 0;
        String infoNum = "";
        //第一步,创建一个webbook,对应一个excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //第二步,在webbook中添加一个sheet,对应excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("举报列表");
        //第三步,在sheet中添加表头第0行,
        HSSFRow row = sheet.createRow(0);
        //第四步,创建单元格,并设置表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
//        style.setAlignment();  创建一个居中格式
        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("案例id");
        cell.setCellStyle(style);

        cell = row.createCell((short) 1);
        cell.setCellValue("标题");
        cell.setCellStyle(style);

        cell = row.createCell((short) 2);
        cell.setCellValue("联系方式");
        cell.setCellStyle(style);

        cell = row.createCell((short) 3);
        cell.setCellValue("举报内容");
        cell.setCellStyle(style);

        cell = row.createCell((short) 4);
        cell.setCellValue("举报时间");
        cell.setCellStyle(style);

        cell = row.createCell((short) 5);
        cell.setCellValue("案例状态");
        cell.setCellStyle(style);


        Map<String,Object> paramMap = new HashMap<String,Object>();

        //第五步,写入实体数据,
        List<FundraisingReportAndOperation> reportByPageList = manageFundraisingReportBiz.getReportByPage(pageNum, pageSize, infoNum, reportStatus);
        FundraisingReportAndOperation r = null;
        for (int i = 0; i < reportByPageList.size(); i++) {
            row = sheet.createRow(i+1);
            r=reportByPageList.get(i);
            //创建单元格并设置值
            row.createCell(0).setCellValue(r.getInfoNum());
            row.createCell(1).setCellValue(r.getTitle());
            row.createCell(2).setCellValue(r.getMobile());
            row.createCell(3).setCellValue(r.getContent());
            row.createCell(4).setCellValue(r.getCreateTime());
            row.createCell(5).setCellValue(r.getReportStatusName());
        }
        //第六步将文件存到指定位置
        Map<String, Object> retMap = new HashMap<String, Object>();
        String resultStr = null;
        File file=new File("/Users/apple/Downloads/举报信息.xls");
        if(!file.exists())
        {
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        FileOutputStream fout = null;
        try {
            fout = new FileOutputStream(file);
            wb.write(fout);
            fout.close();
            System.out.println("导出成功");
        } catch (IOException e1) {
            e1.printStackTrace();
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值