Search in sources :

Example 1 with ExcelExportEntity

use of cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity in project springboot by LiJinHongPassion.

the class MapExcelController method downloadByPoiBaseViewDog.

@RequestMapping("loadDog")
public void downloadByPoiBaseViewDog(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    /*编辑表头——start*/
    List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
    ExcelExportEntity excelentity;
    entity.add(new ExcelExportEntity("狗昵称", "dog_name"));
    entity.add(new ExcelExportEntity("性别", "dog_sex"));
    entity.add(new ExcelExportEntity("年龄", "dog_age"));
    entity.add(new ExcelExportEntity("体重", "dog_height"));
    entity.add(new ExcelExportEntity("种类", "dog_kind"));
    /*编辑表头——end*/
    /*数据源-start*/
    List<Map<String, Object>> list = dogService.getDogsListMap();
    ExportParams params = new ExportParams("2412312", "测试dog", ExcelType.XSSF);
    // 冻结列
    params.setFreezeCol(2);
    // 返回响应下载,构造http头部
    // 文件内容
    modelMap.put(MapExcelConstants.MAP_LIST, list);
    modelMap.put(MapExcelConstants.ENTITY_LIST, entity);
    modelMap.put(MapExcelConstants.PARAMS, params);
    // 文件名
    modelMap.put(MapExcelConstants.FILE_NAME, "测试dog");
    PoiBaseView.render(modelMap, request, response, MapExcelConstants.EASYPOI_MAP_EXCEL_VIEW);
}
Also used : ExportParams(cn.afterturn.easypoi.excel.entity.ExportParams) ExcelExportEntity(cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) ModelMap(org.springframework.ui.ModelMap) Map(java.util.Map) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ExcelExportEntity

use of cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity in project longmarch by yuyueqty.

the class SysTableBodyServiceImpl method exportData.

@DS("master")
@SuppressWarnings("unchecked")
@Override
public void exportData(String code, String dataSource, Map<String, Object> params, HttpServletResponse response, HttpServletRequest request) {
    SysTableBody sysTableBody = getSysTableBody(code);
    List<SysTableTitle> sysTableTitles = sysTableTitleService.tableTitleList(sysTableBody.getId());
    if (CollectionUtil.isEmpty(sysTableTitles)) {
        throw new BusinessException(5000, "统计SQL未配置字段");
    }
    List<ExcelExportEntity> exportEntityList = sysTableTitles.stream().map(e -> new ExcelExportEntity(e.getLabel(), e.getField())).collect(Collectors.toList());
    Map<String, Object> newParams = PageFactory.buildMap(dataSource, params);
    String cacheKey = getCacheKey(STATISTICS_EXPORT, code, newParams.toString());
    Object data = CacheUtil.get(cacheKey);
    List<LinkedHashMap<String, Object>> mapList = null;
    if (data != null && IS_CACHE) {
        mapList = (List<LinkedHashMap<String, Object>>) data;
    } else {
        try {
            switchingDataSource(dataSource, sysTableBody.getDataSource());
            mapList = sysAutoMapper.list(newParams, sysTableBody.getSqlText());
            if (CollectionUtil.isNotEmpty(mapList)) {
                CacheUtil.put(cacheKey, mapList, TIME_OUT);
            }
        } catch (Exception ignored) {
        // TODO do nothing
        }
    }
    if (mapList != null && mapList.size() > 0) {
        ExportParams exportParams = new ExportParams(sysTableBody.getLabel(), sysTableBody.getLabel());
        exportParams.setType(ExcelType.XSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, exportEntityList, mapList);
        DownLoadUtil.downLoad(response, request, workbook, sysTableBody.getCode());
    }
}
Also used : java.util(java.util) ExcelExportUtil(cn.afterturn.easypoi.excel.ExcelExportUtil) ExportParams(cn.afterturn.easypoi.excel.entity.ExportParams) PersistenceException(org.apache.ibatis.exceptions.PersistenceException) BusinessException(top.longmarch.lmcore.exception.BusinessException) PageFactory(top.longmarch.lmcore.common.PageFactory) RequiredArgsConstructor(lombok.RequiredArgsConstructor) TableTitleDTO(top.longmarch.lmsys.dto.TableTitleDTO) SysAutoMapper(top.longmarch.lmsys.mapper.SysAutoMapper) DS(com.baomidou.dynamic.datasource.annotation.DS) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) DynamicRoutingDataSource(com.baomidou.dynamic.datasource.DynamicRoutingDataSource) DownLoadUtil(top.longmarch.lmcore.utils.upload.DownLoadUtil) Function(java.util.function.Function) ISysTableRelService(top.longmarch.lmsys.service.ISysTableRelService) MD5(cn.hutool.crypto.digest.MD5) JSONUtil(cn.hutool.json.JSONUtil) HttpServletRequest(javax.servlet.http.HttpServletRequest) Service(org.springframework.stereotype.Service) TableTitleVO(top.longmarch.lmsys.vo.TableTitleVO) ServiceImpl(com.baomidou.mybatisplus.extension.service.impl.ServiceImpl) Wrappers(com.baomidou.mybatisplus.core.toolkit.Wrappers) ExcelExportEntity(cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity) CollectionUtil(cn.hutool.core.collection.CollectionUtil) SysTableTitle(top.longmarch.lmsys.entity.SysTableTitle) ISysTableTitleService(top.longmarch.lmsys.service.ISysTableTitleService) CacheUtil(top.longmarch.lmcore.cache.CacheUtil) ExcelType(cn.afterturn.easypoi.excel.entity.enmus.ExcelType) HttpServletResponse(javax.servlet.http.HttpServletResponse) ISysTableBodyService(top.longmarch.lmsys.service.ISysTableBodyService) Collectors(java.util.stream.Collectors) SearchSuper(top.longmarch.lmcore.common.SearchSuper) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) StrUtil(cn.hutool.core.util.StrUtil) Slf4j(lombok.extern.slf4j.Slf4j) Workbook(org.apache.poi.ss.usermodel.Workbook) DynamicDataSourceContextHolder(com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder) SysTableRel(top.longmarch.lmsys.entity.SysTableRel) SysTableBodyMapper(top.longmarch.lmsys.mapper.SysTableBodyMapper) Data(lombok.Data) JSONArray(cn.hutool.json.JSONArray) IPage(com.baomidou.mybatisplus.core.metadata.IPage) SysTableBody(top.longmarch.lmsys.entity.SysTableBody) SysTableTitle(top.longmarch.lmsys.entity.SysTableTitle) ExcelExportEntity(cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity) PersistenceException(org.apache.ibatis.exceptions.PersistenceException) BusinessException(top.longmarch.lmcore.exception.BusinessException) Workbook(org.apache.poi.ss.usermodel.Workbook) SysTableBody(top.longmarch.lmsys.entity.SysTableBody) ExportParams(cn.afterturn.easypoi.excel.entity.ExportParams) BusinessException(top.longmarch.lmcore.exception.BusinessException) DS(com.baomidou.dynamic.datasource.annotation.DS)

Example 3 with ExcelExportEntity

use of cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity in project springboot by LiJinHongPassion.

the class MapExcelController method downloadByPoiBaseView.

/**
 *如果上面的方法不行,可以使用下面的用法
 * 同样的效果,只不过是直接问输出了,不经过view了
 * @param
 * @param request
 * @param response
 */
@RequestMapping("load")
public void downloadByPoiBaseView(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    /*编辑表头,表1和表2拼成一张表——start*/
    /*表1*/
    List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
    ExcelExportEntity excelentity = new ExcelExportEntity("姓名", "name");
    // 竖着合并单元格
    excelentity.setNeedMerge(true);
    entity.add(excelentity);
    entity.add(new ExcelExportEntity("性别", "sex"));
    /*表2*/
    excelentity = new ExcelExportEntity(null, "students");
    List<ExcelExportEntity> temp = new ArrayList<ExcelExportEntity>();
    temp.add(new ExcelExportEntity("姓名", "name"));
    temp.add(new ExcelExportEntity("性别", "sex"));
    excelentity.setList(temp);
    entity.add(excelentity);
    /*编辑表头——end*/
    /*数据源-start*/
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    Map<String, Object> map_table1;
    Map<String, Object> map_table2_row1;
    Map<String, Object> map_table2_row2;
    Map<String, Object> map_table2_row3;
    for (int i = 0; i < 10; i++) {
        /*表1数据源*/
        map_table1 = new HashMap<String, Object>();
        map_table1.put("name", "1" + i);
        map_table1.put("sex", "2" + i);
        /*表2数据源*/
        List<Map<String, Object>> tempList = new ArrayList<Map<String, Object>>();
        map_table2_row1 = new HashMap<String, Object>();
        map_table2_row1.put("name", "stu1" + i);
        map_table2_row1.put("sex", "stu2" + i);
        map_table2_row2 = new HashMap<String, Object>();
        map_table2_row2.put("name", "stu11" + i);
        map_table2_row2.put("sex", "stu22" + i);
        map_table2_row3 = new HashMap<String, Object>();
        map_table2_row3.put("name", "stu111" + i);
        map_table2_row3.put("sex", "stu222" + i);
        tempList.add(map_table2_row1);
        tempList.add(map_table2_row2);
        tempList.add(map_table2_row3);
        /*将表2数据合并到表1*/
        map_table1.put("students", tempList);
        /*总数据源--一行*/
        list.add(map_table1);
    }
    ExportParams params = new ExportParams("2412312", "测试复杂", ExcelType.XSSF);
    // 冻结列
    params.setFreezeCol(2);
    // 返回响应下载,构造http头部
    // 文件内容
    modelMap.put(MapExcelConstants.MAP_LIST, list);
    modelMap.put(MapExcelConstants.ENTITY_LIST, entity);
    modelMap.put(MapExcelConstants.PARAMS, params);
    // 文件名
    modelMap.put(MapExcelConstants.FILE_NAME, "EasypoiMapExcelViewTest");
    PoiBaseView.render(modelMap, request, response, MapExcelConstants.EASYPOI_MAP_EXCEL_VIEW);
}
Also used : ExportParams(cn.afterturn.easypoi.excel.entity.ExportParams) ExcelExportEntity(cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) ModelMap(org.springframework.ui.ModelMap) Map(java.util.Map) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with ExcelExportEntity

use of cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity in project springboot by LiJinHongPassion.

the class MapExcelController method download.

@RequestMapping()
public String download(ModelMap modelMap) {
    /*编辑表头,表1和表2拼成一张表——start*/
    /*表1*/
    List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
    ExcelExportEntity excelentity = new ExcelExportEntity("姓名", "name");
    // 竖着合并单元格
    excelentity.setNeedMerge(true);
    entity.add(excelentity);
    entity.add(new ExcelExportEntity("性别", "sex"));
    /*表2*/
    excelentity = new ExcelExportEntity(null, "students");
    List<ExcelExportEntity> temp = new ArrayList<ExcelExportEntity>();
    temp.add(new ExcelExportEntity("姓名", "name"));
    temp.add(new ExcelExportEntity("性别", "sex"));
    excelentity.setList(temp);
    entity.add(excelentity);
    /*编辑表头——end*/
    /*数据源-start*/
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    Map<String, Object> map_table1;
    Map<String, Object> map_table2_row1;
    Map<String, Object> map_table2_row2;
    Map<String, Object> map_table2_row3;
    for (int i = 0; i < 10; i++) {
        /*表1数据源*/
        map_table1 = new HashMap<String, Object>();
        map_table1.put("name", "1" + i);
        map_table1.put("sex", "2" + i);
        /*表2数据源*/
        List<Map<String, Object>> tempList = new ArrayList<Map<String, Object>>();
        map_table2_row1 = new HashMap<String, Object>();
        map_table2_row1.put("name", "stu1" + i);
        map_table2_row1.put("sex", "stu2" + i);
        map_table2_row2 = new HashMap<String, Object>();
        map_table2_row2.put("name", "stu11" + i);
        map_table2_row2.put("sex", "stu22" + i);
        map_table2_row3 = new HashMap<String, Object>();
        map_table2_row3.put("name", "stu111" + i);
        map_table2_row3.put("sex", "stu222" + i);
        tempList.add(map_table2_row1);
        tempList.add(map_table2_row2);
        tempList.add(map_table2_row3);
        /*将表2数据合并到表1*/
        map_table1.put("students", tempList);
        /*总数据源--一行*/
        list.add(map_table1);
    }
    ExportParams params = new ExportParams("2412312", "测试复杂", ExcelType.XSSF);
    // 冻结列
    params.setFreezeCol(2);
    // 返回响应下载,构造http头部
    // 文件内容
    modelMap.put(MapExcelConstants.MAP_LIST, list);
    modelMap.put(MapExcelConstants.ENTITY_LIST, entity);
    modelMap.put(MapExcelConstants.PARAMS, params);
    // 文件名
    modelMap.put(MapExcelConstants.FILE_NAME, "EasypoiMapExcelViewTest");
    return MapExcelConstants.EASYPOI_MAP_EXCEL_VIEW;
}
Also used : ExportParams(cn.afterturn.easypoi.excel.entity.ExportParams) ExcelExportEntity(cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) ModelMap(org.springframework.ui.ModelMap) Map(java.util.Map) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ExportParams (cn.afterturn.easypoi.excel.entity.ExportParams)4 ExcelExportEntity (cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ModelMap (org.springframework.ui.ModelMap)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ExcelExportUtil (cn.afterturn.easypoi.excel.ExcelExportUtil)1 ExcelType (cn.afterturn.easypoi.excel.entity.enmus.ExcelType)1 CollectionUtil (cn.hutool.core.collection.CollectionUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 MD5 (cn.hutool.crypto.digest.MD5)1 JSONArray (cn.hutool.json.JSONArray)1 JSONUtil (cn.hutool.json.JSONUtil)1 DynamicRoutingDataSource (com.baomidou.dynamic.datasource.DynamicRoutingDataSource)1 DS (com.baomidou.dynamic.datasource.annotation.DS)1 DynamicDataSourceContextHolder (com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder)1 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)1 IPage (com.baomidou.mybatisplus.core.metadata.IPage)1 Wrappers (com.baomidou.mybatisplus.core.toolkit.Wrappers)1