easyui+springMVC导出excel并压缩文件

easyui页面代码:

 

<a id="exportExcel" href="#" class="easyui-linkbutton" iconCls="icon-excel" οnclick="exportExcel()">导出Excel</a>

  function exportExcel(){
        $('#serachForm').attr('action','<%=request.getcontextPath%>/system/card/exportExcel'));
        $("#serachForm").submit();
   }

springMVC页面代码:

   @SuppressWarnings("deprecation")
    @RequestMapping(value = "exportExcel")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response, CardVo ca) throws IOException {
        @SuppressWarnings("unused")
        ServletOutputStream out = null;
        long time1 = System.currentTimeMillis();
        @SuppressWarnings("unused")
        short height = 22 * 18;
        try {
            response.setHeader("Content-Disposition",
                    "attachment; filename=" + new String("卡号列表导出数据.xls".getBytes("gb2312"), "ISO8859-1"));
        } catch (UnsupportedEncodingException e2) {
            e2.printStackTrace();
        }
        try {
            out = response.getOutputStream();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        List<Card> cardList = cardBiz.getCardList(ca);
        System.out.println(cardList.size());
        // 获取总列数
        // int CountColumnNum = list.size();
        // 创建Excel文档
        
         int rowaccess=50;//内存中缓存记录行数
        
        HSSFWorkbook wb = null;
        HSSFSheet sheet = null;
        HSSFPatriarch patriarch = null;
        ByteArrayOutputStream byteArrayOut1 = null;
        ByteArrayOutputStream byteArrayOut2 = null;
        BufferedImage bi1 = null;
        BufferedImage bi2 = null;
        HSSFClientAnchor anchor1 = null;
        HSSFClientAnchor anchor2 = null;
        @SuppressWarnings("unused")
        FileOutputStream fileOut = null;
        @SuppressWarnings("unused")
        BufferedImage bufferImg = null;
        @SuppressWarnings("unused")
        ByteArrayOutputStream byteArrayOut = null;
        HSSFRow row;
        int sheetIndex = 0;
        int rowIndex = 0;
        
        int count = cardList.size() / 1000 + 1;
        
        int allCount = 0;
        
        Card card = null;
        
        for (int n = 0; n < count; n++) {
            if (allCount % 1000 == 0) {

                // 第一步,创建一个webbook,对应一个Excel文件
                wb = new HSSFWorkbook();

                sheet = null;
                patriarch = null;
                fileOut = null;
                bufferImg = null;
                byteArrayOut = null;

                byteArrayOut1 = null;
                byteArrayOut2 = null;
                bi1 = null;
                bi2 = null;
                anchor1 = null;
                anchor2 = null;
                sheetIndex = 0;
                rowIndex = 0;
            }
            for (int i = 0; i < 1000; i++) {
                
                // 得到要插入的每一条记录
                if (allCount < cardList.size()) {
                    if (i % 334 == 0) {
                        rowIndex = 0;
                        // sheet 对应一个工作页
                        sheet = wb.createSheet("卡号列表" + (sheetIndex + 1));
                        
                        sheet.setZoom(3, 2);//150%显示比例
                        
                        sheet.setColumnWidth(13, 30 * 256);
                        sheet.setColumnWidth(14, 30 * 256);
                        
                        sheet.setColumnWidth(0, (int) (256 * 12));
                        sheet.setColumnWidth(1, (int) (256 * 12));
                        sheet.setColumnWidth(2, (int) (256 * 9));
                        sheet.setColumnWidth(3, (int) (256 * 16));
                        sheet.setColumnWidth(4, (int) (256 * 10));
                        
                        row = sheet.createRow(0); // 下标为0的行开始
                        HSSFCell[] firstcell = new HSSFCell[13];
                        String[] names = new String[13];
                        names[0] = "卡号";
                        names[1] = "密码";
                        names[2] = "状态";
                        names[3] = "订期";
                        names[4] = "用户姓名";
                        
                        for (int j = 0; j < names.length; j++) {
                            firstcell[j] = row.createCell(j);
                            firstcell[j].setCellValue(new HSSFRichTextString(names[j]));
                        }
                        sheetIndex++;
                    }
                    System.out.println("插入的条数:"+allCount);
                    // 创建一行
                    row = sheet.createRow((int) rowIndex + 1);
                    row.setHeight((short) (10 * 256));
                    
                    // 画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
                    patriarch = sheet.createDrawingPatriarch();
                    byteArrayOut1 = new ByteArrayOutputStream();
                    byteArrayOut2 = new ByteArrayOutputStream();
                    try {
                        bi1 = ImageIO.read(new File("D:/Documents/Tencent Files/908367619/FileRecv/0ea70e1e-aa28-4069-a4f6-544811929478.jpg"));
                        ImageIO.write(bi1, "jpg", byteArrayOut1);
                        
                        // anchor主要用于设置图片的属性
                        bi2 = ImageIO.read(new File("D:/Documents/Tencent Files/908367619/FileRecv/053f6461-e94c-4fd7-93cf-44d7244f8a14.jpg"));
                        ImageIO.write(bi2, "jpg", byteArrayOut2);
                        // anchor主要用于设置图片的属性
                        
                        anchor1 = new HSSFClientAnchor(0, 0, 0, 0, (short) 13, (rowIndex+1), (short) 14, (rowIndex + 2));
                        anchor1.setAnchorType(3);
                        anchor2 = new HSSFClientAnchor(0, 0, 0, 0, (short) 14, (rowIndex+1), (short) 15, (rowIndex + 2));
                        anchor2.setAnchorType(3);
                        
                        // 插入图片
                        patriarch.createPicture(anchor1,
                                wb.addPicture(byteArrayOut1.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
                        patriarch.createPicture(anchor2,
                                wb.addPicture(byteArrayOut2.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
                        
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    } finally {
                        try {
                            if (null != byteArrayOut2) {
                                byteArrayOut2.close();
                            }
                            if (null != byteArrayOut1) {
                                byteArrayOut1.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    card = cardList.get(allCount);
                    if (null != card.getNumber()) {
                        HSSFCell xnumber = row.createCell(0);
                        xnumber.setCellValue(card.getNumber());
                    }
                    if (null != card.getPassword()) {
                        HSSFCell xpassword = row.createCell(1);
                        xpassword.setCellValue(card.getPassword());
                    }
                    if (null != card.getStatus()) {
                        HSSFCell xstatus = row.createCell(2);
                        xstatus.setCellValue(card.getStatus() == 0 ? "未激活" : "已激活");
                    }
                    if (null != card.getOrderDate()) {
                        HSSFCell xorderDate = row.createCell(3);
                        xorderDate.setCellValue(card.getOrderDate());
                    }
                    if (card.getName() != null) {
                        HSSFCell xname = row.createCell(4);
                        xname.setCellValue(card.getName());
                    }
                    
                    //针对2007的Excel导出,可以无上限的导出Excel
                    /*if(allCount%rowaccess==0){
                        ((SXSSFSheet)sheet).flushRows();
                    }*/
                    rowIndex++;
                    allCount++;
                }
                
            }
            if (allCount % 1000 == 0 || allCount == cardList.size()) {
                FileOutputStream fout = new FileOutputStream("E:/Customer" + n + ".xls");
                wb.write(fout);
                fout.close();
            }

        }

//---------------压缩----------------------------------------------------------------------------
        
        int BUFFER = 2048;//预设缓冲区成员(位元阵列)为2048元组
        long l = System.currentTimeMillis();//获得当前系统时间的毫秒数
        BufferedInputStream origin = null;//设置输入流
        FileOutputStream dest = new FileOutputStream("F:\\"+l+".zip");//创建一个文件输出流
        ZipOutputStream out1 = new ZipOutputStream(new BufferedOutputStream(dest));//实例化一个ZIP输出流并实例化缓冲区
        File files[] = null;
        try {
            
            byte data[] = new byte[BUFFER];//获得一个字节数组
            File f  = new File("E:/");//获得需压缩的原始文件夹
            files = f.listFiles();//返回一个抽象路径名数组,这些路径名表示此抽象路径名所表示目录中的文件。
            //循环路径名数组,向ZIP中写入
            for (int i = 0; i < files.length; i++) {
                if(files[i].isFile() && files[i].getName().contains(".xls")){
                    FileInputStream fi = new FileInputStream(files[i]);//通过路径数组来创建文件输入流
                    origin = new BufferedInputStream(fi, BUFFER);//实例化一个有缓冲区大小的输入流
                    ZipEntry entry = new ZipEntry(files[i].getName());//通过指定的名称创建ZIP文件
                    out1.putNextEntry(entry);//输出ZIP文件
                    int count1;//设置变量
                    //将输入流中最多BUFFER个数据字节读入字节数组。以整数形式返回实际读取的字节数,若到达末尾没有数据了,返回-1
                    while ((count1 = origin.read(data, 0, BUFFER)) != -1)
                    {
                        out1.write(data, 0, count1);//输出ZIP数据
                    }
                    
                    System.out.println("压缩文件生成...");
                }
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            origin.close();//关闭输入流
            out1.close();//关闭ZIP输出流
        }
   //---------------下载----------------------------------------------------------------------------
        
        // 第六步,弹出下载框让用户下载
     response.setContentType("application/octet-stream");
     response.setContentType("application/OCTET-STREAM;charset=UTF-8");
     response.setHeader("Content-Disposition", "attachment;filename=customer.zip");

        File file = new File("F:\\"+l+".zip");
        FileInputStream fis = null;
        BufferedOutputStream out2 = null;
        try {
            fis = new FileInputStream(file);
            out2 = new BufferedOutputStream(response.getOutputStream());
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1) {
              out2.write(buffer, 0, len);
              out2.flush();
            }
        }catch (Exception e) {
            // TODO: handle exception
        }finally{
            for(int m=0;m<files.length;m++){
                File file2 = files[m];
                if (file2.getName().contains(".xls")) {
                    file2.delete();
                }
            }
            fis.close();
            out2.close();
        }
        long time2 = System.currentTimeMillis();
        System.out.println(time1 - time2);
        System.out.println("-------------------------------------");
    }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值