基于EasyPOI实现下载excel以及图片,并打包成zip下载至本地,excel中可点击超链接至下载的图片

首先上maven依赖

       <dependency>
			<groupId>cn.afterturn</groupId>
			<artifactId>easypoi-base</artifactId>
			<version>3.2.0</version>
		</dependency>
		<dependency>
			<groupId>cn.afterturn</groupId>
			<artifactId>easypoi-web</artifactId>
			<version>3.2.0</version>
		</dependency>
		<dependency>
			<groupId>cn.afterturn</groupId>
			<artifactId>easypoi-annotation</artifactId>
			<version>3.2.0</version>
		</dependency>
根据自己的业务需求来建立数据实体(QxwhExcelEntity),创建数据集合
       List<QxwhExcelEntity> qxwhList = XXXX;//数据实体集合
       List<QxwhExcelEntity> fzList = new ArrayList<QxwhExcelEntity>(qxwhList);//复制一份数据实体集合
       for (QxwhExcelEntity qxwhExcelEntity : qxwhList) {
     
            params.put("ID", qxwhExcelEntity.getID());
            List<t_mx_attachment> fjidLists = taskService.getFJID(params);
            if (fjidLists.size()>0){
     
                qxwhExcelEntity.setFujian(qxwhExcelEntity.getID());//为excel中的字段设置超链接地址
            }
        }
设置拦截器,用于在生成的excel中为字段添加超链接
import cn.afterturn.easypoi.handler.impl.ExcelDataHandlerDefaultImpl;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Hyperlink;
public class ExcelDataHandler extends ExcelDataHandlerDefaultImpl<QxwhExcelEntity> {
     
    @Override
    public Hyperlink getHyperlink(CreationHelper creationHelper, QxwhExcelEntity obj, String name, Object value) {
     
        Hyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.FILE);
        hyperlink.setLabel(name);
        hyperlink.setAddress((String) value);
        return hyperlink;
    }
}
创建一个excel导出对象
        ExportParams exportParams = new ExportParams();
        exportParams.setDataHandler(new ExcelDataHandler());//使用拦截器
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams,
                QxwhExcelEntity.class, qxwhList);//不知为何,此步操作后  list会清空,由于后期会用到,于是使用比较笨的方法,先前复制了一份
        Sheet sheet = workbook.getSheetAt(0);
        autoSizeColumns(sheet, 45);//设置excel的段落 宽高样式
构建一个zip流对象 并指定实体
        OutputStream out = response.getOutputStream();
        ZipOutputStream zip = new ZipOutputStream(out);
        ZipEntry entry = new ZipEntry("下载excel" + sdf.format(new Date()) + ".xls");
        zip.putNextEntry(entry);
以下为业务逻辑,可参考
        String qxid="";
        List<QxwhExcelEntity> qxList = taskService.getQXInfo(ids);
        for (QxwhExcelEntity qxwhExcelEntity : qxList) {
     
            for(Row row : sheet) {
     
                for(Cell cell : row) {
     
                        if(cell.getRichStringCellValue().getString().trim().equals(qxwhExcelEntity.getQXSB())){
     
                            rowNum = row.getRowNum();
                            qxid=qxwhExcelEntity.getID();
                            params.put("ID", qxid);
                            List<t_mx_attachment> fjidList = taskService.getFJID(params);
                            Row newRow = sheet.getRow(rowNum);
                            Cell newCell = newRow.getCell(45);
                            if (fjidList.size() > 0) {
     
                                newCell.setCellValue("点击查看");
                            }else{
     
                                newCell.setCellValue("");
                            }
                            CellStyle cellStyle = workbook.createCellStyle();
                            Font font = workbook.createFont();
                            font.setUnderline(Font.U_SINGLE);
                            font.setColor(IndexedColors.BLUE.getIndex());
                            cellStyle.setFont(font);
                            newCell.setCellStyle(cellStyle);
                        }
                }
            }
        }
        workbook.write(zip);//写excel
            for (Object id : ids) {
     
            params.clear();
            params.put("ID", id.toString());
            List<t_mx_attachment> fjidList = taskService.getFJID(params);
            for (t_mx_attachment t_mx_attachment : fjidList) {
     
                //图片流
                //获取sftp对象
                ChannelSftp sftp = SftpUtils.getChannelSftp(host, port, username, password);
                String fileName = sftpPath + t_mx_attachment.getAttachmentId() + File.separator + t_mx_attachment.getAttName();
                //从sftp中获取图片流
                ByteArrayOutputStream swapStream = SftpUtils.getOutputStreamFromFtp(sftp, fileName);
                ZipEntry image = new ZipEntry(id + File.separator + t_mx_attachment.getAttName());
                zip.putNextEntry(image);
                if (swapStream == null) {
     
                    System.out.println("流为空");
                }
                ByteArrayInputStream inputStream = new ByteArrayInputStream(swapStream.toByteArray());
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = inputStream.read(buffer)) > 0) {
     
                    zip.write(buffer, 0, len);
                }
                inputStream.close();
            }
        }
        zip.flush();
        zip.close();
封装的方法
    private void autoSizeColumns(Sheet sheet, int columnNumber) {
     
        for (int i = 0; i < columnNumber; i++) {
     
            sheet.autoSizeColumn(i, true);
            int newWidth = sheet.getColumnWidth(i) + sheet.getColumnWidth(i);
            sheet.setColumnWidth(i, newWidth);

        }
    }

你可能感兴趣的:(技术)