POI实现文件导入导出

一、后台部分:将查询到的数据集合转为可下载的ResponseEntity<byte[]>,

1、首先构建一个HSSFWorkbook进行excel基本信息配置,如文档信息,摘要信息等

2、设置宽度和表头

3、遍历数据,将数据填充到excel中

4、设置下载请求的文件名编码

public static ResponseEntity<byte[]> employee2Excel(List<duty> dutys) {
        //1. 创建一个 Excel 文档
        HSSFWorkbook workbook = new HSSFWorkbook();
        //2. 创建文档摘要
        workbook.createInformationProperties();
        //3. 获取并配置文档信息
        DocumentSummaryInformation docInfo = workbook.getDocumentSummaryInformation();
        //文档类别
        docInfo.setCategory("值班信息");
        //文档管理员
        docInfo.setManager("dong");
        //设置公司信息
        docInfo.setCompany("www.javaboy.org");
        //4. 获取文档摘要信息
        SummaryInformation summInfo = workbook.getSummaryInformation();
        //文档标题
        summInfo.setTitle("值班信息表");
        //文档作者
        summInfo.setAuthor("dong");
        // 文档备注
        summInfo.setComments("本文档由 javaboy 提供");
        //5. 创建样式
        //创建标题行的样式
        HSSFCellStyle headerStyle = workbook.createCellStyle();
        //设置标题行单元格背景色
        headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        HSSFCellStyle dateCellStyle = workbook.createCellStyle();
        //设置日期格式
        dateCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
        HSSFSheet sheet = workbook.createSheet("员工信息表");
        //设置列的宽度
        sheet.setColumnWidth(0, 12 * 256);
        sheet.setColumnWidth(1, 12 * 256);
        sheet.setColumnWidth(2, 10 * 256);
        //6. 创建标题行
        HSSFRow r0 = sheet.createRow(0);
        HSSFCell c0 = r0.createCell(0);
        c0.setCellValue("值班日期");
        c0.setCellStyle(headerStyle);
        HSSFCell c1 = r0.createCell(1);
        c1.setCellStyle(headerStyle);
        c1.setCellValue("姓名");
        HSSFCell c2 = r0.createCell(2);
        c2.setCellStyle(headerStyle);
        c2.setCellValue("电话");
        //6、装数据
        for (int i = 0; i < dutys.size(); i++) {
            duty duty = dutys.get(i);
            HSSFRow row = sheet.createRow(i + 1);
            //设置日期格式
            HSSFCell cell0 = row.createCell(0);
            cell0.setCellStyle(dateCellStyle);
            cell0.setCellValue(duty.getDutydate());
            row.createCell(1).setCellValue(duty.getEmp_name());
            row.createCell(2).setCellValue(duty.getEmp_phone());
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        HttpHeaders headers = new HttpHeaders();
        try {
            //设置下载请求的文件名、编码
            headers.setContentDispositionFormData("attachment", new String("值班表.xls".getBytes("UTF-8"), "ISO-8859-1"));
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            workbook.write(baos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ResponseEntity<byte[]>(baos.toByteArray(), headers, HttpStatus.CREATED);

}

 二、前台部分

当点击导出按钮时,执行如下代码,发起请求下载文件

download:function (){
      window.open(baseUrl+"/duty/exportDuty","_parent");
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值