zoukankan      html  css  js  c++  java
  • 进入指定url就可下载xlsx文件

    GuestApplyService.java

    
    

    //
    private final static int PERSONAL_NAME_INDEX = 0;
    private final static int USER_NAME_INDEX = 1;
    private final static int COMPANY_NAME_INDEX = 2;
    private final static int LOGIN_NAME_INDEX = 3;
    private final static int APPLY_DAYS_INDEX = 4;
    private final static int TELEPHONE_INDEX = 5;
    private final static int APPLY_REASON_INDEX = 6;
    private final static int APPLY_TIME_INDEX = 7;
    //


    //
    导出 @Transactional public void exportListApplications(TenantId tenantId, String templatePath, OutputStream os) throws IOException { DataPage<GuestApplication> applications = this.guestApplicationRepository.getByTenantId(tenantId, 0, Integer.MAX_VALUE); List<GuestApplicationDTO> dtos = new ArrayList<GuestApplicationDTO>(); for (GuestApplication guestApplication : applications.getData()) { dtos.add(GuestApplicationDTO.fromGuestApplication(guestApplication)); } String tempPath = FileUtils.getTempDirectoryPath(); File excelFile = new File(tempPath + "\" + generateFileName() + ".xlsx"); File templateFile = new File(templatePath); FileUtils.copyFile(templateFile, excelFile); XSSFWorkbook wb; try (OPCPackage opk = OPCPackage.open(excelFile)) { wb = new XSSFWorkbook(opk); XSSFSheet sheet = wb.getSheetAt(0); int maxRowNum = sheet.getLastRowNum(); XSSFRow row = null; GuestApplicationDTO guestApp = null; for (int i = 3; i < maxRowNum; i++) { row = sheet.getRow(i); if (row != null) { sheet.removeRow(row); } } for (int i = 3; i < dtos.size() + 3; i++) { row = sheet.createRow(i); guestApp = dtos.get(i - 3); if (guestApp == null) { continue; }

    row.createCell(PERSONAL_NAME_INDEX, Cell.CELL_TYPE_STRING).setCellValue(guestApp.getPersonalName());
    row.createCell(LOGIN_NAME_INDEX, Cell.CELL_TYPE_STRING).setCellValue(guestApp.getLoginName());
    row.createCell(COMPANY_NAME_INDEX, Cell.CELL_TYPE_STRING).setCellValue(guestApp.getCompanyName());
    row.createCell(USER_NAME_INDEX, Cell.CELL_TYPE_STRING).setCellValue(guestApp.getUserName());
    row.createCell(APPLY_DAYS_INDEX, Cell.CELL_TYPE_STRING).setCellValue(guestApp.getApplyDays());
    row.createCell(TELEPHONE_INDEX, Cell.CELL_TYPE_STRING).setCellValue(guestApp.getTelephone());
    row.createCell(APPLY_REASON_INDEX, Cell.CELL_TYPE_STRING).setCellValue(guestApp.getApplyReason());
    row.createCell(APPLY_TIME_INDEX, Cell.CELL_TYPE_STRING).setCellValue(guestApp.getApplyTime());

    
                }
                wb.write(os);
            } catch (InvalidFormatException ex) {
                throw new DkRuntimeException(ex);
            } finally {
                FileUtils.forceDelete(excelFile);
            }
        }

    GuestController.java

     @RequestMapping(value = "/guest/export")
        public void exportGuest(HttpServletRequest request, HttpServletResponse response) {
            OutputStream os = null;
            String fileName = "guest_application_template.xlsx";
            try {
                response.reset();
                response.setContentType("application/octet-stream;charset=UTF-8");
                os = response.getOutputStream();
                String fileAbsolutePath = request.getSession().getServletContext()
                        .getRealPath("/")
                        + "page/resources/" + fileName;
                guestApplyApplicationService.exportListApplications(new TenantId(CurrentTenant.getInstance(request).getTenant().getId()), fileAbsolutePath, os);
    
            } catch (IOException e) {
                _logger.error(e.getMessage(), e);
            } finally {
                IOUtils.closeQuietly(os);
            }
        }

    url: http://localhost:8080/am/controller/tenant/guest/export

    guest_application_template.xlsx

  • 相关阅读:
    Mysql 使用触发器,把插入的数据在插入到宁一张表里
    Mysql 查询今天的某些时间之外的数据
    PHPStorm+XDEBUG 调试Laravel
    Python 2.7 爬取51job 全国java岗位
    Tp3.1 文件上传到七牛云
    TP3.1 一对多模型关联
    Mysql 主从配置
    自动化测试Java一:Selenium入门
    Selenium基于Python 进行 web 自动化测试
    Python 创建XML
  • 原文地址:https://www.cnblogs.com/littlehoom/p/4672470.html
Copyright © 2011-2022 走看看