根据实体excel导入导出百万数据,可修改表头名称

这篇具有很好参考价值的文章主要介绍了根据实体excel导入导出百万数据,可修改表头名称。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


表格导入导出实现效果展示

根据实体类导出模板

所有对excel都是根据实体类进行操作

根据实体excel导入导出百万数据,可修改表头名称

根据实体导出的excel模板展示
根据实体excel导入导出百万数据,可修改表头名称

读取表格数据

根据实体excel导入导出百万数据,可修改表头名称

读取结果返回,和表格上传数据一致
根据实体excel导入导出百万数据,可修改表头名称

导出数据为excel

也支持将已有的数据导出为表格
根据实体excel导入导出百万数据,可修改表头名称

进阶:修改表格导出的列头

部分情况,表头需要为中文,可以使用注解,对表格进行标注,导出的模板和导出的数据列头就是注解的内容了
根据实体excel导入导出百万数据,可修改表头名称
根据实体excel导入导出百万数据,可修改表头名称

controller示例

一下示例代码实现了表格模板导出、数据导出、数据读取和百万数据读取文章来源地址https://www.toymoban.com/news/detail-481312.html

package com.mabo.controller;


import com.alibaba.fastjson.JSONArray;
import com.mabo.entity.ChatInfo;
import com.mabo.util.ExcelHSSFUtil;
import com.monitorjbl.xlsx.StreamingReader;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.io.File;
/**
 * TODO your comment
 *
 * @author Yujiaqi
 * @date 2020/12/2 19:20
 */
@Slf4j
@RestController
@RequestMapping("/excel")
public class ExcelController {

    /**
     * 读取excel
     */
    @PostMapping("/upload")
    public Object upload(MultipartHttpServletRequest file) throws Exception {
        MultipartFile file1 = file.getFile("file");
        byte[] bytes = IOUtils.toByteArray(file1.getInputStream());
        File excelFile = new File("test1.xlsx");
        FileOutputStream fos = new FileOutputStream(excelFile);
        fos.write(bytes);
        fos.close();
        JSONArray  jsonArray = ExcelHSSFUtil.readXlsxExcel(ChatInfo.class, excelFile);
        return jsonArray;
    }
    /**
     * 下载excel模板
     */
    @GetMapping("/download")
    public void download(HttpServletResponse response) throws Exception {
        String fileName="downloadModel.xlsx";
        fileName = URLEncoder.encode(fileName,"UTF-8");
        File file=new File(fileName);
        file = ExcelHSSFUtil.createXlsxExcel(ChatInfo.class, new ArrayList<>(), file.getAbsolutePath());
        // 以流的形式下载文件。
        InputStream fis = null;
        try {
            fis = new BufferedInputStream(new FileInputStream(file.getAbsoluteFile()));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        // 清空response
        response.reset();
        // 设置response的Header
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes()));
        response.addHeader("Content-Length", "" + file.length());
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        response.setContentType("application/octet-stream;charset=utf-8");
        toClient.write(buffer);
        toClient.flush();
        toClient.close();
    }

    /**
     * 超大数据量上传
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadBatch")
    public Object uploadBatch(MultipartHttpServletRequest file) throws Exception {
        MultipartFile file1 = file.getFile("file");
        byte[] bytes = IOUtils.toByteArray(file1.getInputStream());
        File excelFile = new File("test1.xlsx");
        FileOutputStream fos = new FileOutputStream(excelFile);
        fos.write(bytes);
        fos.close();

        InputStream inputStream1 = new FileInputStream(excelFile);
        JSONArray jsonArray = null;
        Workbook work= StreamingReader.builder()
                .rowCacheSize(100)  //缓存到内存中的行数,默认是10
                .bufferSize(4096)  //读取资源时,缓存到内存的字节大小,默认是1024
                .open(inputStream1);
        Sheet sheet = work.getSheetAt(0);//得到第一个sheet
        int excelSize = sheet.getLastRowNum();
        int max =3;
        if (excelSize < max) {
            jsonArray = ExcelHSSFUtil.readXlsxExcel(ChatInfo.class, excelFile);
            System.out.println(jsonArray);
        }else {
            //大数据进行多线程处理,并且直接返回数据
            int size=max;
            for (int i = 1; i < excelSize; ) {
                log.info("当前为第" + i);
                jsonArray = ExcelHSSFUtil.readXlsxExcelCache(ChatInfo.class, excelFile, i, size);
                System.out.println(jsonArray);
                i+=max;
                if (i+max>excelSize){
                    jsonArray = ExcelHSSFUtil.readXlsxExcelCache(ChatInfo.class, excelFile,i,excelSize-i);
                    System.out.println(jsonArray);
                }
            }

        }
        return jsonArray;
    }



    /**
     * 下载excel批量数据
     */
    @GetMapping("/downloadBatch")
    public void downloadBatch(HttpServletResponse response) throws Exception {
        String fileName="downloadBatch.xlsx";
        fileName = URLEncoder.encode(fileName,"UTF-8");
        File file=new File(fileName);
        List list = new ArrayList<>();
        ChatInfo chatInfo = new ChatInfo();
        chatInfo.setMsg("1");
        chatInfo.setId("1");
        //写入业务数据
        list.add(chatInfo);
        list.add(chatInfo);
        list.add(chatInfo);
        file = ExcelHSSFUtil.createXlsxExcel(ChatInfo.class, list, file.getAbsolutePath());
        // 以流的形式下载文件。
        InputStream fis = null;
        try {
            fis = new BufferedInputStream(new FileInputStream(file.getAbsoluteFile()));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        // 清空response
        response.reset();
        // 设置response的Header
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes()));
        response.addHeader("Content-Length", "" + file.length());
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        response.setContentType("application/octet-stream;charset=utf-8");
        toClient.write(buffer);
        toClient.flush();
        toClient.close();
    }

}

工具类

package com.mabo.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mabo.annotation.ExcelField;
import com.monitorjbl.xlsx.StreamingReader;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
public class ExcelHSSFUtil<T> {

    //日期支持以下以下格式
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy.MM.dd");
    private static SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy年MM月dd日");

    public static <T> File create(Class<T> aClass, List<T> list, String fileName) throws IOException {
        // 创建一个webbook,对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFCellStyle textType = wb.createCellStyle();
        HSSFCellStyle dateType = wb.createCellStyle();
        HSSFDataFormat format = wb.createDataFormat();
        textType.setDataFormat(format.getFormat("@"));
        dateType.setDataFormat(format.getFormat("yyyy年m月d日"));

        // 在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("sheet1");
        // 在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow(0);
        // 添加标题行
        HSSFCell cell = null;
        Field[] fields = aClass.getDeclaredFields();
        List<Field> excelField = new ArrayList<>();
        int excelNo = 0;
        for (int i = 0; i < fields.length; i++) {
            ExcelField annotation = fields[i].getAnnotation(ExcelField.class);
            if (annotation != null) {
                if (annotation.ignore() == true) {
                    continue;
                } else {
                    excelField.add(fields[i]);
                    // 获取行内对应单元格
                    cell = row.createCell(excelNo++);
                    // 单元格赋值
                    String value = annotation.value();
                    if (value.equals("")) {
                        cell.setCellValue(fields[i].getName());
                    } else {
                        cell.setCellValue(value);
                    }
                }
            } else {
                cell = row.createCell(excelNo++);
                cell.setCellValue(fields[i].getName());
            }
        }
        // 写入实体数据,实际应用中这些数据从数据库得到,list中字符串的顺序必须和数组strArray中的顺序一致
        int i = 0;
        for (int j = 0; j < list.size(); j++) {
            T t = list.get(i);
            i++;
            row = sheet.createRow(i);
            // 添加数据行
            //数据转为Json
            String json = JSON.toJSONString(t);//关键
            JSONObject parse = (JSONObject) JSONObject.parse(json);
            for (int z = 0; z < excelField.size(); z++) {
                Field field = excelField.get(z);
                ExcelField annotation = field.getAnnotation(ExcelField.class);
                boolean ignore = false;
                if (annotation != null) {
                    ignore = annotation.ignore();
                }
                if (!ignore) {
                    cell = row.createCell(z);
                    cell.setCellStyle(textType);
                    // 获取行内对应单元格
                    String name = field.getName();
                    Object o = parse.get(name);
                    // 单元格赋值
                    if (o instanceof Long) {
                        long o1 = (long) o;
                        Date date = null;
                        SimpleDateFormat simpleDateFormat = null;
                        try {
                            date = new Date();
                            date.setTime(o1);
                            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            cell.setCellValue(simpleDateFormat.format(date));
                        } catch (Exception e) {
                            e.printStackTrace();
                            cell.setCellValue(o1);
                        }

                    } else if (o instanceof String) {
                        cell.setCellValue((String) o);
                    } else if (o instanceof Double) {
                        cell.setCellValue((double) o);
                    } else if (o instanceof Boolean) {
                        cell.setCellValue((boolean) o);
                    }
                }

            }
        }
//            // 第六步,将文件存到指定位置
        FileOutputStream fout = new FileOutputStream(fileName);
        try {
            wb.write(fout);
            fout.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            fout.close();
        }

        return new File(fileName);
    }

    /**
     * @Description : 读取excel为实体集合
     * @Author : mabo
     */

    public static <T> JSONArray readExcel(Class<T> aClass, File file) {
        JSONArray array = new JSONArray();
        try {
            FileInputStream fileInputStream = new FileInputStream(file.getAbsolutePath());
            HSSFWorkbook work = new HSSFWorkbook(fileInputStream);// 得到这个excel表格对象
            HSSFSheet sheet = work.getSheetAt(0); //得到第一个sheet
            int rowNo = sheet.getLastRowNum(); //得到行数
            //获取首行列头
            HSSFRow row = sheet.getRow(0);
            short lastCellNum = row.getLastCellNum();
            List<String> fieldNames = new ArrayList<>();
            for (int i = 0; i < lastCellNum; i++) {
                HSSFCell cell = row.getCell(i);
                if (cell != null) {
                    String stringCellValue = cell.getStringCellValue();
                    fieldNames.add(stringCellValue);
                }
            }
            JSONObject jsonField = getJsonField(aClass);
            for (int i = 1; i <= rowNo; i++) {
                row = sheet.getRow(i);
                JSONObject jsonObject = new JSONObject();
                for (int j = 0; j < fieldNames.size(); j++) {
                    HSSFCell cell = row.getCell(j);
                    if (cell != null) {
                        Object value = null;
                        CellType cellTypeEnum = cell.getCellTypeEnum();
                        if (cellTypeEnum.equals(CellType.STRING)) {
                            value = cell.getStringCellValue();
//                            try {
//                                value = simpleDateFormat.parse(value.toString());
//                            } catch (ParseException e) {
//                                try {
//                                    value = sdf1.parse(value.toString());
//                                } catch (ParseException e1) {
//                                    try {
//                                        value = sdf2.parse(value.toString());
//                                    } catch (ParseException e2) {
//                                        try {
//                                            value = sdf3.parse(value.toString());
//                                        } catch (ParseException e3) {
//                                        }
//                                    }
//                                }
//                            }
                        } else if (cellTypeEnum.equals(CellType.NUMERIC)) {
                            value = cell.getNumericCellValue();
                        } else if (cellTypeEnum.equals(CellType.BOOLEAN)) {
                            value = cell.getBooleanCellValue();
                        }
                        String string = jsonField.getString(fieldNames.get(j));
                        jsonObject.put(string, value);
                    }
                }
                array.add(jsonObject);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return array;
    }

    /**
     * @Description : 获取表格列头和实体的对应关系
     * @Author : mabo
     */
    public static <T> JSONObject getJsonField(Class<T> aClass) {
        Field[] fields = aClass.getDeclaredFields();
        JSONObject js = new JSONObject();
        for (int i = 0; i < fields.length; i++) {
            ExcelField annotation = fields[i].getAnnotation(ExcelField.class);
            if (annotation != null) {
                if (annotation.ignore() == true) {
                    continue;
                } else {
                    if (!annotation.value().equals("")) {
                        String value = annotation.value();
                        js.put(value, fields[i].getName());

                    } else {
                        js.put(fields[i].getName(), fields[i].getName());
                    }
                }
            } else {
                js.put(fields[i].getName(), fields[i].getName());
            }
        }
        return js;
    }


    /**
     * @Description : 生成xlsx格式表格文件,可上传的数据量更大
     */

    public static <T> File createXlsxExcel(Class<T> aClass, List<T> list, String fileName) throws Exception {
        // 创建一个webbook,对应一个Excel文件
//        Workbook wb =  WorkbookFactory.create(new File(fileName));
        Workbook wb = new SXSSFWorkbook(3000);
        CellStyle textType = wb.createCellStyle();
        CellStyle dateType = wb.createCellStyle();
        DataFormat dataFormat = wb.createDataFormat();
        textType.setDataFormat(dataFormat.getFormat("@"));
        dateType.setDataFormat(dataFormat.getFormat("yyyy年m月d日"));

        // 在webbook中添加一个sheet,对应Excel文件中的sheet
        Sheet sheet = wb.createSheet("sheet1");
        // 在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        Row row = sheet.createRow(0);
        // 添加标题行
        Cell cell = null;
        Field[] fields = aClass.getDeclaredFields();
        List<Field> excelField = new ArrayList<>();
        int excelNo = 0;
        for (int i = 0; i < fields.length; i++) {
            ExcelField annotation = fields[i].getAnnotation(ExcelField.class);
            if (annotation != null) {
                if (annotation.ignore() == true) {
                    continue;
                } else {
                    excelField.add(fields[i]);
                    // 获取行内对应单元格
                    cell = row.createCell(excelNo++);
                    // 单元格赋值
                    String value = annotation.value();
                    if (value.equals("")) {
                        cell.setCellValue(fields[i].getName());
                    } else {
                        cell.setCellValue(value);
                    }
                }
            } else {
                excelField.add(fields[i]);
                cell = row.createCell(excelNo++);
                cell.setCellValue(fields[i].getName());
            }
        }
        // 写入实体数据,实际应用中这些数据从数据库得到,list中字符串的顺序必须和数组strArray中的顺序一致
        int i = 0;
        for (int j = 0; j < list.size(); j++) {
            T t = list.get(i);
            i++;
            row = sheet.createRow(i);
            // 添加数据行
            //数据转为Json
            String json = JSON.toJSONString(t);//关键
            JSONObject parse = (JSONObject) JSONObject.parse(json);
            for (int z = 0; z < excelField.size(); z++) {
                Field field = excelField.get(z);
                ExcelField annotation = field.getAnnotation(ExcelField.class);
                boolean ignore = false;
                if (annotation != null) {
                    ignore = annotation.ignore();
                }
                if (!ignore) {
                    cell = row.createCell(z);
                    cell.setCellStyle(textType);
                    // 获取行内对应单元格
                    String name = field.getName();
                    Object o = parse.get(name);
                    // 单元格赋值
                    if (o instanceof Long) {
                        long o1 = (long) o;
                        Date date = null;
                        SimpleDateFormat simpleDateFormat = null;
                        try {
                            date = new Date();
                            date.setTime(o1);
                            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            cell.setCellValue(simpleDateFormat.format(date));
                        } catch (Exception e) {
                            e.printStackTrace();
                            cell.setCellValue(o1);
                        }

                    } else if (o instanceof String) {
                        cell.setCellValue((String) o);
                    } else if (o instanceof Double) {
                        cell.setCellValue((double) o);
                    } else if (o instanceof Boolean) {
                        cell.setCellValue((boolean) o);
                    }
                }

            }
        }
//            // 第六步,将文件存到指定位置
        FileOutputStream fout = new FileOutputStream(fileName);
        try {
            wb.write(fout);
            fout.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            fout.close();
        }

        return new File(fileName);
    }

    public static <T> JSONArray readXlsxExcel(Class<T> aClass, File file) {
        JSONArray array = new JSONArray();
        try {
            Workbook work = new XSSFWorkbook(new FileInputStream(file.getAbsolutePath()));// 得到这个excel表格对象
            Sheet sheet = work.getSheetAt(0);//得到第一个sheet
            int rowNo = sheet.getLastRowNum(); //得到行数
            //获取首行列头
            Row row = sheet.getRow(0);
            short lastCellNum = row.getLastCellNum();
            List<String> fieldNames = new ArrayList<>();
            for (int i = 0; i < lastCellNum; i++) {
                Cell cell = row.getCell(i);
                if (cell != null) {
                    String stringCellValue = cell.getStringCellValue();
                    fieldNames.add(stringCellValue);
                }
            }
            JSONObject jsonField = getJsonField(aClass);
            for (int i = 1; i <= rowNo; i++) {
                row = sheet.getRow(i);
                JSONObject jsonObject = new JSONObject();
                for (int j = 0; j < fieldNames.size(); j++) {
                    Cell cell = row.getCell(j);
                    if (cell != null) {
                        Object value = null;
                        CellType cellTypeEnum = cell.getCellTypeEnum();
                        if (cellTypeEnum.equals(CellType.STRING)) {
                            value = cell.getStringCellValue();
//                            try {
//                                value = simpleDateFormat.parse(value.toString());
//                            } catch (ParseException e) {
//                                try {
//                                    value = sdf1.parse(value.toString());
//                                } catch (ParseException e1) {
//                                    try {
//                                        value = sdf2.parse(value.toString());
//                                    } catch (ParseException e2) {
//                                        try {
//                                            value = sdf3.parse(value.toString());
//                                        } catch (ParseException e3) {
//                                        }
//                                    }
//                                }
//                            }
                        } else if (cellTypeEnum.equals(CellType.NUMERIC)) {
                            value = cell.getNumericCellValue();
                        } else if (cellTypeEnum.equals(CellType.BOOLEAN)) {
                            value = cell.getBooleanCellValue();
                        }
                        String string = jsonField.getString(fieldNames.get(j));
                        jsonObject.put(string, value);
                    }
                }
                array.add(jsonObject);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return array;
    }

    public static <T> JSONArray readXlsxExcel(Class<T> aClass, File file, int start, int size) {
        JSONArray array = new JSONArray();
        try {
            Workbook work = new XSSFWorkbook(new FileInputStream(file.getAbsolutePath()));// 得到这个excel表格对象
            Sheet sheet = work.getSheetAt(0);//得到第一个sheet
            int rowNo = sheet.getLastRowNum(); //得到行数
            if (rowNo > start + size) {
                rowNo = start + size;
            }
            //获取首行列头
            Row row = sheet.getRow(0);
            short lastCellNum = row.getLastCellNum();
            List<String> fieldNames = new ArrayList<>();
            for (int i = 0; i < lastCellNum; i++) {
                Cell cell = row.getCell(i);
                if (cell != null) {
                    String stringCellValue = cell.getStringCellValue();
                    fieldNames.add(stringCellValue);
                }
            }
            JSONObject jsonField = getJsonField(aClass);
            //从数据行开始
            start++;
            for (int i = start; i <= rowNo; i++) {
                row = sheet.getRow(i);
                JSONObject jsonObject = new JSONObject();
                for (int j = 0; j < fieldNames.size(); j++) {
                    Cell cell = row.getCell(j);
                    if (cell != null) {
                        Object value = null;
                        CellType cellTypeEnum = cell.getCellTypeEnum();
                        if (cellTypeEnum.equals(CellType.STRING)) {
                            value = cell.getStringCellValue();
//                            try {
//                                value = simpleDateFormat.parse(value.toString());
//                            } catch (ParseException e) {
//                                try {
//                                    value = sdf1.parse(value.toString());
//                                } catch (ParseException e1) {
//                                    try {
//                                        value = sdf2.parse(value.toString());
//                                    } catch (ParseException e2) {
//                                        try {
//                                            value = sdf3.parse(value.toString());
//                                        } catch (ParseException e3) {
//                                        }
//                                    }
//                                }
//                            }
                        } else if (cellTypeEnum.equals(CellType.NUMERIC)) {
                            value = cell.getNumericCellValue();
                        } else if (cellTypeEnum.equals(CellType.BOOLEAN)) {
                            value = cell.getBooleanCellValue();
                        }
                        String string = jsonField.getString(fieldNames.get(j));
                        jsonObject.put(string, value);
                    }
                }
                array.add(jsonObject);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return array;
    }

    public static <T> JSONArray readXlsxExcelCache(Class<T> aClass, File file, int start, int size) throws FileNotFoundException {
        InputStream inputStream1 = new FileInputStream(file);
        Workbook work = StreamingReader.builder()
                .rowCacheSize(100)  //缓存到内存中的行数,默认是10
                .bufferSize(4096)  //读取资源时,缓存到内存的字节大小,默认是1024
                .open(inputStream1);
        Sheet sheet = work.getSheetAt(0);//得到第一个sheet
        int excelSize = sheet.getLastRowNum();
        //大数据进行多线程处理,并且直接返回数据
        log.info("当前数据量大小为" + excelSize);
        List<String> fieldNames = new ArrayList<>();
        JSONArray array = new JSONArray();
        JSONObject jsonField = ExcelHSSFUtil.getJsonField(aClass);
        for (Row row : sheet) {
            if (row.getRowNum() == 0) {
                short lastCellNum = row.getLastCellNum();
                for (int i = 0; i < lastCellNum; i++) {
                    Cell cell = row.getCell(i);
                    if (cell != null) {
                        String stringCellValue = cell.getStringCellValue();
                        fieldNames.add(stringCellValue);
                    }
                }
            }
            int maxSize = start + size;
            if (row.getRowNum() >= start && row.getRowNum()<maxSize) { //从设定的行开始取值
                //对当前行逐列进行循环取值
                JSONObject jsonObject = new JSONObject();
                for (int j = 0; j < fieldNames.size(); j++) {
                    Cell cell = row.getCell(j);
                    if (cell != null) {
                        Object value = null;
                        CellType cellTypeEnum = cell.getCellTypeEnum();
                        if (cellTypeEnum.equals(CellType.STRING)) {
                            value = cell.getStringCellValue();
//                            try {
//                                value = simpleDateFormat.parse(value.toString());
//                            } catch (ParseException e) {
//                                try {
//                                    value = sdf1.parse(value.toString());
//                                } catch (ParseException e1) {
//                                    try {
//                                        value = sdf2.parse(value.toString());
//                                    } catch (ParseException e2) {
//                                        try {
//                                            value = sdf3.parse(value.toString());
//                                        } catch (ParseException e3) {
//                                        }
//                                    }
//                                }
//                            }
                        } else if (cellTypeEnum.equals(CellType.NUMERIC)) {
                            value = cell.getNumericCellValue();
                        } else if (cellTypeEnum.equals(CellType.BOOLEAN)) {
                            value = cell.getBooleanCellValue();
                        }
                        String string = jsonField.getString(fieldNames.get(j));
                        jsonObject.put(string, value);
                    }
                }
                array.add(jsonObject);
            }
            if (row.getRowNum()>maxSize){
                break;
            }
        }
        return array;
    }


    public static int getExcelSize(File file) throws IOException {
//        Workbook work = new XSSFWorkbook(new FileInputStream(file.getAbsolutePath()));// 得到这个excel表格对象
//        Sheet sheet = work.getSheetAt(0);//得到第一个sheet
//        return  sheet.getLastRowNum(); //得到行数
        InputStream inputStream1 = new FileInputStream(file);
        Workbook work= StreamingReader.builder()
                .rowCacheSize(100)  //缓存到内存中的行数,默认是10
                .bufferSize(4096)  //读取资源时,缓存到内存的字节大小,默认是1024
                .open(inputStream1);
        Sheet sheet = work.getSheetAt(0);//得到第一个sheet
        int excelSize = sheet.getLastRowNum();
        return excelSize;
    }

    /**
     * @Description : 生成xlsx格式表格文件,可上传的数据量更大
     */

    public static <T> File createXlsxExcelCache(Class<T> aClass, List<T> list, String fileName,int startRow) throws Exception {
        // 创建一个webbook,对应一个Excel文件
//        File file = new File(fileName);
//        XSSFWorkbook tplWorkBook = new XSSFWorkbook(new FileInputStream(file));
        Workbook wb = new SXSSFWorkbook( 3000);
//
//        InputStream inputStream1 = new FileInputStream(file);
//        Workbook wb =   new XSSFWorkbook(inputStream1);
//        wb = new SXSSFWorkbook(wb,3000);
        CellStyle textType = wb.createCellStyle();
        CellStyle dateType = wb.createCellStyle();
        DataFormat dataFormat = wb.createDataFormat();
        textType.setDataFormat(dataFormat.getFormat("@"));
        dateType.setDataFormat(dataFormat.getFormat("yyyy年m月d日"));
        // 在webbook中添加一个sheet,对应Excel文件中的sheet
        Sheet sheet = wb.createSheet("sheet1");
        // 在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        List<Field> excelField = new ArrayList<>();
        Row row =null;
        Cell cell = null;
        if (startRow==0){
            row = sheet.createRow(0);
            // 添加标题行
            Field[] fields = aClass.getDeclaredFields();
            int excelNo = 0;
            for (int i = 0; i < fields.length; i++) {
                ExcelField annotation = fields[i].getAnnotation(ExcelField.class);
                if (annotation != null) {
                    if (annotation.ignore() == true) {
                        continue;
                    } else {
                        excelField.add(fields[i]);
                        // 获取行内对应单元格
                        cell = row.createCell(excelNo++);
                        // 单元格赋值
                        String value = annotation.value();
                        if (value.equals("")) {
                            cell.setCellValue(fields[i].getName());
                        } else {
                            cell.setCellValue(value);
                        }
                    }
                } else {
                    excelField.add(fields[i]);
                    cell = row.createCell(excelNo++);
                    cell.setCellValue(fields[i].getName());
                }
            }
        }else {
            // 不添加标题行,只获取数
            Field[] fields = aClass.getDeclaredFields();
            int excelNo = 0;
            for (int i = 0; i < fields.length; i++) {
                ExcelField annotation = fields[i].getAnnotation(ExcelField.class);
                if (annotation != null) {
                    if (annotation.ignore() == true) {
                        continue;
                    } else {
                        excelField.add(fields[i]);
                    }
                } else {
                    excelField.add(fields[i]);
                }
            }
        }

        // 写入实体数据,实际应用中这些数据从数据库得到,list中字符串的顺序必须和数组strArray中的顺序一致
        int i = startRow;
        for (int j = 0; j < list.size(); j++) {
            T t = list.get(j);
            i++;
            row = sheet.createRow(i);
            // 添加数据行
            //数据转为Json
            String json = JSON.toJSONString(t);//关键
            JSONObject parse = (JSONObject) JSONObject.parse(json);
            for (int z = 0; z < excelField.size(); z++) {
                Field field = excelField.get(z);
                ExcelField annotation = field.getAnnotation(ExcelField.class);
                boolean ignore = false;
                if (annotation != null) {
                    ignore = annotation.ignore();
                }
                if (!ignore) {
                    cell = row.createCell(z);
                    cell.setCellStyle(textType);
                    // 获取行内对应单元格
                    String name = field.getName();
                    Object o = parse.get(name);
                    // 单元格赋值
                    if (o instanceof Long) {
                        long o1 = (long) o;
                        Date date = null;
                        SimpleDateFormat simpleDateFormat = null;
                        try {
                            date = new Date();
                            date.setTime(o1);
                            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            cell.setCellValue(simpleDateFormat.format(date));
                        } catch (Exception e) {
                            e.printStackTrace();
                            cell.setCellValue(o1);
                        }

                    } else if (o instanceof String) {
                        cell.setCellValue((String) o);
                    } else if (o instanceof Double) {
                        cell.setCellValue((double) o);
                    } else if (o instanceof Boolean) {
                        cell.setCellValue((boolean) o);
                    }
                }

            }
        }
//            // 第六步,将文件存到指定位置
        FileOutputStream fout = new FileOutputStream(fileName);
        try {
            wb.write(fout);
            fout.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            fout.close();
        }

        return new File(fileName);
    }

    public static <T> File appendExcelDataWithCache(Class<T> aClass, List<T> list, String fileName,int startRow) throws Exception {
        FileInputStream input = new FileInputStream(fileName);
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(input);
        //构建SXSSF,设置模板和内存保留行数Workbook   wb = new SXSSFWorkbook(xssfWorkbook,100);
        // 创建一个webbook,对应一个Excel文件
        Workbook wb = new SXSSFWorkbook( xssfWorkbook,3000);
        CellStyle textType = wb.createCellStyle();
        CellStyle dateType = wb.createCellStyle();
        DataFormat dataFormat = wb.createDataFormat();
        textType.setDataFormat(dataFormat.getFormat("@"));
        dateType.setDataFormat(dataFormat.getFormat("yyyy年m月d日"));
        // 在webbook中添加一个sheet,对应Excel文件中的sheet
        Sheet sheet = wb.getSheet("sheet1");
        if (sheet==null){
            sheet= wb.createSheet("sheet1");
        }
        // 在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        List<Field> excelField = new ArrayList<>();
        Row row =null;
        Cell cell = null;
        if (startRow==0){
            row = sheet.createRow(0);
            // 添加标题行
            Field[] fields = aClass.getDeclaredFields();
            int excelNo = 0;
            for (int i = 0; i < fields.length; i++) {
                ExcelField annotation = fields[i].getAnnotation(ExcelField.class);
                if (annotation != null) {
                    if (annotation.ignore() == true) {
                        continue;
                    } else {
                        excelField.add(fields[i]);
                        // 获取行内对应单元格
                        cell = row.createCell(excelNo++);
                        // 单元格赋值
                        String value = annotation.value();
                        if (value.equals("")) {
                            cell.setCellValue(fields[i].getName());
                        } else {
                            cell.setCellValue(value);
                        }
                    }
                } else {
                    excelField.add(fields[i]);
                    cell = row.createCell(excelNo++);
                    cell.setCellValue(fields[i].getName());
                }
            }
        }else {
            // 不添加标题行,只获取数
            Field[] fields = aClass.getDeclaredFields();
            int excelNo = 0;
            for (int i = 0; i < fields.length; i++) {
                ExcelField annotation = fields[i].getAnnotation(ExcelField.class);
                if (annotation != null) {
                    if (annotation.ignore() == true) {
                        continue;
                    } else {
                        excelField.add(fields[i]);
                    }
                } else {
                    excelField.add(fields[i]);
                }
            }
        }

        // 写入实体数据,实际应用中这些数据从数据库得到,list中字符串的顺序必须和数组strArray中的顺序一致
        int i = startRow;
        for (int j = 0; j < list.size(); j++) {
            T t = list.get(j);
            i++;
            row = sheet.createRow(i);
            // 添加数据行
            //数据转为Json
            String json = JSON.toJSONString(t);//关键
            JSONObject parse = (JSONObject) JSONObject.parse(json);
            for (int z = 0; z < excelField.size(); z++) {
                Field field = excelField.get(z);
                ExcelField annotation = field.getAnnotation(ExcelField.class);
                boolean ignore = false;
                if (annotation != null) {
                    ignore = annotation.ignore();
                }
                if (!ignore) {
                    cell = row.createCell(z);
                    cell.setCellStyle(textType);
                    // 获取行内对应单元格
                    String name = field.getName();
                    Object o = parse.get(name);
                    // 单元格赋值
                    if (o instanceof Long) {
                        long o1 = (long) o;
                        Date date = null;
                        SimpleDateFormat simpleDateFormat = null;
                        try {
                            date = new Date();
                            date.setTime(o1);
                            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            cell.setCellValue(simpleDateFormat.format(date));
                        } catch (Exception e) {
                            e.printStackTrace();
                            cell.setCellValue(o1);
                        }

                    } else if (o instanceof String) {
                        cell.setCellValue((String) o);
                    } else if (o instanceof Double) {
                        cell.setCellValue((double) o);
                    } else if (o instanceof Boolean) {
                        cell.setCellValue((boolean) o);
                    }
                }

            }
        }
//            // 第六步,将文件存到指定位置
        FileOutputStream fout = new FileOutputStream(fileName);
        try {
            wb.write(fout);
            fout.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            fout.close();
        }

        return new File(fileName);
    }


    /**
     * @Description : 向excel中追加数据,不影响原来数据
     * @Author : mabo
     */
    public static <T> Workbook appendExcelDataWithCache( Workbook wb,Class<T> aClass, List<T> list,int startRow) throws Exception {
        CellStyle textType = wb.createCellStyle();
        CellStyle dateType = wb.createCellStyle();
        DataFormat dataFormat = wb.createDataFormat();
        textType.setDataFormat(dataFormat.getFormat("@"));
        dateType.setDataFormat(dataFormat.getFormat("yyyy年m月d日"));
        // 在webbook中添加一个sheet,对应Excel文件中的sheet
        Sheet sheet = wb.getSheet("sheet1");
        if (sheet==null){
            sheet= wb.createSheet("sheet1");
        }
        // 在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        List<Field> excelField = new ArrayList<>();
        Row row =null;
        Cell cell = null;
        if (startRow==0){
            row = sheet.createRow(0);
            // 添加标题行
            Field[] fields = aClass.getDeclaredFields();
            int excelNo = 0;
            for (int i = 0; i < fields.length; i++) {
                ExcelField annotation = fields[i].getAnnotation(ExcelField.class);
                if (annotation != null) {
                    if (annotation.ignore() == true) {
                        continue;
                    } else {
                        excelField.add(fields[i]);
                        // 获取行内对应单元格
                        cell = row.createCell(excelNo++);
                        // 单元格赋值
                        String value = annotation.value();
                        if (value.equals("")) {
                            cell.setCellValue(fields[i].getName());
                        } else {
                            cell.setCellValue(value);
                        }
                    }
                } else {
                    excelField.add(fields[i]);
                    cell = row.createCell(excelNo++);
                    cell.setCellValue(fields[i].getName());
                }
            }
        }else {
            // 不添加标题行,只获取数
            Field[] fields = aClass.getDeclaredFields();
            int excelNo = 0;
            for (int i = 0; i < fields.length; i++) {
                ExcelField annotation = fields[i].getAnnotation(ExcelField.class);
                if (annotation != null) {
                    if (annotation.ignore() == true) {
                        continue;
                    } else {
                        excelField.add(fields[i]);
                    }
                } else {
                    excelField.add(fields[i]);
                }
            }
        }

        // 写入实体数据,实际应用中这些数据从数据库得到,list中字符串的顺序必须和数组strArray中的顺序一致
        int i = startRow;
        for (int j = 0; j < list.size(); j++) {
            T t = list.get(j);
            i++;
            row = sheet.createRow(i);
            // 添加数据行
            //数据转为Json
            String json = JSON.toJSONString(t);//关键
            JSONObject parse = (JSONObject) JSONObject.parse(json);
            for (int z = 0; z < excelField.size(); z++) {
                Field field = excelField.get(z);
                ExcelField annotation = field.getAnnotation(ExcelField.class);
                boolean ignore = false;
                if (annotation != null) {
                    ignore = annotation.ignore();
                }
                if (!ignore) {
                    cell = row.createCell(z);
                    cell.setCellStyle(textType);
                    // 获取行内对应单元格
                    String name = field.getName();
                    Object o = parse.get(name);
                    // 单元格赋值
                    if (o instanceof Long) {
                        long o1 = (long) o;
                        Date date = null;
                        SimpleDateFormat simpleDateFormat = null;
                        try {
                            date = new Date();
                            date.setTime(o1);
                            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            cell.setCellValue(simpleDateFormat.format(date));
                        } catch (Exception e) {
                            e.printStackTrace();
                            cell.setCellValue(o1);
                        }

                    } else if (o instanceof String) {
                        cell.setCellValue((String) o);
                    } else if (o instanceof Double) {
                        cell.setCellValue((double) o);
                    } else if (o instanceof Boolean) {
                        cell.setCellValue((boolean) o);
                    }
                }

            }
        }
        return wb;
    }
}


测试实体

package com.mabo.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.mabo.annotation.ExcelField;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.Serializable;
import java.util.Date;

/**
 * (ChatInfo)实体类
 *
 * @author makejava
 * @since 2022-08-01 16:18:08
 */
@Data
public class ChatInfo implements Serializable {
    /**
     * 主键
     */
    @ExcelField("主键id")
    private String id;
    /**
     * 消息
     */
    @ExcelField("消息")
    private String msg;

}


实体注解

package com.mabo.annotation;

import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ExcelField {
    String value() default "";
    boolean ignore() default false;
}

maven依赖

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.22</version>
        </dependency>


        <!-- 07版本以后的格式 .xlsx -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
        <!-- 读取大量excel数据时使用 -->
        <dependency>
            <groupId>com.monitorjbl</groupId>
            <artifactId>xlsx-streamer</artifactId>
            <version>2.1.0</version>
        </dependency>

到了这里,关于根据实体excel导入导出百万数据,可修改表头名称的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用EasyExcel实现excel导出,支持百万大数据量导出-----超简单

    通过设置sheet数量,完成分批导出,每个sheet存100万数据,每次查询插入20万数据,避免超时,内存溢出等问题,可以根据服务器配置调整参数设置。 1.引入依赖 2.创建对应的实体类 @ExcelProperty设置的就是导出的列名,还可以设置排序等等 3.核心导出代码 4.配置类 至此就完成导

    2024年02月11日
    浏览(46)
  • Java,excel大量百万数据导出优化措施,SXSSFWorkbook流式、分批次导出示例

    在导出百万级的数据时,如果不采用适当的优化措施,确实可能会造成死机和内存崩溃等问题。 为避免这些问题,可以采用以下优化措施: 分批次读取数据:将需要导出的数据分成多个批次进行读取和写入,每次读取部分数据,写入 Excel 后即时清除内存。这样可以避免一次

    2024年02月16日
    浏览(26)
  • java根据excel模板进行导出数据

     一、pom文件添加以下依赖 二、添加util包 三、在resources目录下添加template并添加xlsx模板  注意:xlsx模板使用${list.XXX}     XXX表示数据源list(map形式的list)的数据key值,如果list是对象形式的,那么就是该业务字段  四、业务层使用:

    2024年02月11日
    浏览(31)
  • Go 单表头结构体导入导出

    首先感谢 github.com/xuri/excelize/v2 基础库对excel表格的支持,本文在此基础上进行功能的api扩展方便,(web/其他)有结构体导出与导入需求的封装优化,当前版本 v0.0.2 安装 当前版本v0.0.2,也可指定版本安装,以防版本变化 结构体标签 excelName 列名 excelIndex 列序号 toExcelFormat 列转

    2023年04月18日
    浏览(17)
  • 【前端】批量导入和导出Excel数据

    excel导入功能需要使用npm包 xlsx ,所以需要安装 xlsx 插件,读取和写入都依赖她 vue-element-admin模板提供了一个导入excel数据的文件,我们只需用即可 代码地址: https://github.com/PanJiaChen/vue-element-admin/blob/master/src/components/UploadExcel/index.vue 将vue-element-admin提供的导入功能新建一个组件

    2024年02月09日
    浏览(32)
  • hutool 导出复杂表头excel

    假如已这样的表头导出数据  1.把包含表头的excel添加到项目资源目录  2.编写代码读取表头所在sheet,并且加入需导出的数据

    2024年02月13日
    浏览(29)
  • Java导出Excel模板,导出数据到指定模板,通过模板导入数据(一)

    本文章主要是介绍阿里巴巴的easyexcel的使用 1. 首先需要我们导入easyexcel的依赖包 2. 前期工作准备 编写相关导出模板和导入模板。在项目的resources下创建文件夹,命名为excel 导出模板(此处仅做示例,字段根据自己项目来):  导入模板(导入时需要哪些字段根据自己项目业

    2024年02月03日
    浏览(33)
  • java中用SXSSFWorkbook把多个list数据和单个实体dto导出到excel如何导出到多个sheet页详细实例?(亲测)

    以下是一个详细的示例,展示了如何使用SXSSFWorkbook将多个List数据和单个实体DTO导出到多个Sheet页: import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFCell; import java.io.FileOutputStream;

    2024年02月11日
    浏览(30)
  • 积木报表Excel数据量大导出慢导不出问题、大量数据导不出问题优化方案和分析解决思路(优化前一万多导出失败,优化后支持百万级跨库表导出)

    原积木导出有两种导出,直接导出和大数据导出(大数据导出是做了优化去掉了一些样式之类的,性能更好) 实测中发现 原积木大数据导出性能:1万条数据导出耗时30秒,1.5万条耗时1.5分钟导出失败,数据超过一万条后经常导出失败,还会导致容器实例探活失败/内存撑爆重

    2024年04月11日
    浏览(42)
  • POI实现Excel多行复杂表头导出

    1. 定义表头标题 2. 编写导出/生成Excel工具类 3. 测试 测试结果

    2024年01月19日
    浏览(41)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包