Search in sources :

Example 1 with ExcelDecAndTextbookVO

use of com.bc.pmpheep.back.vo.ExcelDecAndTextbookVO in project pmph by BCSquad.

the class FileDownLoadController method exportExcel.

/**
 * 导出书籍遴选名单/批量导出
 *
 * @param request
 * @param response
 * @param textbookIds
 * @throws CheckedServiceException
 * @throws Exception
 */
@LogDetail(businessType = BUSSINESS_TYPE, logRemark = "导出书籍遴选名单/批量导出")
@RequestMapping(value = "/chosenPosition/exportExcel", method = RequestMethod.GET)
public void exportExcel(HttpServletRequest request, HttpServletResponse response, @RequestParam("textbookIds") Long[] textbookIds) throws CheckedServiceException, Exception {
    Workbook workbook = null;
    List<ExcelDecAndTextbookVO> list = null;
    try {
        list = textbookService.getExcelDecAndTextbooks(textbookIds);
        if (list.size() == 0) {
            // 设置表头 ,放置初始化表出错
            list.add(new ExcelDecAndTextbookVO());
        }
        workbook = excelHelper.fromBusinessObjectList(list, "遴选名单");
    } catch (CheckedServiceException | IllegalArgumentException | IllegalAccessException e) {
        logger.warn("数据表格化的时候失败");
    }
    response.setCharacterEncoding("utf-8");
    response.setContentType("application/force-download");
    String fileName = null;
    if (textbookIds.length > 1) {
        // 当批量导出的时候 文件名为教材名称
        // 书籍名称
        String materialName = list.get(0).getMaterialName();
        if (null == materialName) {
            materialName = "遴选名单导出";
        }
        fileName = returnFileName(request, materialName + ".xls");
    } else {
        // 当单个导出的时候 文件名为书籍名称
        // 书籍名称
        String TextbookName = list.get(0).getTextbookName();
        if (null == TextbookName) {
            TextbookName = "遴选名单导出";
        }
        fileName = returnFileName(request, TextbookName + ".xls");
    }
    response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
    try (OutputStream out = response.getOutputStream()) {
        workbook.write(out);
        out.flush();
        out.close();
    } catch (Exception e) {
        logger.warn("文件下载时出现IO异常:{}", e.getMessage());
        throw new CheckedServiceException(CheckedExceptionBusiness.FILE, CheckedExceptionResult.FILE_DOWNLOAD_FAILED, "文件在传输时中断");
    }
}
Also used : ExcelDecAndTextbookVO(com.bc.pmpheep.back.vo.ExcelDecAndTextbookVO) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) Workbook(org.apache.poi.ss.usermodel.Workbook) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) LogDetail(com.bc.pmpheep.annotation.LogDetail) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

LogDetail (com.bc.pmpheep.annotation.LogDetail)1 ExcelDecAndTextbookVO (com.bc.pmpheep.back.vo.ExcelDecAndTextbookVO)1 CheckedServiceException (com.bc.pmpheep.service.exception.CheckedServiceException)1 BufferedOutputStream (java.io.BufferedOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Workbook (org.apache.poi.ss.usermodel.Workbook)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1