excel导出以流的形式

Posted lyon91

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了excel导出以流的形式相关的知识,希望对你有一定的参考价值。

前端后分离,导出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导出以流的形式的主要内容,如果未能解决你的问题,请参考以下文章

C#以流方式把DataGridView数据导出Excel!

jquery把jqgrid中数据导出excel表格求助

java以流的形式读取html文件,再输出为excel文件

我想用java poi 写个EXCEL导出工具,用啥设计模式写比较好,原因是啥?

使用 Laravel-Excel 和流的方法导出 Excel

使用struts2和poi导出excel文档