导出Excel,支持最佳

列表信息导出为Excel文件,

依赖pom:

XML 复制代码
Sheet, Row:
   
<dependency>
     <groupId>org.apache.poi</groupId>
     <artifactId>poi</artifactId>
</dependency>


XSSFWorkbook
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
</dependency>
复制代码
实现代码: 

override表示是否覆盖现有文件,false,追加;true,覆盖。

java 复制代码
private <T> void exportExcelToLocal(List<T> list, Class<T> clazz) throws IOException {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }

        XSSFWorkbook workbook = null;
        File file = new File("src/main/resources/xxx.xlsx");
        FileInputStream fis = null;
        if (!file.exists()) {
            file.createNewFile();
        } else {
            if (!override) {
                fis = new FileInputStream(file);
                workbook = new XSSFWorkbook(fis);
            }
        }

        XSSFWorkbook workbookResult = ExcelUtils.exportExcelData(new XSSFWorkbook(), list, clazz, false);

        if (Objects.nonNull(workbook)) {
            Integer index = workbookResult.getSheetAt(0).getLastRowNum() + 1;

            Sheet sheet = workbookResult.getSheetAt(0);

            Sheet lastSheet = workbook.getSheetAt(0);

            for (int i = 1; i <= lastSheet.getLastRowNum(); ++i) {
                Row row = sheet.createRow(index++);

                for (int cellIndex = 0; cellIndex < lastSheet.getRow(i).getLastCellNum(); ++cellIndex) {
                    String value = lastSheet.getRow(i).getCell(cellIndex).getStringCellValue();
                    row.createCell(cellIndex).setCellValue(value);
                }
            }
        }


        FileOutputStream fos = new FileOutputStream(file);
        workbookResult.write(fos);
        fos.flush();
        fos.close();
        if (Objects.nonNull(fis)) {
            fis.close();
        }
    }

ExcelUtils:

单次导出操作

java 复制代码
public static <T> XSSFWorkbook exportExcelData(XSSFWorkbook workbook, List<T> data, Class<T> clazz, boolean template) {
        XSSFSheet sheet = workbook.createSheet();
        Row rowHeader = sheet.createRow(0);
        Map<Integer, Field> fieldMap = analysisAnnotation(clazz);

        for(int i = 0; i < fieldMap.size(); ++i) {
            Cell cell = rowHeader.createCell(i);
            Field field = (Field)fieldMap.get(i + 1);
            ExcelBind ea = (ExcelBind)field.getAnnotation(ExcelBind.class);
            cell.setCellValue(ea.columnName());
            cell.setCellStyle(setRowHeaderStyle(workbook));
            cell.setCellType(CellType.STRING);
        }

        if (!template) {
            analysisData(sheet, data, clazz, fieldMap);
        }

        return workbook;
    }



// 数据导出
private static <T> void analysisData(Sheet sheet, List<T> data, Class<T> clazz, Map<Integer, Field> fieldMap) {
        for(int i = 0; i < data.size(); ++i) {
            Row row = sheet.createRow(i + 1);
            T t = data.get(i);
            Class<?> aClass = t.getClass();

            for(int j = 0; j < fieldMap.size(); ++j) {
                Field declaredField = (Field)fieldMap.get(j + 1);
                declaredField.setAccessible(true);
                String key = declaredField.getName();
                Object value = "";

                try {
                    value = declaredField.get(t) == null ? "" : declaredField.get(t);
                } catch (IllegalAccessException var13) {
                    var13.printStackTrace();
                }

                if ("java.util.Date".equals(declaredField.getType().getName())) {
                    ExcelBind ea = (ExcelBind)declaredField.getAnnotation(ExcelBind.class);
                    if (StringUtils.isNotEmpty(ea.dateFormat())) {
                        value = (new SimpleDateFormat(ea.dateFormat())).format(value);
                    }
                }

                row.createCell(j).setCellValue(value.toString());
            }
        }

    }
相关推荐
Theodore_10223 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
冰帝海岸4 小时前
01-spring security认证笔记
java·笔记·spring
世间万物皆对象5 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
没书读了5 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
小二·5 小时前
java基础面试题笔记(基础篇)
java·笔记·python
开心工作室_kaic6 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
懒洋洋大魔王6 小时前
RocketMQ的使⽤
java·rocketmq·java-rocketmq
武子康6 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
转世成为计算机大神6 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式
qq_327342737 小时前
Java实现离线身份证号码OCR识别
java·开发语言