SpringBoot+Vue Poi下载和上传

1 篇文章 0 订阅
1 篇文章 0 订阅

这个是我封装的Poi写数据的操作

 public static void WriteExcel(HttpServletResponse response, String sheetName, ArrayList<ArrayList>arrayLists, ArrayList<String> TableName){
        Workbook workBook = new XSSFWorkbook();
        Sheet sheet = workBook.createSheet(sheetName);

        Font font = workBook.createFont();
        font.setFontHeightInPoints((short)16);
        font.setFontName("黑体");
        font.setItalic(true);
        font.setStrikeout(true);

        CellStyle cellStyle = workBook.createCellStyle();
        cellStyle.setAlignment(CENTER);
        cellStyle.setFont(font);

        sheet.addMergedRegion(new CellRangeAddress(
                0, //第一行(从0开始)
                0, //最后一行(从0开始)
                0, //第一列(从0开始)
                arrayLists.get(1).size()-1 //最后一列(从0开始)
        ));
        sheet.createRow(0).createCell(0).setCellValue("学生违纪表");
        Row row = sheet.createRow(1);
        row.setRowStyle(cellStyle);
        for (int i = 0; i < TableName.size(); i++) {
            row.createCell(i).setCellValue(TableName.get(i)); }
        for (int i = 2; i < arrayLists.size()+2; i++) {
            int U=i-2;
            Row row1 = sheet.createRow(i);
            for (int j = 0; j <arrayLists.get(U).size(); j++) {
                Cell cell = row1.createCell(j);
                switch (arrayLists.get(U).get(j).getClass().getSimpleName()){
                    case "String":cell.setCellValue((String)arrayLists.get(U).get(j));
                                default:cell.setCellValue((String) arrayLists.get(U).get(j)); } } }

        response.setContentType("application/octet-stream");
        //默认Excel名称
        response.setHeader("Content-Disposition", "attachment;fileName="+"test.xls");

        try {
            response.flushBuffer();
            workBook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

业务代码

 /*查询日期以内的数据
    * */
    public  void GetDataDate(HttpServletResponse response,Object MaxDate, Object MinDate){
        List<ViolationTable> violationTables = violationTableMapper.selectList(new QueryWrapper<ViolationTable>().ge("_time", MinDate).and(Qw -> (Qw.le("_time", MaxDate))));
        ArrayList<ArrayList> arrayLists = new ArrayList<>();
        for (ViolationTable violationTable : violationTables) {
            ArrayList<String> violationTables1 = new ArrayList<>();
           violationTables1.add(violationTable.getStudent_number());
            violationTables1.add(violationTable.getStudent_name());
            if(violationTable.getStudent_sex()==0){
                violationTables1.add("女");
            }else {violationTables1.add("男");}
            violationTables1.add(violationTable.getStudent_class());
            violationTables1.add(String.valueOf(violationTable.getBe_late()));
            violationTables1.add(String.valueOf( violationTable.getTruant() ));
            violationTables1.add(String.valueOf(violationTable.getLeave_early()));
            violationTables1.add(String.valueOf(violationTable.get_time()));
            arrayLists.add(violationTables1);
        }
            poi.WriteExcel(response,new DateTool().GetCurrentTime(), arrayLists, new StringTool().StringMore("学号", "姓名", "性别", "班级", "迟到", "旷课", "早退","日期"));
    }

接口

@ResponseBody
    @PostMapping("/download")
    public void   OutFile(@RequestBody Map<Object,String> GetMap, HttpServletResponse response) throws IOException {
        System.out.println(GetMap.get("EndTime") + GetMap.get("StartingTime"));
         violationTableService.GetDataDate(response,GetMap.get("EndTime"), GetMap.get("StartingTime"));

    }

接口

@ResponseBody
    @PostMapping("/download")
    public void   OutFile(@RequestBody Map<Object,String> GetMap, HttpServletResponse response) throws IOException {
        System.out.println(GetMap.get("EndTime") + GetMap.get("StartingTime"));
         violationTableService.GetDataDate(response,GetMap.get("EndTime"), GetMap.get("StartingTime"));

    }

Vue前端代码

download(){
      this.axios.post('/download',{StartingTime:this.value[0],EndTime:this.value[1]},{
        responseType: 'blob'
      }).then(res=>{
        let url = window.URL.createObjectURL(new Blob([res.data]));
        let link = document.createElement('a');
        link.style.display = 'none';
        link.href = url;
        link.setAttribute('download', this.value[0]+'到'+this.value[1]+'违规表'+'.xlsx');
        document.body.appendChild(link);
        link.click()
      })
      }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值