码迷,mamicode.com
首页 > 其他好文 > 详细

excel导出以流的形式

时间:2019-08-20 18:34:27      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:seconds   log   sys   public   book   get   mil   numbers   mem   

前端后分离,导出excel

@PostMapping("/exportSnByRDC")
    @ApiAuthority(noAuth = true)
    @ApiOperation("导出序列码")
    public void exportSnByRDC(@RequestBody RDCVM vm,HttpServletResponse response) {
        int type = vm.getType();
        log.info("exportSnByRDC type="+type);
            List<HuSnVO> vos = serialNumberService.exportSnByRDC(vm);
            //List<String> shipmentNumbers = vm.getShipmentNumbers();

            OutputStream output = null;
            try {
                SXSSFWorkbook workbook = new SXSSFWorkbook();
                workbook.createSheet("序列码");
                Sheet sheet = workbook.getSheetAt(0);
            /*Sheet sheet1 = workBook.createSheet();
            SXSSFSheet sheet = workBook.createSheet("序列码")*/;
                String []title = {"DeliveryNumber","OperDate","SkuCode",
                    "SkuName","Batch","SapQuantity","QRQuantity","Variance","From","To","SalesOrder"};
                for (int i=0;i<title.length;i++) {
                    sheet.setColumnWidth(i, 252*12+323);//width=12
                }
                // 设置列宽这里12就是excel 里列宽12 i是第几列

                CellStyle style = workbook.createCellStyle();
                style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
                Row row = sheet.createRow(0);
                //声明列对象
                Cell cell = null;
                //创建标题
                for(int i=0;i<title.length;i++){
                    cell = row.createCell(i);
                    cell.setCellValue(title[i]);
                    cell.setCellStyle(style);
                }

                long startTimea = System.currentTimeMillis();
                //创建内容HuSnVO vo : vos
                if(vos.size()>0){
                    for (int i=0;i<vos.size();i++) {
                        row = sheet.createRow(i + 1);
                        row.createCell(0).setCellValue(vos.get(i).getDn());
                        row.createCell(1).setCellValue(vos.get(i).getOperTime());
                        row.createCell(2).setCellValue(vos.get(i).getSkuCode());
                        row.createCell(3).setCellValue(vos.get(i).getSkuName());
                        row.createCell(4).setCellValue(vos.get(i).getBatch());

                        int sapQuan = vos.get(i).getSapQuantity();
                        row.createCell(5).setCellValue(sapQuan);
                        int qrQuan = (int)vos.get(i).getCartonCount();
                        row.createCell(6).setCellValue(qrQuan);

                        row.createCell(7).setCellValue(getPercet(qrQuan,sapQuan));
                        row.createCell(8).setCellValue(vos.get(i).getFrom());
                        row.createCell(9).setCellValue(vos.get(i).getTo());
                        row.createCell(10).setCellValue(vos.get(i).getSalesOrder());
                    }
                }
                //long endTimea = System.currentTimeMillis();
                //float secondsa = (endTimea - startTimea) / 1000F;
                //log.info("写序列码到excel时间" + Float.toString(secondsa) + " seconds.");

                output = response.getOutputStream();
                response.reset();
                response.setHeader("Access-Control-Allow-Origin", "*");
                response.setHeader("Cache-Control","no-cache");
                response.setContentType("application/octet-stream");
                String fileName ="";
                if(StringUtil.isNotBlank(vm.getStartDate())){
                    fileName = vm.getStartDate().substring(0,10)+"-"+vm.getEndDate().substring(0,10) +".xlsx";
                    if(fileName.contains("_")){
                        fileName = fileName.replaceAll("_", "");
                    }
                }else{
                    fileName = DataUtils.getCurrentTimes()+"-SnReport" +".xlsx";
                }

                response.addHeader("Content-Disposition", "attachment;filename="+fileName);
                workbook.write(output);
                output.flush();
                output.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

技术图片

 

excel导出以流的形式

标签:seconds   log   sys   public   book   get   mil   numbers   mem   

原文地址:https://www.cnblogs.com/lyon91/p/11384516.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!