国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

關(guān)于java操作excel導(dǎo)入導(dǎo)出三種方式

這篇具有很好參考價(jià)值的文章主要介紹了關(guān)于java操作excel導(dǎo)入導(dǎo)出三種方式。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

一、介紹

在平時(shí)的業(yè)務(wù)系統(tǒng)開(kāi)發(fā)中,少不了需要用到導(dǎo)出、導(dǎo)入excel功能,今天我們就一起來(lái)總結(jié)一下,如果你正為此需求感到困惑,那么閱讀完本文,你一定會(huì)有所收獲!

二、poi

大概在很久很久以前,微軟的電子表格軟件 Excel 以操作簡(jiǎn)單、存儲(chǔ)數(shù)據(jù)直觀方便,還支持打印報(bào)表,在誕生之初,可謂深得辦公室里的白領(lǐng)青睞,極大的提升了工作的效率,不久之后,便成了辦公室里的必備工具。

隨著更多的新語(yǔ)言的崛起,例如我們所熟悉的 java,后來(lái)便有一些團(tuán)隊(duì)開(kāi)始開(kāi)發(fā)一套能與 Excel 軟件無(wú)縫切換的操作工具!

這其中就有我們所熟悉的 apache 的 poi,其前身是 Jakarta 的 POI Project項(xiàng)目,之后將其開(kāi)源給 apache 基金會(huì)!

當(dāng)然,在java生態(tài)體系里面,能與Excel無(wú)縫銜接的第三方工具還有很多,因?yàn)?apache poi 在業(yè)界使用的最廣泛,因此其他的工具不做過(guò)多介紹!

話不多說(shuō),直接開(kāi)擼!

2.1、首先引入apache poi的依賴

<dependencies>
    <!--xls(03)-->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.1.2</version>
    </dependency>
    <!--xlsx(07)-->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>4.1.2</version>
    </dependency>
    <!--時(shí)間格式化工具-->
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.10.6</version>
    </dependency>
</dependencies>

2.2、導(dǎo)出excel

導(dǎo)出操作,即使用 Java 寫(xiě)出數(shù)據(jù)到 Excel 中,常見(jiàn)場(chǎng)景是將頁(yè)面上的數(shù)據(jù)導(dǎo)出,這些數(shù)據(jù)可能是財(cái)務(wù)數(shù)據(jù),也可能是商品數(shù)據(jù),生成 Excel 后返回給用戶下載文件。

在 poi 工具庫(kù)中,導(dǎo)出 api 可以分三種方式

  • HSSF方式:這種方式導(dǎo)出的文件格式為office 2003專用格式,即.xls,優(yōu)點(diǎn)是導(dǎo)出數(shù)據(jù)速度快,但是最多65536行數(shù)據(jù)
  • XSSF方式:這種方式導(dǎo)出的文件格式為office 2007專用格式,即.xlsx,優(yōu)點(diǎn)是導(dǎo)出的數(shù)據(jù)不受行數(shù)限制,缺點(diǎn)導(dǎo)出速度慢
  • SXSSF方式:SXSSF 是 XSSF API的兼容流式擴(kuò)展,主要解決當(dāng)使用 XSSF 方式導(dǎo)出大數(shù)據(jù)量時(shí),內(nèi)存溢出的問(wèn)題,支持導(dǎo)出大批量的excel數(shù)據(jù)

2.2.1、HSSF方式導(dǎo)出

HSSF方式,最多只支持65536條數(shù)據(jù)導(dǎo)出,超過(guò)這個(gè)條數(shù)會(huì)報(bào)錯(cuò)!

public class ExcelWrite2003Test {

    public static String PATH = "/Users/hello/Desktop/";

    public static void main(String[] args) throws Exception {
        //時(shí)間
        long begin = System.currentTimeMillis();

        //創(chuàng)建一個(gè)工作簿
        Workbook workbook = new HSSFWorkbook();
        //創(chuàng)建表
        Sheet sheet = workbook.createSheet();
        //寫(xiě)入數(shù)據(jù)
        for (int rowNumber = 0; rowNumber < 65536; rowNumber++) {
            //創(chuàng)建行
            Row row = sheet.createRow(rowNumber);
            for (int cellNumber = 0; cellNumber < 10; cellNumber++) {
                //創(chuàng)建列
                Cell cell = row.createCell(cellNumber);
                cell.setCellValue(cellNumber);
            }
        }
        System.out.println("over");

        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "用戶信息表2003BigData.xls");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        long end = System.currentTimeMillis();
        System.out.println((double) (end - begin) / 1000);//4.29s
    }
}

2.2.2、XSSF方式導(dǎo)出

XSSF方式支持大批量數(shù)據(jù)導(dǎo)出,所有的數(shù)據(jù)先寫(xiě)入內(nèi)存再導(dǎo)出,容易出現(xiàn)內(nèi)存溢出!

public class ExcelWrite2007Test {

    public static String PATH = "/Users/hello/Desktop/";

    public static void main(String[] args) throws Exception {
        //時(shí)間
        long begin = System.currentTimeMillis();

        //創(chuàng)建一個(gè)工作簿
        Workbook workbook = new XSSFWorkbook();
        //創(chuàng)建表
        Sheet sheet = workbook.createSheet();
        //寫(xiě)入數(shù)據(jù)
        for (int rowNumber = 0; rowNumber < 65537; rowNumber++) {
            Row row = sheet.createRow(rowNumber);
            for (int cellNumber = 0; cellNumber < 10; cellNumber++) {
                Cell cell = row.createCell(cellNumber);
                cell.setCellValue(cellNumber);
            }
        }
        System.out.println("over");

        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "用戶信息表2007BigData.xlsx");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        long end = System.currentTimeMillis();
        System.out.println((double) (end - begin) / 1000);//15.87s
    }
}

2.2.3、SXSSF方式導(dǎo)出

SXSSF方式是XSSF方式的一種延伸,主要特性是低內(nèi)存,導(dǎo)出的時(shí)候,先將數(shù)據(jù)寫(xiě)入磁盤(pán)再導(dǎo)出,避免報(bào)內(nèi)存不足,導(dǎo)致程序運(yùn)行異常,缺點(diǎn)是運(yùn)行很慢!

public class ExcelWriteSXSSFTest {

    public static String PATH = "/Users/hello/Desktop/";

    public static void main(String[] args) throws Exception {
        //時(shí)間
        long begin = System.currentTimeMillis();

        //創(chuàng)建一個(gè)工作簿
        Workbook workbook = new SXSSFWorkbook();

        //創(chuàng)建表
        Sheet sheet = workbook.createSheet();

        //寫(xiě)入數(shù)據(jù)
        for (int rowNumber = 0; rowNumber < 100000; rowNumber++) {
            Row row = sheet.createRow(rowNumber);
            for (int cellNumber = 0; cellNumber < 10; cellNumber++) {
                Cell cell = row.createCell(cellNumber);
                cell.setCellValue(cellNumber);
            }
        }
        System.out.println("over");

        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "用戶信息表2007BigDataS.xlsx");
        workbook.write(fileOutputStream);
        fileOutputStream.close();


        long end = System.currentTimeMillis();
        System.out.println((double) (end - begin) / 1000);//6.39s
    }
}

2.3、導(dǎo)入excel

導(dǎo)入操作,即將 excel 中的數(shù)據(jù)采用java工具庫(kù)將其解析出來(lái),進(jìn)而將 excel 數(shù)據(jù)寫(xiě)入數(shù)據(jù)庫(kù)!

同樣,在 poi 工具庫(kù)中,導(dǎo)入 api 也分三種方式,與上面的導(dǎo)出一一對(duì)應(yīng)!

2.3.1、HSSF方式導(dǎo)入

public class ExcelRead2003Test {

    public static String PATH = "/Users/hello/Desktop/";

    public static void main(String[] args) throws Exception {
        //獲取文件流
        FileInputStream inputStream = new FileInputStream(PATH + "用戶信息表BigData.xls");

        //1.創(chuàng)建工作簿,使用excel能操作的這邊都看看操作
        Workbook workbook = new HSSFWorkbook(inputStream);
        //2.得到表
        Sheet sheet = workbook.getSheetAt(0);
        //3.得到行
        Row row = sheet.getRow(0);
        //4.得到列
        Cell cell = row.getCell(0);
        getValue(cell);
        inputStream.close();
    }

    public static void getValue(Cell cell){
        //匹配類型數(shù)據(jù)
        if (cell != null) {
            CellType cellType = cell.getCellType();
            String cellValue = "";
            switch (cellType) {
                case STRING: //字符串
                    System.out.print("[String類型]");
                    cellValue = cell.getStringCellValue();
                    break;
                case BOOLEAN: //布爾類型
                    System.out.print("[boolean類型]");
                    cellValue = String.valueOf(cell.getBooleanCellValue());
                    break;
                case BLANK: //空
                    System.out.print("[BLANK類型]");
                    break;
                case NUMERIC: //數(shù)字(日期、普通數(shù)字)
                    System.out.print("[NUMERIC類型]");
                    if (HSSFDateUtil.isCellDateFormatted(cell)) { //日期
                        System.out.print("[日期]");
                        Date date = cell.getDateCellValue();
                        cellValue = new DateTime(date).toString("yyyy-MM-dd");
                    } else {
                        //不是日期格式,防止數(shù)字過(guò)長(zhǎng)
                        System.out.print("[轉(zhuǎn)換為字符串輸出]");
                        cell.setCellType(CellType.STRING);
                        cellValue = cell.toString();
                    }
                    break;
                case ERROR:
                    System.out.print("[數(shù)據(jù)類型錯(cuò)誤]");
                    break;
            }
            System.out.println(cellValue);
        }
    }
}

2.3.2、XSSF方式導(dǎo)入

public class ExcelRead2007Test {

    public static String PATH = "/Users/hello/Desktop/";

    public static void main(String[] args) throws Exception {
        //獲取文件流
        FileInputStream inputStream = new FileInputStream(PATH + "用戶信息表2007BigData.xlsx");

        //1.創(chuàng)建工作簿,使用excel能操作的這邊都看看操作
        Workbook workbook = new XSSFWorkbook(inputStream);
        //2.得到表
        Sheet sheet = workbook.getSheetAt(0);
        //3.得到行
        Row row = sheet.getRow(0);
        //4.得到列
        Cell cell = row.getCell(0);
        getValue(cell);
        inputStream.close();
    }


    public static void getValue(Cell cell){
        //匹配類型數(shù)據(jù)
        if (cell != null) {
            CellType cellType = cell.getCellType();
            String cellValue = "";
            switch (cellType) {
                case STRING: //字符串
                    System.out.print("[String類型]");
                    cellValue = cell.getStringCellValue();
                    break;
                case BOOLEAN: //布爾類型
                    System.out.print("[boolean類型]");
                    cellValue = String.valueOf(cell.getBooleanCellValue());
                    break;
                case BLANK: //空
                    System.out.print("[BLANK類型]");
                    break;
                case NUMERIC: //數(shù)字(日期、普通數(shù)字)
                    System.out.print("[NUMERIC類型]");
                    if (HSSFDateUtil.isCellDateFormatted(cell)) { //日期
                        System.out.print("[日期]");
                        Date date = cell.getDateCellValue();
                        cellValue = new DateTime(date).toString("yyyy-MM-dd");
                    } else {
                        //不是日期格式,防止數(shù)字過(guò)長(zhǎng)
                        System.out.print("[轉(zhuǎn)換為字符串輸出]");
                        cell.setCellType(CellType.STRING);
                        cellValue = cell.toString();
                    }
                    break;
                case ERROR:
                    System.out.print("[數(shù)據(jù)類型錯(cuò)誤]");
                    break;
            }
            System.out.println(cellValue);
        }
    }
}

2.3.3、SXSSF方式導(dǎo)入

public class ExcelReadSXSSFTest {

    public static String PATH = "/Users/hello/Desktop/";

    public static void main(String[] args) throws Exception {
        //獲取文件流

        //1.創(chuàng)建工作簿,使用excel能操作的這邊都看看操作
        OPCPackage opcPackage = OPCPackage.open(PATH + "用戶信息表2007BigData.xlsx");
        XSSFReader xssfReader = new XSSFReader(opcPackage);
        StylesTable stylesTable = xssfReader.getStylesTable();
        ReadOnlySharedStringsTable sharedStringsTable = new ReadOnlySharedStringsTable(opcPackage);
        // 創(chuàng)建XMLReader,設(shè)置ContentHandler
        XMLReader xmlReader = SAXHelper.newXMLReader();
        xmlReader.setContentHandler(new XSSFSheetXMLHandler(stylesTable, sharedStringsTable, new SimpleSheetContentsHandler(), false));
        // 解析每個(gè)Sheet數(shù)據(jù)
        Iterator<InputStream> sheetsData = xssfReader.getSheetsData();
        while (sheetsData.hasNext()) {
            try (InputStream inputStream = sheetsData.next();) {
                xmlReader.parse(new InputSource(inputStream));
            }
        }
    }

    /**
     * 內(nèi)容處理器
     */
    public static class SimpleSheetContentsHandler implements XSSFSheetXMLHandler.SheetContentsHandler {

        protected List<String> row;

        /**
         * A row with the (zero based) row number has started
         *
         * @param rowNum
         */
        @Override
        public void startRow(int rowNum) {
            row = new ArrayList<>();
        }

        /**
         * A row with the (zero based) row number has ended
         *
         * @param rowNum
         */
        @Override
        public void endRow(int rowNum) {
            if (row.isEmpty()) {
                return;
            }
            // 處理數(shù)據(jù)
            System.out.println(row.stream().collect(Collectors.joining("   ")));
        }

        /**
         * A cell, with the given formatted value (may be null),
         * and possibly a comment (may be null), was encountered
         *
         * @param cellReference
         * @param formattedValue
         * @param comment
         */
        @Override
        public void cell(String cellReference, String formattedValue, XSSFComment comment) {
            row.add(formattedValue);
        }

        /**
         * A header or footer has been encountered
         *
         * @param text
         * @param isHeader
         * @param tagName
         */
        @Override
        public void headerFooter(String text, boolean isHeader, String tagName) {
        }
    }

}

三、easypoi

easypoi 的底層也是基于 apache poi 進(jìn)行深度開(kāi)發(fā)的,它主要的特點(diǎn)就是將更多重復(fù)的工作,全部簡(jiǎn)單化,避免編寫(xiě)重復(fù)的代碼!

3.1、首先添加依賴包

<dependencies>
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-base</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-web</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-annotation</artifactId>
        <version>4.1.0</version>
    </dependency>
</dependencies>

3.2、采用注解導(dǎo)出導(dǎo)入

easypoi 最大的亮點(diǎn)就是基于注解實(shí)體類來(lái)導(dǎo)出、導(dǎo)入excel,使用起來(lái)非常簡(jiǎn)單!

首先,我們創(chuàng)建一個(gè)實(shí)體類UserEntity,其中@Excel注解表示導(dǎo)出文件的頭部信息。

public class UserEntity {

    @Excel(name = "姓名")
    private String name;

    @Excel(name = "年齡")
    private int age;

    @Excel(name = "操作時(shí)間",format="yyyy-MM-dd HH:mm:ss", width = 20.0)
    private Date time;
 
 //set、get省略
}

接著,我們來(lái)編寫(xiě)導(dǎo)出服務(wù)!

public static void main(String[] args) throws Exception {
    List<UserEntity> dataList = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        UserEntity userEntity = new UserEntity();
        userEntity.setName("張三" + i);
        userEntity.setAge(20 + i);
        userEntity.setTime(new Date(System.currentTimeMillis() + i));
        dataList.add(userEntity);
    }
    //生成excel文檔
    Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用戶","用戶信息"),
            UserEntity.class, dataList);
    FileOutputStream fos = new FileOutputStream("/Users/hello/Documents/easypoi-user1.xls");
    workbook.write(fos);
    fos.close();
}

導(dǎo)出的文件預(yù)覽如下:

關(guān)于java操作excel導(dǎo)入導(dǎo)出三種方式

?

對(duì)應(yīng)的導(dǎo)入操作,也很簡(jiǎn)單,源碼如下:

public static void main(String[] args) {
    ImportParams params = new ImportParams();
    params.setTitleRows(1);
    params.setHeadRows(1);
    long start = new Date().getTime();
    List<StudentEntity> list = ExcelImportUtil.importExcel(new File("/Users/hello/Documents/easypoi-user1.xls"),
            UserEntity.class, params);
    System.out.println(new Date().getTime() - start);
    System.out.println(JSONArray.toJSONString(list));
}

運(yùn)行程序,輸出結(jié)果如下:

[{"age":20,"name":"張三0","time":1616919493000},{"age":21,"name":"張三1","time":1616919493000},{"age":22,"name":"張三2","time":1616919493000},{"age":23,"name":"張三3","time":1616919493000},{"age":24,"name":"張三4","time":1616919493000},{"age":25,"name":"張三5","time":1616919493000},{"age":26,"name":"張三6","time":1616919493000},{"age":27,"name":"張三7","time":1616919493000},{"age":28,"name":"張三8","time":1616919493000},{"age":29,"name":"張三9","time":1616919493000}]

3.3、自定義數(shù)據(jù)結(jié)構(gòu)導(dǎo)出導(dǎo)入

easypoi 同樣也支持自定義數(shù)據(jù)結(jié)構(gòu)導(dǎo)出導(dǎo)入excel。

  • 自定義數(shù)據(jù)導(dǎo)出 excel
public static void main(String[] args) throws Exception {
    //封裝表頭
    List<ExcelExportEntity> entityList = new ArrayList<ExcelExportEntity>();
    entityList.add(new ExcelExportEntity("姓名", "name"));
    entityList.add(new ExcelExportEntity("年齡", "age"));
    ExcelExportEntity entityTime = new ExcelExportEntity("操作時(shí)間", "time");
    entityTime.setFormat("yyyy-MM-dd HH:mm:ss");
    entityTime.setWidth(20.0);
    entityList.add(entityTime);
    //封裝數(shù)據(jù)體
    List<Map<String, Object>> dataList = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        Map<String, Object> userEntityMap = new HashMap<>();
        userEntityMap.put("name", "張三" + i);
        userEntityMap.put("age", 20 + i);
        userEntityMap.put("time", new Date(System.currentTimeMillis() + i));
        dataList.add(userEntityMap);
    }
    //生成excel文檔
    Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("學(xué)生","用戶信息"), entityList, dataList);
    FileOutputStream fos = new FileOutputStream("/Users/panzhi/Documents/easypoi-user2.xls");
    workbook.write(fos);
    fos.close();
}
  • 導(dǎo)入 excel
public static void main(String[] args) {
    ImportParams params = new ImportParams();
    params.setTitleRows(1);
    params.setHeadRows(1);
    long start = new Date().getTime();
    List<Map<String, Object>> list = ExcelImportUtil.importExcel(new File("/Users/panzhi/Documents/easypoi-user2.xls"),
            Map.class, params);
    System.out.println(new Date().getTime() - start);
    System.out.println(JSONArray.toJSONString(list));
}

更多的 api 操作可以訪問(wèn) Easypoi - 接口文檔

四、easyexcel

easyexcel 是阿里巴巴開(kāi)源的一款 excel 解析工具,底層邏輯也是基于 apache poi 進(jìn)行二次開(kāi)發(fā)的。不同的是,再讀寫(xiě)數(shù)據(jù)的時(shí)候,采用 sax 模式一行一行解析,在并發(fā)量很大的情況下,依然能穩(wěn)定運(yùn)行!

下面,我們就一起來(lái)了解一下這款新起之秀!

4.1、首先添加依賴包

<dependencies>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>easyexcel</artifactId>
        <version>2.2.6</version>
    </dependency>
 <!--常用工具庫(kù)-->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>29.0-jre</version>
    </dependency>
</dependencies>

4.2、采用注解導(dǎo)出導(dǎo)入

easyexcel 同樣也支持采用注解方式進(jìn)行導(dǎo)出、導(dǎo)入!

首先,我們創(chuàng)建一個(gè)實(shí)體類UserEntity,其中@ExcelProperty注解表示導(dǎo)出文件的頭部信息。

public class UserEntity {

    @ExcelProperty(value = "姓名")
    private String name;

    @ExcelProperty(value = "年齡")
    private int age;

    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    @ExcelProperty(value = "操作時(shí)間")
    private Date time;
    
    //set、get省略
}

接著,我們來(lái)編寫(xiě)導(dǎo)出服務(wù)!

public static void main(String[] args) {
    List<UserEntity> dataList = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        UserEntity userEntity = new UserEntity();
        userEntity.setName("張三" + i);
        userEntity.setAge(20 + i);
        userEntity.setTime(new Date(System.currentTimeMillis() + i));
        dataList.add(userEntity);
    }
    EasyExcel.write("/Users/hello/Documents/easyexcel-user1.xls", UserEntity.class).sheet("用戶信息").doWrite(dataList);
}

導(dǎo)出的文件預(yù)覽如下:

關(guān)于java操作excel導(dǎo)入導(dǎo)出三種方式

?

對(duì)應(yīng)的導(dǎo)入操作,也很簡(jiǎn)單,源碼如下:

public static void main(String[] args) {
    String filePath = "/Users/hello/Documents/easyexcel-user1.xls";
    List<DemoData> list = EasyExcel.read(filePath).head(UserEntity.class).sheet().doReadSync();
    System.out.println(JSONArray.toJSONString(list));
}

運(yùn)行程序,輸出結(jié)果如下:

[{"age":20,"name":"張三0","time":1616920360000},{"age":21,"name":"張三1","time":1616920360000},{"age":22,"name":"張三2","time":1616920360000},{"age":23,"name":"張三3","time":1616920360000},{"age":24,"name":"張三4","time":1616920360000},{"age":25,"name":"張三5","time":1616920360000},{"age":26,"name":"張三6","time":1616920360000},{"age":27,"name":"張三7","time":1616920360000},{"age":28,"name":"張三8","time":1616920360000},{"age":29,"name":"張三9","time":1616920360000}]

4.3、自定義數(shù)據(jù)結(jié)構(gòu)導(dǎo)出導(dǎo)入

easyexcel 同樣也支持自定義數(shù)據(jù)結(jié)構(gòu)導(dǎo)出導(dǎo)入excel。

  • 自定義數(shù)據(jù)導(dǎo)出 excel
public static void main(String[] args) {
    //表頭
    List<List<String>> headList = new ArrayList<>();
    headList.add(Lists.newArrayList("姓名"));
    headList.add(Lists.newArrayList("年齡"));
    headList.add(Lists.newArrayList("操作時(shí)間"));

    //數(shù)據(jù)體
    List<List<Object>> dataList = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        List<Object> data = new ArrayList<>();
        data.add("張三" + i);
        data.add(20 + i);
        data.add(new Date(System.currentTimeMillis() + i));
        dataList.add(data);
    }
    EasyExcel.write("/Users/hello/Documents/easyexcel-user2.xls").head(headList).sheet("用戶信息").doWrite(dataList);
}
  • 導(dǎo)入 excel
public static void main(String[] args) {
    String filePath = "/Users/panzhi/Documents/easyexcel-user2.xls";
    UserDataListener userDataListener = new UserDataListener();
    EasyExcel.read(filePath, userDataListener).sheet().doRead();
    System.out.println("表頭:" + JSONArray.toJSONString(userDataListener.getHeadList()));
    System.out.println("數(shù)據(jù)體:" + JSONArray.toJSONString(userDataListener.getDataList()));
}

運(yùn)行程序,輸出結(jié)果如下:

表頭:[{0:"姓名",1:"年齡",2:"操作時(shí)間"}]
數(shù)據(jù)體:[{0:"張三0",1:"20",2:"2021-03-28 16:31:39"},{0:"張三1",1:"21",2:"2021-03-28 16:31:39"},{0:"張三2",1:"22",2:"2021-03-28 16:31:39"},{0:"張三3",1:"23",2:"2021-03-28 16:31:39"},{0:"張三4",1:"24",2:"2021-03-28 16:31:39"},{0:"張三5",1:"25",2:"2021-03-28 16:31:39"},{0:"張三6",1:"26",2:"2021-03-28 16:31:39"},{0:"張三7",1:"27",2:"2021-03-28 16:31:39"},{0:"張三8",1:"28",2:"2021-03-28 16:31:39"},{0:"張三9",1:"29",2:"2021-03-28 16:31:39"}]

更多的 api 操作可以訪問(wèn) easyexcel - 接口文檔

五、小結(jié)

總體來(lái)說(shuō),easypoi和easyexcel都是基于apache poi進(jìn)行二次開(kāi)發(fā)的。

不同點(diǎn)在于:

1、easypoi 在讀寫(xiě)數(shù)據(jù)的時(shí)候,優(yōu)先是先將數(shù)據(jù)寫(xiě)入內(nèi)存,優(yōu)點(diǎn)是讀寫(xiě)性能非常高,但是當(dāng)數(shù)據(jù)量很大的時(shí)候,會(huì)出現(xiàn)oom,當(dāng)然它也提供了 sax 模式的讀寫(xiě)方式,需要調(diào)用特定的方法實(shí)現(xiàn)。

2、easyexcel 基于sax模式進(jìn)行讀寫(xiě)數(shù)據(jù),不會(huì)出現(xiàn)oom情況,程序有過(guò)高并發(fā)場(chǎng)景的驗(yàn)證,因此程序運(yùn)行比較穩(wěn)定,相對(duì)于 easypoi 來(lái)說(shuō),讀寫(xiě)性能稍慢!

easypoi 與 easyexcel 還有一點(diǎn)區(qū)別在于,easypoi 對(duì)定制化的導(dǎo)出支持非常的豐富,如果當(dāng)前的項(xiàng)目需求,并發(fā)量不大、數(shù)據(jù)量也不大,但是需要導(dǎo)出 excel 的文件樣式千差萬(wàn)別,那么我推薦你用 easypoi;反之,使用 easyexcel !文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-450233.html

到了這里,關(guān)于關(guān)于java操作excel導(dǎo)入導(dǎo)出三種方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Java導(dǎo)出Excel模板,導(dǎo)出數(shù)據(jù)到指定模板,通過(guò)模板導(dǎo)入數(shù)據(jù)(一)

    Java導(dǎo)出Excel模板,導(dǎo)出數(shù)據(jù)到指定模板,通過(guò)模板導(dǎo)入數(shù)據(jù)(一)

    本文章主要是介紹阿里巴巴的easyexcel的使用 1. 首先需要我們導(dǎo)入easyexcel的依賴包 2. 前期工作準(zhǔn)備 編寫(xiě)相關(guān)導(dǎo)出模板和導(dǎo)入模板。在項(xiàng)目的resources下創(chuàng)建文件夾,命名為excel 導(dǎo)出模板(此處僅做示例,字段根據(jù)自己項(xiàng)目來(lái)): ?導(dǎo)入模板(導(dǎo)入時(shí)需要哪些字段根據(jù)自己項(xiàng)目業(yè)

    2024年02月03日
    瀏覽(30)
  • C#操作Excel文件三種方式

    C#操作Excel文件三種方式 彭世瑜2021-07-12 16:14:28 文章標(biāo)簽C/C++文章分類C/C++后端開(kāi)發(fā)閱讀數(shù)5317 .Net平臺(tái)上對(duì)Excel進(jìn)行操作主要有兩種方式。第一種,把Excel文件看成一個(gè)數(shù)據(jù)庫(kù),通過(guò)OleDb的方式進(jìn)行讀取與操作;第二種,調(diào)用Excel的COM組件。兩種方式各有特點(diǎn)。 注意一些簡(jiǎn)單的問(wèn)

    2024年02月15日
    瀏覽(20)
  • Easys Excel的表格導(dǎo)入(讀)導(dǎo)出(寫(xiě))-----java

    Easys Excel的表格導(dǎo)入(讀)導(dǎo)出(寫(xiě))-----java

    可以學(xué)習(xí)一些新知識(shí): EasyExcel官方文檔 - 基于Java的Excel處理工具 | Easy Excel excel的一些優(yōu)點(diǎn)和缺點(diǎn) java解析excel的框架有很多 : poi jxl,存在問(wèn)題:非常的消耗內(nèi)存, easyexcel 我們遇到再大的excel都不會(huì)出現(xiàn)內(nèi)存溢出的問(wèn)題 能夠?qū)⒁粋€(gè)原本3M excel文件,poi來(lái)操作將會(huì)占用內(nèi)存 100MB,

    2024年02月13日
    瀏覽(24)
  • C#操作Excel文件三種方式詳解

    1.OleDb方式: 使用.NET Framework內(nèi)置的System.Data.OleDb命名空間中的類,可以將Excel文件當(dāng)作數(shù)據(jù)庫(kù)來(lái)讀取數(shù)據(jù)。這種方式適用于較舊版本的Excel文件(.xls格式,即Excel 2003及更早版本)。 2.COM組件方式: 利用Office Interop庫(kù)(如Microsoft.Office.Interop.Excel),可以直接調(diào)用Excel應(yīng)用程序的

    2024年03月18日
    瀏覽(19)
  • Java原生POI實(shí)現(xiàn)的Excel導(dǎo)入導(dǎo)出(簡(jiǎn)單易懂)

    Java原生POI實(shí)現(xiàn)的Excel導(dǎo)入導(dǎo)出(簡(jiǎn)單易懂)

    首先是Controller入口方法 這個(gè)接口在postman上傳參是下面這樣的: 注意里面的參數(shù)名稱要和接口上的一致,不然會(huì)拿不到值 還有file那里key的類型要選file類型的,這樣就可以在后面value里面選擇文件 然后是Service方法 首先是Controller入口 strJson是用來(lái)接受其它參數(shù)的,一般導(dǎo)出的

    2024年02月11日
    瀏覽(27)
  • Java 使用hutool工具進(jìn)行導(dǎo)出導(dǎo)入excel表格(代碼很簡(jiǎn)單)

    Java 使用hutool工具進(jìn)行導(dǎo)出導(dǎo)入excel表格(代碼很簡(jiǎn)單)

    創(chuàng)建一個(gè)Controller進(jìn)行測(cè)試?

    2024年02月07日
    瀏覽(24)
  • 使用Java導(dǎo)入、導(dǎo)出excel詳解(附有封裝好的工具類)

    使用Java導(dǎo)入、導(dǎo)出excel詳解(附有封裝好的工具類)

    ?? 作 ? ??????? 者 :是江迪呀 ?? 本文 : Java 、 Excel 、 導(dǎo)出 、 工具類 、 后端 ?? 每日?? 一言 :有些事情不是對(duì)的才去堅(jiān)持,而是堅(jiān)持了它才是對(duì)的! 我們?cè)谌粘i_(kāi)發(fā)中,一定遇到過(guò)要將數(shù)據(jù)導(dǎo)出為 Excel 的需求,那么怎么做呢?在做之前,我們需要思考

    2024年02月06日
    瀏覽(24)
  • java實(shí)現(xiàn)excel的導(dǎo)入導(dǎo)出(帶參數(shù)校驗(yàn):非空校驗(yàn)、數(shù)據(jù)格式校驗(yàn))

    java實(shí)現(xiàn)excel的導(dǎo)入導(dǎo)出(帶參數(shù)校驗(yàn):非空校驗(yàn)、數(shù)據(jù)格式校驗(yàn))

    本次封裝引入阿里開(kāi)源框架EasyExcel,EasyExcel是一個(gè)基于Java的簡(jiǎn)單、省內(nèi)存的讀寫(xiě)Excel的開(kāi)源項(xiàng)目。在盡可能節(jié)約內(nèi)存的情況下支持讀寫(xiě)百M(fèi)的Excel。 github地址:GitHub - alibaba/easyexcel: 快速、簡(jiǎn)潔、解決大文件內(nèi)存溢出的java處理Excel工具 。 64M內(nèi)存20秒讀取75M(46W行25列)的Excel(3.0.2

    2024年02月01日
    瀏覽(42)
  • JAVA:使用POI SXSSFWorkbook方式導(dǎo)出Excel大數(shù)據(jù)文件

    Apache POI 是用Java編寫(xiě)的免費(fèi)開(kāi)源的跨平臺(tái)的 Java API,Apache POI提供API給Java對(duì)Microsoft Office格式檔案讀和寫(xiě)的功能。POI組件可以提供Java操作Microsoft Office的API,導(dǎo)出格式為Office 2003時(shí)POI調(diào)用的HSSF包,導(dǎo)出格式為Office 2007時(shí),調(diào)用XSSF包,而SXSSF包是POI3.8版本之上對(duì)XSSF的一個(gè)擴(kuò)展,用

    2024年02月11日
    瀏覽(21)
  • Java多線程 - 創(chuàng)建的三種方式介紹

    什么是線程 ? 線程(thread)是一個(gè)程序內(nèi)部的一條執(zhí)行路徑。 我們之前啟動(dòng)程序執(zhí)行后,main方法的執(zhí)行其實(shí)就是一條單獨(dú)的執(zhí)行路徑。 程序中如果只有一條執(zhí)行路徑,那么這個(gè)程序就是單線程的程序。 什么是多線程 ? 多線程是指從軟硬件上實(shí)現(xiàn)多條執(zhí)行流程的技術(shù)。 方式一

    2024年02月20日
    瀏覽(29)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包