• Apache POI & EasyExcel


    Apache POI => EXCEL/WORD/PPT

    引入Maven:

    <!--03版Excel .xls-->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>4.1.2</version>
            </dependency>
    <!--07版Excel .xlsx-->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>4.1.2</version>
            </dependency>
    <!--日期格式化工具>
            <dependency>
                <groupId>joda-time</groupId>
                <artifactId>joda-time</artifactId>
                <version>2.10.1</version>
            </dependency>

    首先了解Excel:

    Workbook==>工作簿;
    Sheet==>工作表;


     POI代码操作Excel:

    public class ExcelWrite {
        @Test //测试方法
        public void write() throws IOException {
            //1.创建一个工作簿
            Workbook workbook = new XSSFWorkbook(); //如果是03excel就替换为HSSFWorkbook
            //2.创建一个工作表
            Sheet sheet = workbook.createSheet();
            //3.创建一个行
            Row row1 = sheet.createRow(0);//0代表第一行
            Row row2 = sheet.createRow(1);//0代表第一行
            //4.创建一个单元格
            Cell cell_11 =  row1.createCell(0);
            cell_11.setCellValue("姓名");
            Cell cell_12 =  row1.createCell(1);
            cell_12.setCellValue("时间");
    
            Cell cell_21 = row2.createCell(0);
            cell_21.setCellValue("jqy");
            Cell cell_22 = row2.createCell(1);
            String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");//获取时间
            cell_22.setCellValue(time);
    
            //生成一张表(IO操作)
            String path = "C:\Users\j50016344\Desktop\easyexcel";//本项目路径
    
            FileOutputStream fileOutputStream = new FileOutputStream(path + "myExcelName07.xlsx");//如果是03Excel就替换为.xls
            workbook.write(fileOutputStream);
    
            //关闭流
            fileOutputStream.close();
            System.out.println("生成完毕!!");
        }
    }

    生成:

     


    大数据量导入

    03版HSSF

      缺点:最多65536行

      优点:过程中写入缓存,速度快;无磁盘操作

    07版XSSF 

      优点:可以写较大的数据量,如20万条

      缺点:写数据时速度非常慢,且消耗内存 容易发生OOM内存溢出,如100万条

    SXSSF

    优点:100万+条,且速度快


    POI 读Excel

        @Test
        public void Read() throws IOException {
            String path = "C:\Users\j50016344\Desktop\easyexcel";//本项目路径
    
            //读取表
            FileInputStream fileInputStream = new FileInputStream(path + "\myExcelName07.xlsx");
            XSSFWorkbook readExcel = new XSSFWorkbook(fileInputStream);
            Sheet sheet1 = readExcel.getSheetAt(0);
    
    
            Row row1 = sheet1.getRow(0);
            Cell cell11 = row1.getCell(1);
            System.out.println("one cell's value:" + cell11);
            System.out.println("===========================");
    
            double start = System.currentTimeMillis();//获取读取时间
            int rowSize = sheet1.getPhysicalNumberOfRows();//行数rowSize
            for(int rowNum=0;rowNum<rowSize;rowNum++){
                Row row = sheet1.getRow(rowNum);
                int columnSize = row.getPhysicalNumberOfCells();//列数columnSize
                for(int column =0;column < columnSize;column++){
                    Cell cell = row.getCell(column);
                    System.out.println(cell);
                }
            }
            double end = System.currentTimeMillis();//结束
            double timeUse = (end-start)/1000;//毫秒÷1000=>秒
            System.out.println("Use: "+ timeUse + " Second");
    
            fileInputStream.close();
        }


    获取 计算公式(了解即可)

    从Excel中获取出公式的表达式


     EasyExcel操作

    强推语雀文档:https://www.yuque.com/easyexcel/doc


    导入依赖:

    <!--直接用alibaba的easyexcel套装就好,不用一个一个导入了-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>easyexcel</artifactId>
        <version>2.2.7</version>
    </dependency>
    <!--joda-time有没有都行,这个不重要--> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.10.1</version> </dependency>

    写Excel:

    其实和POI区别不大,差别在于先将数据放入对象,然后把对象搞到Excel里面。

    非常多的功能,可以花式修改、包括样式

    public class WriteExcel {
    
        private List<Student> data() {
            List<Student> list = new ArrayList<Student>();
            for (int i = 0; i < 10; i++) {
                Student data = new Student();
                data.setString("字符串" + i);
                data.setDate(new Date());
                data.setDoubleData(0.56);
                list.add(data);
            }
            return list;
        }
    
    
        @Test
        public void simpleWrite() {
            String fileName = "C:\Users\j50016344\Desktop\temp (2)\easy-excel" + "\JQY's_Simple_Write" + System.currentTimeMillis() + ".xlsx";
            // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
            // 如果这里想使用03 则 传入excelType参数即可
            EasyExcel.write(fileName, Student.class).sheet("模板").doWrite(data());
        }

    详见:https://www.yuque.com/easyexcel/doc/write

    读Excel:

    详见:https://www.yuque.com/easyexcel/doc/read  (写得非常详细了)

        public void simpleRead() {
            String fileName = "C:\Users\j50016344\Desktop\temp (2)\easy-excel\JQY's_Simple_Write1617023855038.xlsx";
            // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
            EasyExcel.read(fileName, Student.class, (ReadListener) new StudentListener()).sheet().doRead(); //记住套路
        }

    将excel的行,转换为对象:

    dao的操作可进一步将对象存储到DB中

    Excel==》对象==》DB

    Web上传下载

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

    (此处测试不顺利,也可以将文件上传下载和读写分开)

  • 相关阅读:
    js实现点击上下按钮,图片向上向下循环滚动切换
    jquery实现点击进入新的页面。(jquery实现超链接)
    jquery实现鼠标移入移除背景图片切换
    C:WindowsSystem32driversetchosts文件显示
    网页添加qq咨询
    本地虚拟站点创建
    ftp获取mysql数据库方法
    数论基础
    最小费用最大流
    AC自动机 hdu2222
  • 原文地址:https://www.cnblogs.com/qyf2199/p/14584206.html
Copyright © 2020-2023  润新知