1. Java读写excel的两种方式

  (1)apache poi

  (2)alibaba easyexcel

  两种方式写入excel的区别;

  poi会先把所有的数据读入到内存中,然后写入到excel中;easyexcel则是把数据一条一条写入excel中。所以如果有大量数据时,比如有100w条数据写入excel中,使用poi会先把数据读入内存中,可能会造成内存溢出;而easyexcel则把数据一条一条写入excel中。

 

2. 依赖引入

    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>4.1.2</version>
    </dependency>

    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>4.1.2</version>
    </dependency>

 

3. 使用poi读写excel

  excel有03和07两个版本,所以使用poi读写excel也略有区别。

  (1)03版本excel文件后缀名.xls,最多能写入65535条数据;07版本excel文件后缀名.xlsx,理论上能写更多条数据,但是使用普通对象写入excel消耗时间更长,大量写入可以使用SXSSFWorkbook;

  (2)03和07版本excel创建对象不同,03版本创建HSSFWorkbook对象,07版本创建XSSFWorkbook对象,大量数据读写使用SXSSFWorkbook;

  (3)部分方法不用;

  写入excel的几个步骤:

    a. 创建空的表格(工作簿);

    b. 创建工作区;

    c. 创建行;

    d. 创建单元格;

    e. 给单元格设置内容;(对单元格内容格式设置)

    f. 把内存中的单元格生成到本地的磁盘上;

 

  写入03版本exel

public class ExcelWrite03 {
    public static void main( String[] args ) {
        //创建空的excel表格,HSSF普通excel
        Workbook wb = new HSSFWorkbook();
        //创建工作区
        Sheet sheet = wb.createSheet("员工数据");
        //设置列宽(第一个参数表示:第二列;第二个参数表示:列宽为50,此处注意设置列宽要*256)
        sheet.setColumnWidth(2,50*256);
        //创建行 方法参数是行号,从0开始
        Row row = sheet.createRow(1);
        //设置行高
        row.setHeightInPoints(40f);
        //创建 2C 单元格,参数:列号,从0开始
        Cell cell = row.createCell(2);

        Row row1 = sheet.createRow(0);
        Cell cell1 = row1.createCell(0);
        cell1.setCellValue("hello");

        /**
         * 设置单元格样式
         */
        //创建单元格样式
        CellStyle style = wb.createCellStyle();
        //设置字体样式,要先创建字体对象
        Font font = wb.createFont();
        //字体加粗
        font.setBold(true);
        //设置字体大小
        font.setFontHeightInPoints((short) 20);
        //设置字体类型
        font.setFontName("楷体");
        //给style添加字体样式
        style.setFont(font);
        //给单元格设置样式
        cell.setCellStyle(style);

        //给单元格设置内容
        cell.setCellValue("hello world");
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File("D:\\JavaProject\\hello.xls"));
            wb.write(fos);
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (wb != null){
                try {
                    wb.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

  写入07版本excel,与03版本创建对象不用,其他相同

public class ExcelWrite07 {
    public static void main(String[] args) {
        Workbook wb = new XSSFWorkbook();    //03和07创建对象不同
        Sheet sheet = wb.createSheet("员工数据");
        //设置列宽(第一个参数表示:第二列;第二个参数表示:列宽为50,此处注意设置列宽要*256)
        sheet.setColumnWidth(2,50*256);
        //创建行 方法参数是行号,从0开始
        Row row = sheet.createRow(1);
        //设置行高
        row.setHeightInPoints(40f);
        //创建 2C 单元格,参数:列号,从0开始
        Cell cell = row.createCell(2);
        /**
         * 设置单元格样式
         */
        //创建单元格样式
        CellStyle style = wb.createCellStyle();
        //设置字体样式,要先创建字体对象
        Font font = wb.createFont();
        //字体加粗
        font.setBold(true);
        //设置字体大小
        font.setFontHeightInPoints((short) 20);
        //设置字体类型
        font.setFontName("楷体");
        //给style添加字体样式
        style.setFont(font);
        //给单元格设置样式
        cell.setCellStyle(style);

        //给单元格设置内容
        cell.setCellValue("hello world");
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File("D:\\JavaProject\\hello07.xlsx"));  //后缀不同
            wb.write(fos);
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (wb != null){
                try {
                    wb.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
View Code

 

  读excel的基本步骤:

    a. 创建工作簿;

    b. 获取sheet;

    c. 获取行;

    d. 获取单元格;

    e. 获取单元格中的数据

 

  读03版本excel

public class ExcelRead03 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream(
                new File("D:\\JavaProject\\read03.xls"));
        //1. 创建工作簿
        Workbook wb = new HSSFWorkbook(fis);
        //2. 获取sheet
        Sheet sheet = wb.getSheetAt(0);
        System.out.println(sheet);
        //3. 获取行
        Row row = sheet.getRow(0);
        //4. 获取单元格
        Cell cell = row.getCell(0);

        //5. 获取单元格中的字符串内容
        System.out.println(cell.getStringCellValue());
        //获取单元格中数字内容
        System.out.println(sheet.getRow(1).getCell(1).getNumericCellValue());
        fis.close();
    }
}

 

  读07版本excel,与03版本创建对象不同,其他相同

public class ExcelRead07 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream(
                new File("D:\\JavaProject\\read07.xlsx"));
        //1. 创建工作簿
        Workbook wb = new XSSFWorkbook(fis);
        //2. 获取sheet
        Sheet sheet = wb.getSheetAt(0);
        System.out.println(sheet);
        //3. 获取行
        Row row = sheet.getRow(0);
        //4. 获取单元格
        Cell cell = row.getCell(0);

        //5. 获取单元格中的字符串内容
        System.out.println(cell.getStringCellValue());
        //获取单元格中数字内容
        System.out.println(sheet.getRow(1).getCell(1).getNumericCellValue());
        fis.close();
    }
}
View Code

 

  单元格不同类型数据读取,在读excel时,单元格中会存在不用的数据类型,日期型,字符串型,数字型等,需要做不同类型的获取

public class ReadCellType07 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream(
                new File("D:\\JavaProject\\test.xlsx"));
        Workbook wb = new XSSFWorkbook(fis);
        Sheet sheet = wb.getSheetAt(0);
        //获取标题行内容
        Row rowTitle = sheet.getRow(0);
        if(rowTitle != null){
            //获取单元格的个数
            int cellCount = rowTitle.getPhysicalNumberOfCells();
            for (int cellNum = 0; cellNum < cellCount; cellNum ++){
                Cell cell = rowTitle.getCell(cellNum);
                if (cell != null){
                    String cellValue = cell.getStringCellValue();
                    System.out.print(cellValue + " | ");
                }
            }
            System.out.println();
        }
        //获取表中的内容
        int rowCount = sheet.getPhysicalNumberOfRows();
        for(int rowNum = 1; rowNum < rowCount; rowNum++){
            Row rowData = sheet.getRow(rowNum);
            if(rowData != null){
                int cellCount = rowTitle.getPhysicalNumberOfCells();
                for(int cellNum = 0; cellNum < cellCount; cellNum++){
                    System.out.print("[" + (rowNum + 1) + "-" + (cellNum + 1) + "]");
                    Cell cell = rowData.getCell(cellNum);
                    //匹配单元格中数据的类型
                    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:
                                System.out.print("【NUMERIC】");
                                if(DateUtil.isCellDateFormatted(cell)){  //日期
                                    System.out.print("日期");
                                    Date dateCellValue = cell.getDateCellValue();
                                    cellValue = dateCellValue.toString();
                                }else {
                                    //不是日期,防止数字过长
                                    System.out.print("【转换为字符串输出】");
                                    cell.setCellType(CellType.STRING);
                                    cellValue = cell.toString();
                                }
                                break;
                            case ERROR:
                                System.out.print("数据类型错误");
                                break;
                        }
                        System.out.println(cellValue);
                    }
                }
            }
        }
        fis.close();
    }
}

 

  读取单元格中的公式,表格中A5是一个计算和的公式

public class CalcCellFormula03 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream(
                new File("D:\\JavaProject\\公式.xls"));
        Workbook wb = new HSSFWorkbook(fis);
        Sheet sheet = wb.getSheetAt(0);
        Row row = sheet.getRow(4);
        Cell cell = row.getCell(0);
        //获取计算公式
        FormulaEvaluator formulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) wb);
        //输出单元格内容
        CellType cellType = cell.getCellType();
        switch (cellType){
            case FORMULA:    //公式
                String cellFormula = cell.getCellFormula();
                System.out.println(cellFormula);

                //计算
                CellValue evaluate = formulaEvaluator.evaluate(cell);
                String s = evaluate.formatAsString();
                System.out.println(s);
        }

    }
}

 

4. 使用easyexcel读写excel

  https://www.yuque.com/easyexcel/doc/easyexcel

posted on 2021-12-25 18:20  homle  阅读(7065)  评论(0编辑  收藏  举报