Excel Java

 /**
     * 导出excel
     * @param pbCode
     * @return
     */
    @RequestMapping("exportTrQrcode")
    @ResponseBody
    public ResultBase exportExcel(String pbCode) {
        //参数初始化
        startRow = -1;
        startCol = 0;
        patriarch = null;
        totalSheet = 0;
        sheet = null;

        ResultBase resultBase;
        FileOutputStream fileOut = null;
        BufferedImage bufferImg = null;
        try {
            if (StringUtils.isEmpty(pbCode)) {
                resultBase = new ResultBase("1014");
            } else if (batchService.getProBatchByPbCode(pbCode) == null) {
                resultBase = new ResultBase("1015");
            } else {
                List<TbTraceQrcode> qrcodeList = qrcodeService.getListByPbCode(pbCode);
                File model = new File(FilePathConsts.rootPath
                        + "file/excel/export_trace_qrcode.xls");
                FileInputStream fis = new FileInputStream(model);
                POIFSFileSystem fs = new POIFSFileSystem(fis);
                HSSFWorkbook wb = new HSSFWorkbook(fs);

                //HSSFSheet sheet = wb.createSheet("qrcode picture");
                sheet = wb.getSheetAt(0);
                //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
                patriarch = sheet.createDrawingPatriarch();

                int width = sheet.getColumnWidth(0);
                int height = sheet.getRow(0).getHeight();
                totalSheet = 1;
                //溯源码图片生成  顺序为:溯源码-其对应的活动码们-下一个溯源码...
                for (TbTraceQrcode qrcode : qrcodeList) {
                    String imagePath = FilePathConsts.trace_qrcode_image + qrcode.getTqId() +".jpeg";
                    OrCodeUtils.qrCode(qrcode.getTqQrcode(), imagePath);

                    ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
                    bufferImg = ImageIO.read(new File(imagePath));
                    ImageIO.write(bufferImg, "jpg", byteArrayOut);

                    insertPicture(width, height, wb, byteArrayOut);
                   /* HSSFClientAnchor anchor;
                    //anchor主要用于设置图片的属性

                    if (startCol != 0 && startCol < 5) {
                        anchor = new HSSFClientAnchor(0, 0, 1023 , 255,(short) startCol,  startRow, (short) startCol, startRow);
                        sheet.setColumnWidth(startCol, width);
                        //sheet.getRow(startRow).setHeight((short)height);
                        startCol ++;
                    } else {
                        startCol = 0;
                        startRow ++;
                        anchor = new HSSFClientAnchor(0, 0, 1023, 255,(short)startCol, startRow, (short) startCol, startRow);
                        sheet.setColumnWidth(startCol, width);
                        if (sheet.getRow(startRow) == null) {
                            sheet.createRow(startRow);
                        }
                        sheet.getRow(startRow).setHeight((short)height);
                        startCol ++;
                    }

                    anchor.setAnchorType(3);
                    //插入图片
                    patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

                    //每页五行五列
                    if (startRow == 5 && startCol == 5) {
                        sheet = wb.createSheet();
                        patriarch = sheet.createDrawingPatriarch();
                        startRow = -1;
                        startCol = 0;
                    }*/

                    //startRow ++;
                    //活动码
                    List<TbDrawQrcode> drawQrcodeList = drawQrcodeService.getDrQrcodeListByTqQrcode(qrcode.getTqQrcode());
                    //活动码图片生成
                    for (TbDrawQrcode tbDrawQrcode : drawQrcodeList) {
                        String imagePath2 = FilePathConsts.trace_qrcode_image + tbDrawQrcode.getDqId() +".jpeg";
                        OrCodeUtils.qrCode(tbDrawQrcode.getDqQrcode(), imagePath2);

                        ByteArrayOutputStream byteArrayOut2 = new ByteArrayOutputStream();
                        bufferImg = ImageIO.read(new File(imagePath2));
                        ImageIO.write(bufferImg, "jpg", byteArrayOut2);

                        insertPicture(width, height, wb, byteArrayOut2);
                        /*//anchor主要用于设置图片的属性
                        HSSFClientAnchor anchor2;
                        if (startCol != 0 && startCol < 5) {
                            anchor2 = new HSSFClientAnchor(0, 0, 1023 , 255,(short) startCol,  startRow, (short) startCol, startRow);
                            sheet.setColumnWidth(startCol, width);
                            //sheet.getRow(startRow).setHeight((short)height);
                            startCol ++;
                        } else {
                            startCol = 0;
                            startRow ++;
                            anchor2 = new HSSFClientAnchor(0, 0, 1023, 255,(short)startCol, startRow, (short) startCol, startRow);
                            sheet.setColumnWidth(startCol, width);
                            if (sheet.getRow(startRow) == null) {
                                sheet.createRow(startRow);
                            }
                            sheet.getRow(startRow).setHeight((short)height);
                            startCol ++;
                        }
                        anchor2.setAnchorType(3);
                        //插入图片
                        patriarch.createPicture(anchor2, wb.addPicture(byteArrayOut2.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

                        //每页五行五列
                        if (startRow == 5 && startCol == 5) {
                            sheet = wb.createSheet();
                            patriarch = sheet.createDrawingPatriarch();
                            startRow = -1;
                            startCol = 0;
                        }*/
                        //startRow ++;
                    }
                }

                String outFileName = UUIDUtils.getUniquekey() + ".xls";
                fileOut = new FileOutputStream(FilePathConsts.excel_temp + outFileName);
                /*fileOut = new FileOutputStream("D:/测试Excel.xls");*/
                wb.write(fileOut);
                fileOut.flush();
                fileOut.close();
                resultBase = new ResultBase("1000", outFileName);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            resultBase = new ResultBase("1003");
            e.printStackTrace();
        } finally {
            if(fileOut != null){
                try {
                    fileOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return resultBase;
    }

    /**
     * EXCEL插入图片
     * @param width
     * @param height
     * @param wb
     * @param byteArrayOut
     */
    private void insertPicture(int width, int height, HSSFWorkbook wb, ByteArrayOutputStream byteArrayOut) {
        try {
            HSSFClientAnchor anchor;
            //anchor主要用于设置图片的属性
            if (startCol != 0 && startCol < 5) {
                anchor = new HSSFClientAnchor(0, 0, 1023 , 255,(short) startCol,  startRow, (short) startCol, startRow);
                sheet.setColumnWidth(startCol, width);
                //sheet.getRow(startRow).setHeight((short)height);
                startCol ++;
            } else {
                startCol = 0;
                startRow ++;
                anchor = new HSSFClientAnchor(0, 0, 1023, 255,(short)startCol, startRow, (short) startCol, startRow);
                sheet.setColumnWidth(startCol, width);
                if (sheet.getRow(startRow) == null) {
                    sheet.createRow(startRow);
                }
                sheet.getRow(startRow).setHeight((short)height);
                startCol ++;
            }

            anchor.setAnchorType(3);
            //插入图片
            patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

            //每页五行五列
            if (startRow == 5 && startCol == 5) {
                sheet = wb.createSheet("Sheet" + (++totalSheet));
                patriarch = sheet.createDrawingPatriarch();
                startRow = -1;
                startCol = 0;
            }
        } catch (Exception e) {
            throw e;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值