How Java Implements Exporting Excel

Keywords: Excel Attribute Database xml

Tool class used: org. jeecgframework. poi. excel. Excel ExportUtil
The source code is as follows:

public class ExcelExportUtil {

    private ExcelExportUtil() {
    }

    /**
     * @param entity
     *            Table Title Properties
     * @param pojoClass
     *            Excel Object Class
     * @param dataSet
     *            Excel Object Data List
     */
    public static Workbook exportBigExcel(ExportParams entity, Class<?> pojoClass,
                                          Collection<?> dataSet) {
        ExcelBatchExportServer batachServer = ExcelBatchExportServer
            .getExcelBatchExportServer(entity, pojoClass);
        return batachServer.appendData(dataSet);
    }

    public static void closeExportBigExcel() {
        ExcelBatchExportServer batachServer = ExcelBatchExportServer.getExcelBatchExportServer(null,
            null);
        batachServer.closeExportBigExcel();
    }

    /**
     * @param entity
     *            Table Title Properties
     * @param pojoClass
     *            Excel Object Class
     * @param dataSet
     *            Excel Object Data List
     */
    public static Workbook exportExcel(ExportParams entity, Class<?> pojoClass,
                                       Collection<?> dataSet) {
        Workbook workbook;
        if (ExcelType.HSSF.equals(entity.getType())) {
            workbook = new HSSFWorkbook();
        } else if (dataSet.size() < 10000) {
            workbook = new XSSFWorkbook();
        } else {
            workbook = new SXSSFWorkbook();
        }
        new ExcelExportServer().createSheet(workbook, entity, pojoClass, dataSet);
        return workbook;
    }

    /**
     * Create the corresponding Excel based on Map
     * @param entity
     *            Table Title Properties
     * @param entityList
     *            Map Object list
     * @param dataSet
     *            Excel Object Data List
     */
    public static Workbook exportExcel(ExportParams entity, List<ExcelExportEntity> entityList,
                                       Collection<? extends Map<?, ?>> dataSet) {
        Workbook workbook;
        if (ExcelType.HSSF.equals(entity.getType())) {
            workbook = new HSSFWorkbook();
        } else if (dataSet.size() < 10000) {
            workbook = new XSSFWorkbook();
        } else {
            workbook = new SXSSFWorkbook();
        }
        new ExcelExportServer().createSheetForMap(workbook, entity, entityList, dataSet);
        return workbook;
    }

    /**
     * One excel creates multiple sheet s
     * 
     * @param list
     *            Multiple Map key title s correspond to tables Title key entity corresponds to tables corresponding to entity key data
     *            Collection data
     * @return
     */
    public static Workbook exportExcel(List<Map<String, Object>> list, ExcelType type) {
        Workbook workbook;
        if (ExcelType.HSSF.equals(type)) {
            workbook = new HSSFWorkbook();
        } else {
            workbook = new XSSFWorkbook();
        }
        for (Map<String, Object> map : list) {
            ExcelExportServer server = new ExcelExportServer();
            server.createSheet(workbook, (ExportParams) map.get("title"),
                (Class<?>) map.get("entity"), (Collection<?>) map.get("data"));
        }
        return workbook;
    }

    /**
     * Export files are parsed by templates. This is not recommended. It is recommended that all export files be processed by templates.
     * 
     * @param params
     *            Derived parameter class
     * @param pojoClass
     *            Corresponding entity
     * @param dataSet
     *            Entity set
     * @param map
     *            Template set
     * @return
     */
    @Deprecated
    public static Workbook exportExcel(TemplateExportParams params, Class<?> pojoClass,
                                       Collection<?> dataSet, Map<String, Object> map) {
        return new ExcelExportOfTemplateUtil().createExcleByTemplate(params, pojoClass, dataSet,
            map);
    }

    /**
     * Export files are parsed through templates only with templates, no collections
     * 
     * @param params
     *            Derived parameter class
     * @param map
     *            Template set
     * @return
     */
    public static Workbook exportExcel(TemplateExportParams params, Map<String, Object> map) {
        return new ExcelExportOfTemplateUtil().createExcleByTemplate(params, null, null, map);
    }

    /**
     * Export files are parsed through templates only with templates, no collections
     * Each sheet corresponds to a map and is exported everywhere. The key is the NUM of the sheet.
     * @param params
     *            Derived parameter class
     * @param map
     *            Template set
     * @return
     */
    public static Workbook exportExcel(Map<Integer, Map<String, Object>> map,
                                       TemplateExportParams params) {
        return new ExcelExportOfTemplateUtil().createExcleByTemplate(params, map);
    }

}

Next, we use the public static Workbook exportExcel (Export Params entity, Class <?> pojoClass, Collection <?> dataSet) method to realize the export function of excel. The method parameters are as follows:

  • @param entity
  •        The table title attribute is generally Excel BaseParams and its subclasses, such as ExportParams class, which defines the export parameters of Excel, such as table name, table size, etc.
    
  • @param pojoClass
  •        Excel object Class simply means what column name does excel have
    
  • @param dataSet
  •        The elements of Excel object data List List correspond to a row of Excel, which is a pojoClass instance.
    

PeopleExcel.class:

class PeopleExcel{
	private Integer id;
	private String name;
	private Integer age;
}

Controller: Input address print out Excel directly:

    @RequestMapping("/peopleExcelList")
    @ResponseBody
    public void queryList(HttpServletResponse response) throws Exception {
        Map<String, Object> params = new HashMap<>();
        List<PeopleExcel> peopleExcelList = peopleExcelService.exportTo(params);
        response.setHeader("content-Type", SysConstants.ExportQueue.EXPORT_EXCEL_CONTENT_TYPE);
        String fileName = "Excel Export example.xls";
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), PeopleExcel.class, peopleExcelList );
        workbook.write(response.getOutputStream());
    }

Service: Call the mapper method to fetch the PeopleExcel list

Service Interface:
	List<PeopleExcel> exportTo(Map<String, Object> params);
	
Service Interface: Implementation class:
    @Override
    public List<PeopleExcel> exportTo(Map<String, Object> params) {
        params.put("pageBegin", 0);
        params.put("pageSize", 30000);
        List<PeopleExcel> list = peopleExcelMapper.selectList(params);
        return list ;
    }

Mapper: Remove the value of the database query

Mapper Interface
List<PeopleExcel> selectList(Map<String, Object> param);

Mapper.xml
	<select id="selectList" parameterType="Object" resultType="PeopleExcel">
        select p.* from peopel p
        ORDER BY p.id DESC
        limit #{pageBegin},#{pageSize}
    </select>

Start the server, such as tomcat, enter the address: http://localhost:8080/peopleExcelList to print out excel.

Posted by Kaylub on Thu, 10 Oct 2019 11:56:53 -0700