easy poi 的,使用ExcelExportEntity生成多sheet的excel文件

pom.xml


	cn.afterturn
	easypoi-base
	3.2.0


	cn.afterturn
	easypoi-web
	3.2.0


	cn.afterturn
	easypoi-annotation
	3.2.0

代码

  try {
        	
     	List> sheets = new ArrayList>();
     	
     	for(int i = 0;i<10;i++) {
     		Map sheet = new HashMap();
         	
             List entity = new ArrayList();//构造对象等同于@Excel
             
             entity.add(new ExcelExportEntity("性别", "sex"));
             entity.add(new ExcelExportEntity("姓名", "name"));
             
             List> list = new ArrayList>();
             Map h1 = new HashMap();
             h1.put("name", "name" + i);
             h1.put("sex", "sex" +  i);
             Map h2 = new HashMap();
             h2.put("name", "name" +i+i);
             h2.put("sex", "sex" +i+i);
             list.add(h1);
             list.add(h2);
             
             sheet.put(NormalExcelConstants.CLASS, ExcelExportEntity.class);
             sheet.put(NormalExcelConstants.DATA_LIST, list);
             sheet.put(NormalExcelConstants.PARAMS, new ExportParams(null, "sheet"+i));
             sheet.put(NormalExcelConstants.MAP_LIST, entity);
             sheets.add(sheet);
     	}
     	
     	 Workbook workbook = new HSSFWorkbook();
     	 
     	 for(Map map : sheets) {
     		 ExcelExportService server = new ExcelExportService();
     		 ExportParams param = (ExportParams) map.get("params");
     		 @SuppressWarnings("unchecked")
	List entity = (List) map.get("mapList");
     		 Collection data = (Collection) map.get("data");
     		 server.createSheetForMap(workbook, param, entity, data);
     	 }

         FileOutputStream fos = new FileOutputStream("D:/ExcelExportForMap.tt.xls");
         workbook.write(fos);
         fos.close();
     } catch (FileNotFoundException e) {
         e.printStackTrace();
     } catch (IOException e) {
         e.printStackTrace();
     }

你可能感兴趣的:(java)