导出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());
            }
        }

    }
相关推荐
P.H. Infinity1 小时前
【RabbitMQ】04-发送者可靠性
java·rabbitmq·java-rabbitmq
生命几十年3万天1 小时前
java的threadlocal为何内存泄漏
java
caridle1 小时前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
^velpro^1 小时前
数据库连接池的创建
java·开发语言·数据库
苹果醋31 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
秋の花1 小时前
【JAVA基础】Java集合基础
java·开发语言·windows
小松学前端1 小时前
第六章 7.0 LinkList
java·开发语言·网络
Wx-bishekaifayuan2 小时前
django电商易购系统-计算机设计毕业源码61059
java·spring boot·spring·spring cloud·django·sqlite·guava
customer082 小时前
【开源免费】基于SpringBoot+Vue.JS周边产品销售网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源
全栈开发圈2 小时前
新书速览|Java网络爬虫精解与实践
java·开发语言·爬虫