一,导出excel
1,使用excel模板
public void exportLog() throws Exception{ SystemUser currentUsr = getCurrentSystemUser(); //该用户的所有日志 List<TLogInfo> loglist=logService.getLogInfosByUserId(currentUsr.getId()); List<TLogInfo> list=new ArrayList<TLogInfo>(); if(loglist!=null && loglist.size()>0){ for(TLogInfo log:loglist){ TLogInfo export=new TLogInfo(); export.setProName(log.getProName()); export.setLogTitle(log.getLogTitle()); export.setLogType(log.getLogType()); export.setWorkTime(log.getWorkTime()); export.setLogFillTime(log.getLogFillTime()); list.add(export); } } String templateFileName = "xlstemp/personallog.xls"; String resultFileName = "xlsresult/personallog.xls"; String path = ExcelUtil.createExcel(templateFileName, list, resultFileName); File file = new File(path); byte[] bytes = FileUtils.readFileToByteArray(file); downloadFile("信息.xls", bytes); file.deleteOnExit(); }
ExcelUtil类的createExcel方法
public static String createExcel(String templateFileName, List<?> list, String resultFileName) { // 创建XLSTransformer对象 XLSTransformer transformer = new XLSTransformer(); // 获取java项目编译后根路径 String class_path = ExcelUtil.class.getClassLoader().getResource("").getPath(); // 得到模板文件路径 String srcFilePath = class_path + templateFileName; Map<String, Object> beanParams = new HashMap<String, Object>(); beanParams.put("list", list); String destFilePath = class_path + resultFileName; try { // 生成Excel文件 transformer.transformXLS(srcFilePath, beanParams, destFilePath); return destFilePath; } catch (ParsePropertyException e) { e.printStackTrace(); } catch (InvalidFormatException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } return null; }
未完待续。。。。
时间: 2024-11-05 20:28:15