java实现导出excel表格

项目需要导出数据,保存为excel形式,自己弄了一天,终于摸索出一条可行的方法,现在贴在下面,大家相互学习。

public static String toExcelReport(List<ReportVO> empVOs, HttpServletRequest request) throws Exception
    {
        // 第一步,创建一个webbook,对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("举报表");
        // 第三步,在sheet中添加表头第0,注意老版本poiExcel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        // 第四步,创建单元格,并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("举报人");
        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);
        cell = row.createCell((short) 6);
        cell.setCellValue("处理人");
        cell.setCellStyle(style);
        cell = row.createCell((short) 7);
        cell.setCellValue("处理时间");
        cell.setCellStyle(style);

        // 第五步,写入实体数据 实际应用中这些数据从数据库得到,
//        List list = CreateSimpleExcelToDisk.getStudent();

        for (int i = 0; i < empVOs.size(); i++)
        {
            row = sheet.createRow((int) i + 1);
            ReportVO stu = (ReportVO) empVOs.get(i);
            // 第四步,创建单元格,并设置值
//            row.createCell((short) 0).setCellValue((double) stu.getId());
            row.createCell((short) 0).setCellValue(stu.getEmpName());
            row.createCell((short) 1).setCellValue(stu.getEmpNameReport());
            row.createCell((short) 2).setCellValue(stu.getDateline());

            row.createCell((short) 3).setCellValue(stu.getMm_report_content());
            row.createCell((short) 4).setCellValue(stu.getMsgCont()==null?"":stu.getMsgCont());

            if("0".equals(stu.getIs_use())){
                row.createCell((short) 5).setCellValue("未处理");
            }else {
                row.createCell((short) 5).setCellValue("已处理");
            }
//            row.createCell((short) 7).setCellValue(stu.getDateline());
            row.createCell((short) 6).setCellValue(stu.getManagerName()==null?"":stu.getManagerName());
            if(!StringUtil.isNullOrEmpty(stu.getEnd_dateline())){
                row.createCell((short) 7).setCellValue(stu.getEnd_dateline());
            }else {
                row.createCell((short) 7).setCellValue("");
            }

        }
        // 第六步,将文件存到指定位置
        try
        {
//		注意:这是绝对路径
//            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
//            String fileName = String.valueOf(df.format(new Date()));
//            FileOutputStream fout = new FileOutputStream("D:/jubao_"+fileName+".xls");
//            wb.write(fout);
//            fout.close();
//            return fileName;
//		这个是相对路径,request就是获得项目的相对路径的
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
            String fileName = String.valueOf(df.format(new Date()));
            String path = request.getSession().getServletContext().getRealPath("upload");
            FileOutputStream fout = new FileOutputStream(path+"/jubao_"+fileName+".xls");
            wb.write(fout);
            fout.close();
            return "/jubao_"+fileName+".xls";
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }
重点说明一下几个地方:
1、HttpServletRequest request 是为了获得项目的相对地址,保存文件到项目下面。如果你保存文件到绝对地址,这个可以不用。
2、
 String path = request.getSession().getServletContext().getRealPath("upload");
            FileOutputStream fout = new FileOutputStream(path+"/jubao_"+fileName+".xls");
这个就是保存的相对路径,如果你要保存绝对路径,可以这么写
FileOutputStream fout = new FileOutputStream("D://jubao_"+fileName+".xls");

、------------------------------------
通过以上的方法就可以保存数据到excel文件,怎么通过浏览器下载呢,哈哈,其实很简单,一行代码搞定:
window.location.href = data.url;//这样就可以弹出下载对话框了
注意!data.url 一定是相对路径,绝对路径是下载不了的!!!这就是上面为什么用request保存文件到项目下面的原因!


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值