• poi包工具类


    @Slf4j
    public class EasyPoiUtils {
        /**
         * 2003
         */
        private static final String EXCEL_XLS = "xls";
        /**
         * 2007
         */
        private static final String EXCEL_XLSX = "xlsx";
    
        private EasyPoiUtils() {
        }
    
        /**
         * 读入excel文件,解析后返回
         * 多sheet页,
         *
         * @param file
         * @return key:sheetName value:sheet内容
         */
        public static Map> readExcel(MultipartFile file) {
            //检查文件
            checkFile(file);
            //获得Workbook工作薄对象
            Workbook workbook = getWorkBook(file);
            //创建返回对象,把每行中的值作为一个数组,所有行作为一个集合返回
            Map> map = new HashMap<>(16);
            List list = new ArrayList<>();
            if (workbook != null) {
                for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
                    //获得当前sheet工作表
                    Sheet sheet = workbook.getSheetAt(sheetNum);
                    String sheetName = workbook.getSheetName(sheetNum);
                    if (sheet == null) {
                        continue;
                    }
                    //获得当前sheet的开始行
                    int firstRowNum = sheet.getFirstRowNum();
                    //获得当前sheet的结束行
                    int lastRowNum = sheet.getLastRowNum();
                    //循环除了第一行的所有行
                    for (int rowNum = firstRowNum; rowNum <= lastRowNum; rowNum++) {
                        //获得当前行
                        Row row = sheet.getRow(rowNum);
                        if (row == null) {
                            continue;
                        }
                        //获得当前行的开始列
                        int firstCellNum = row.getFirstCellNum();
                        //获得当前行的列数
                        int lastCellNum = row.getPhysicalNumberOfCells();
                        String[] cells = new String[row.getPhysicalNumberOfCells()];
                        //循环当前行
                        for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {
                            Cell cell = row.getCell(cellNum);
                            cells[cellNum] = getCellValue(cell);
                        }
                        list.add(cells);
                    }
                    map.put(sheetName, list);
                }
                try {
                    workbook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return map;
        }
    
        private static void checkFile(MultipartFile file) {
            //判断文件是否存在
            if (null == file) {
                log.error("文件不存在!");
                throw new BusinessException("文件不存在!");
            }
            //获得文件名
            String fileName = file.getOriginalFilename();
            //判断文件是否是excel文件
            if (fileName != null && !fileName.endsWith(EXCEL_XLS) && !fileName.endsWith(EXCEL_XLSX)) {
                log.error(fileName + "不是excel文件");
                throw new BusinessException(fileName + "不是excel文件");
            }
        }
    
        private static Workbook getWorkBook(MultipartFile file) {
            //获得文件名
            String fileName = file.getOriginalFilename();
            //创建Workbook工作薄对象,表示整个excel
            Workbook workbook = null;
            try {
                //获取excel文件的io流
                InputStream is = file.getInputStream();
                //根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象
                if (fileName != null && fileName.endsWith(EXCEL_XLS)) {
                    //2003
                    workbook = new HSSFWorkbook(is);
                } else if (fileName != null && fileName.endsWith(EXCEL_XLSX)) {
                    //2007
                    workbook = new XSSFWorkbook(is);
                }
            } catch (IOException e) {
                log.info(e.getMessage());
            }
            return workbook;
        }
    
        private static String getCellValue(Cell cell) {
            String cellValue = "";
            if (cell == null) {
                return cellValue;
            }
            //把数字当成String来读,避免出现1读成1.0的情况
            if (cell.getCellType() == NUMERIC) {
                cell.setCellType(CellType.STRING);
            }
            //判断数据的类型
            switch (cell.getCellType()) {
                //数字
                case NUMERIC:
                    cellValue = String.valueOf(cell.getNumericCellValue());
                    break;
                //字符串
                case STRING:
                    cellValue = String.valueOf(cell.getStringCellValue());
                    break;
                //Boolean
                case BOOLEAN:
                    cellValue = String.valueOf(cell.getBooleanCellValue());
                    break;
                //公式
                case FORMULA:
                    cellValue = String.valueOf(cell.getCellFormula());
                    break;
                //空值
                case BLANK:
                    cellValue = "";
                    break;
                //故障
                case ERROR:
                    cellValue = "非法字符";
                    break;
                default:
                    cellValue = "未知类型";
                    break;
            }
            return cellValue;
        }
    
        /***
         *导出Excel
         * @param list 数据集合
         * @param title 标题名
         * @param sheetName sheet页名
         * @param pojoClass 主体类
         * @param fileName 文件名
         * @param isCreateHeader 是否创建表头
         * @param response
         */
        public static void exportExcel(List list, String title, String sheetName, Class pojoClass,String fileName,boolean isCreateHeader, HttpServletResponse response){
            ExportParams exportParams = new ExportParams(title, sheetName);
            exportParams.setCreateHeadRows(isCreateHeader);
            defaultExport(list, pojoClass, fileName, response, exportParams);
    
        }
    
        /***
         * 导出Excel
         * @param list 数据集合
         * @param title 标题名
         * @param sheetName sheet页名
         * @param pojoClass 主体类
         * @param fileName 文件名
         * @param response
         */
        public static void exportExcel(List list, String title, String sheetName, Class pojoClass,String fileName, HttpServletResponse response){
            defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
        }
    
        public static void exportExcel(List list, String title, String sheetName, Class pojoClass,String fileName, HttpServletResponse response,ExcelType excelType){
            defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName, excelType));
        }
    
        /**
         * 不管数据量多少,直接用SXSSFWorkbook 导出数据到excel
         * @param list
         * @param title
         * @param sheetName
         * @param pojoClass
         * @param fileName
         * @param response
         * @param excelType
         */
        public static void commonExportExcel(List list, String title, String sheetName, Class pojoClass,String fileName, HttpServletResponse response,ExcelType excelType) {
            ExportParams entity = new ExportParams(title, sheetName, excelType);
            Workbook workbook = new SXSSFWorkbook(1000);
            (new ExcelExportService()).createSheet(workbook, entity, pojoClass, list);
            if (workbook != null){
                downLoadExcel(fileName, response, workbook);
            }
        }
    
        /***
         * 导出Excel
         * @param list 数据集合
         * @param fileName 文件名
         * @param response
         */
        public static void exportExcel(List> list, String fileName, HttpServletResponse response){
            defaultExport(list, fileName, response);
        }
    
        private static void defaultExport(List list, Class pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
            if (workbook != null){
                downLoadExcel(fileName, response, workbook);
            }
        }
    
        private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
            try {
                response.setCharacterEncoding("UTF-8");
                response.setHeader("content-Type", "application/vnd.ms-excel");
                response.setHeader("Content-Disposition",
                        "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
                workbook.write(response.getOutputStream());
            } catch (IOException e) {
                throw new BusinessException(e.getMessage());
            }
        }
        private static void defaultExport(List> list, String fileName, HttpServletResponse response) {
            Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
            if (workbook != null) {
                ;
            }
            downLoadExcel(fileName, response, workbook);
        }
    
        public static  List importExcel(String filePath,Integer titleRows,Integer headerRows, Class pojoClass){
            if (StringUtils.isBlank(filePath)){
                return null;
            }
            ImportParams params = new ImportParams();
            params.setTitleRows(titleRows);
            params.setHeadRows(headerRows);
            List list = null;
            try {
                list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
            }catch (NoSuchElementException e){
                throw new BusinessException("模板不能为空");
            } catch (Exception e) {
                e.printStackTrace();
                throw new BusinessException(e.getMessage());
            }
            return list;
        }
    
        /***
         *
         * @param file 文件
         * @param titleRows 标题行
         * @param headerRows 投行
         * @param pojoClass class文件
         * @param 
         * @return
         */
        public static  List importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class pojoClass){
            if (file == null){
                return null;
            }
            ImportParams params = new ImportParams();
            params.setTitleRows(titleRows);
            params.setHeadRows(headerRows);
            List list = null;
            try {
                list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
            }catch (NoSuchElementException e){
                e.printStackTrace();
                throw new BusinessException("excel文件不能为空");
            } catch (Exception e) {
                e.printStackTrace();
                throw new BusinessException(e.getMessage());
            }
            return list;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288

    poi包的工具类,导入功能实现:

    List packageAgreementList1 = EasyPoiUtils.importExcel(file, 0, 1, PackageAgreementNewAluminumDto.class);
    
    • 1

    PackageAgreementNewAluminumDto为导入dto

    通用导出:

    EasyPoiUtils.exportExcel(list, "价格查询列表", "价格查询列表", PackagePriceExcelDto.class, "价格查询列表.xlsx", response, ExcelType.XSSF);
    
    • 1

    高效率大批量数据导出可以参考我前面写的博客。

    链接:https://blog.csdn.net/qq_37713521/article/details/129660313

  • 相关阅读:
    一种电磁兼容半电波暗室设计和实现
    Linux开发者的CI/CD(3)jenkins pipeline语法学习
    C++_基础语法
    java 注解反射项目实战
    理解nginx的 location 和root
    Python爬取天气数据并进行分析与预测
    「校园 Pie」 系列活动正式启航,首站走进南方科技大学!
    DDoS攻击、CC攻击
    【git】gitee的上传文件与报错
    RTL乒乓运算设计
  • 原文地址:https://blog.csdn.net/qq_37713521/article/details/133878256