기술나눔

easy-poi는 동적 열(제목) 및 다중 시트 내보내기를 Excel로 구현합니다.

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

시트는 채워진 통합 문서 개체를 동적으로 내보내고, 의사 코드를 만들고, 생성합니다.

  1. List<Map<String, Object>>list = new ArrayList<Map<String, Object>>();
  2. HashMap<String, Object> map = new HashMap<>();
  3. map.put("name", "小明");
  4. map.put("age", "18");
  5. list1.add(map);
  6. List<ExcelExportEntity> entitys=new ArrayList<>();
  7. entitys.add(new ExcelExportEntity("名字" ,"name"));
  8. entitys.add(new ExcelExportEntity("年龄" ,"age"));
  9. ExportParams exportParams = new ExportParams(null, "sheet1名字");
  10. exportParams.setType(ExcelType.XSSF);
  11. Workbook workbook = ExcelExportUtil.exportExcel(exportParams, entitys, list);

여러 시트, 의사 코드 및 채워진 통합 문서 개체 생성의 동적 내보내기

  1. List<Map<String, Object>> sheet1ListMap= new ArrayList<Map<String, Object>>();
  2. List<Map<String, Object>> sheet2ListMap= new ArrayList<Map<String, Object>>();
  3. List<ExcelExportEntity> sheet1List=new ArrayList<>();
  4. sheet1List.add(new ExcelExportEntity("名字" ,"name"));
  5. sheet1List.add(new ExcelExportEntity("年龄" ,"age"));
  6. List<ExcelExportEntity> sheet2List=new ArrayList<>();
  7. sheet2List.add(new ExcelExportEntity("学号" ,"sno"));
  8. sheet2List.add(new ExcelExportEntity("课程" ,"course"));
  9. ExportParams exportParams1 = new ExportParams(null, "sheet1名字");
  10. exportParams1.setType(ExcelType.XSSF);
  11. ExportParams exportParams2 = new ExportParams(null, "sheet2名字");
  12. exportParams2.setType(ExcelType.XSSF);
  13. Workbook workbook = new XSSFWorkbook();
  14. ExcelExportService service = new ExcelExportService();
  15. // 创建sheet1,执行的先后顺序
  16. service.createSheetForMap(workbook, exportParams1, sheet1List, sheet1ListMap);
  17. // 创建sheet2
  18. service.createSheetForMap(workbook, exportParams2, sheet2List, sheet2ListMap);

참고: createSheetForMap() 메소드에는 사용 제한이 있으며 내보낸 데이터는 null일 수 없습니다.헤더만 있고 데이터는 없는 양식을 내보내야 하는 경우 해당 엔터티 클래스를 만들고 주석을 사용해야 합니다.

참조 문서:easy-poi는 동적 열(제목), Excel 문서의 다중 시트 내보내기 등과 같은 작업을 구현합니다. - 완전한 테스트 사례가 첨부되어 있습니다.