数据导出,文件压缩,文件下载

数据导出

 HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
   /**
     * 合规提取-下载封装
     * @param transOfflineist 数据
     * @param wb HSSFWorkbook
     * @param sheetName sheet名称
     * @param filedMap  数据列名
     */
    public void  exportDetailed(List<Map> list, HSSFWorkbook wb, String sheetName,Map<String,String> filedMap) {
        //生成excel
        HSSFSheet sheet = wb.createSheet(sheetName);
        //设置样式
        HSSFCellStyle style = (HSSFCellStyle) wb.createCellStyle();
        HSSFCellStyle style1 = (HSSFCellStyle) wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style1.setWrapText(true);
        style1.setAlignment(HorizontalAlignment.CENTER);
        //设置边框
        style.setBorderBottom(BorderStyle.THIN); //下边框
        style.setBorderLeft(BorderStyle.THIN);//左边框
        style.setBorderTop(BorderStyle.THIN);//上边框
        style.setBorderRight(BorderStyle.THIN);//右边框
        style1.setBorderBottom(BorderStyle.THIN); //下边框
        style1.setBorderLeft(BorderStyle.THIN);//左边框
        style1.setBorderTop(BorderStyle.THIN);//上边框
        style1.setBorderRight(BorderStyle.THIN);//右边框
        //字体
        HSSFFont font1 = (HSSFFont) wb.createFont();
        font1.setFontName("微软雅黑");
        font1.setBold(true);//粗体显示
        font1.setFontHeightInPoints((short) 11);
        style1.setFont(font1);
        HSSFFont font2 = (HSSFFont) wb.createFont();
        font2.setFontName("微软雅黑");
        font2.setFontHeightInPoints((short) 11);
        style.setFont(font2);
//        style2.setFont(font2);
        HSSFRow row0 = sheet.createRow(0);
        Iterator<Map.Entry<String, String>> filed = filedMap.entrySet().iterator();
        int filedIndex = 0;
        while (filed.hasNext()) {
            Map.Entry<String, String> entry = filed.next();
            String value = entry.getValue();
            HSSFCell cellcr = row0.createCell(filedIndex);
            cellcr.setCellValue(value);
            cellcr.setCellStyle(style1);
            filedIndex++;
        }

        if (null != list && !list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                Map hg004TransOffline = transOfflineist.get(i);
                HSSFRow row = sheet.createRow(i+1 );
                filed = filedMap.entrySet().iterator();
                int filedValueIndex = 0;
                while (filed.hasNext()) {
                    Map.Entry<String, String> entry = filed.next();
                    String key = entry.getKey();
                    String cellValue = StringUtil.isEmpty(hg004TransOffline.get(key)) ?"":String.valueOf(hg004TransOffline.get(key));
                    HSSFCell crCell = row.createCell(filedValueIndex);
                    crCell.setCellValue(cellValue);
                    crCell.setCellStyle(style);
                    filedValueIndex++;
                }
            }
        }
        //自适应列宽
        sheet.setColumnWidth(0, 8 * 2 * 256);
        sheet.setColumnWidth(1, 14 * 2 * 256);
        sheet.setColumnWidth(2, 14 * 2 * 256);
        sheet.setColumnWidth(3, 14 * 2 * 256);
        sheet.setColumnWidth(4, 14 * 2 * 256);
        sheet.setColumnWidth(5, 14 * 2 * 256);
        sheet.setColumnWidth(6, 14 * 2 * 256);
        /* sheet.setColumnWidth(4, 7 * 2 * 256);*/
        sheet.setColumnWidth(7, 10 * 2 * 256);
        sheet.setColumnWidth(8, 14 * 2 * 256);
        sheet.setColumnWidth(9, 14 * 2 * 256);
        sheet.setColumnWidth(10, 14 * 2 * 256);
        sheet.setColumnWidth(11, 14 * 2 * 256);
        sheet.setColumnWidth(12, 9 * 2 * 256);
        sheet.setColumnWidth(13, 9 * 2 * 256);
        sheet.setColumnWidth(14, 9 * 2 * 256);
        sheet.setColumnWidth(15, 9* 2 * 256);

    }

// 写入文件
  String fileName =  "test.xls";
  File gile = new File(file + "/" + fileName);
  OutputStream outputStream = new FileOutputStream(gile);
  hssfWorkbook .write(outputStream);
  outputStream.close();

文件压缩:

 /**
     * @param zipFileName // 目的地Zip文件
     * @param sourceFileName //源文件(带压缩的文件或文件夹)
     * @throws Exception
     */
    public static void compressToZip(String zipFileName,String sourceFileName) throws Exception {
        log.info("压缩中....");
        //创建zip输出流
        ZipOutputStream out = new ZipOutputStream( new FileOutputStream(zipFileName));

        //创建缓冲输出流
        BufferedOutputStream bos = new BufferedOutputStream(out);

        File sourceFile = new File(sourceFileName);

        //调用函数
        compress(out,bos,sourceFile,sourceFile.getName());

        bos.close();
        out.close();
        log.info("压缩完成");

    }

    public static void compress(ZipOutputStream out,BufferedOutputStream bos,File sourceFile,String base) throws Exception {
        //如果路径为目录(文件夹)
        if(sourceFile.isDirectory())  {
            //取出文件夹中的文件(或子文件夹)
            File[] flist = sourceFile.listFiles();

            if(flist.length==0)  {//如果文件夹为空,则只需在目的地zip文件中写入一个目录进入点
                log.info("压缩文件:{}",base+"/");
                out.putNextEntry(  new ZipEntry(base+"/") );
            }
            else {//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
                for(int i=0;i<flist.length;i++)
                {
                    compress(out,bos,flist[i],base+"/"+flist[i].getName());
                }
            }
        }  else {//如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
            out.putNextEntry( new ZipEntry(base) );
            FileInputStream fos = new FileInputStream(sourceFile);
            BufferedInputStream bis = new BufferedInputStream(fos);

            System.out.println(base);
            int len;
            byte[] buffer = new byte[1024 * 10];
            while ((len = bis.read(buffer, 0, buffer.length)) != -1) {
                out.write(buffer, 0, len);
                out.flush();
            }
            bis.close();
            fos.close();
        }
    }

        /**
         * 删除文件夹
         *
         * @param dirPath
         * @return
         */
        public static void deleteDir(File dirPath) {
            log.info(dirPath.getPath());
            if (!dirPath.exists()) {
             log.info("删除的文件夹不存在");
            }
            File[] files = dirPath.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        //递归直到目录下没有文件
                        deleteDir(file);
                    } else {
                        //删除
                        file.delete();
                    }
                }
            }
            //删除
            dirPath.delete();
        }

zip下载

 try {

            //响应头的设置
            response.reset();
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/zip");// 定义输出类型
            response.setHeader("Content-Disposition",
                    "attachment; filename=" + new String(downloadName.getBytes("UTF-8"), "ISO8859-1"));

            FileInputStream fis = new FileInputStream(zipfile);
            BufferedInputStream buff = new BufferedInputStream(fis);
            byte[] b = new byte[1024];// 相当于我们的缓存
            long k = 0;// 该值用于计算当前实际下载了多少字节
            OutputStream myout = response.getOutputStream();// 从response对象中得到输出流,准备下载
            // 开始循环下载
            while (k < zipfile.length()) {
                int j = buff.read(b, 0, 1024);
                k += j;
                myout.write(b, 0, j);
            }
            myout.flush();
            buff.close();
        } catch (Exception e) {
            log.info("下载异常{}",e);
        }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值