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, "文件在传输时中断");
}
}
Aggregations