EasyPOI学习笔记(一)

官网地址

码云地址

前言

使用

  1. easypoi父包

  2. easypoi-annotation基础注解包,作用与实体对象上,拆分后方便maven多工程的依赖管理;

  3. easypoi-base导入导出工具包,可以完成excel导出、导入,word导出导入;

  4. easypoi-web耦合了spring-mvc基于abstractview,极大简化了spring-mvc下的导出功能;

  5. sax导入使用xercesImpl这个包(这个包可能有问题),word导出使用poi-scratchpad,都已作为可选包

maven坐标如下

   <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>4.2.0</version>
        </dependency>

简单使用

  1. 搭建项目maven

image-20210106100733268

Easypoi注解

Excel导入导出

Excel的导入导出是Easypoi的核心功能,前期基本也是围绕这个打造的,主要分为三种方式的处理,其中模板和Html目前只支持导出,因为支持Map.class其实导入应该是怎样都支持的

  • 注解方式,注解变种方式
  • 模板方式
  • Html方式

下面分别就这三种方式进行讲解

注解

注解介绍

easypoi起因就是Excel的导入导出,最初的模板是实体和Excel的对应,model–row,filed–col 这样利用注解我们可以和容易做到excel到导入导出

image-20210106104736345

目前注解有五类分别为:

  • @Excel 作用到filed上面,是对Excel一列的一个描述
  • @ExcelCollection 表示一个集合,主要针对一对多的导出,eg一个教师对应多个科目,科目就可以用集合表示
  • @ExcelEntity 表示一个继续深入导出的实体,但无过多实际意义,只是告诉系统这个对象里同样有导出的字段
  • @Excelgnore 和名字一样表示这个字段被忽略跳过这个导出
  • @ExcelTarget 这个是作用于最外层的对象,描述这个对象的id,以便支持一个对象可以针对不同导出做出不同处理

@Excel参数

这个是必须使用的注解,如果需求简单只使用这一个注解也是可以的,涵盖了常用的Excel需求,需要大家熟悉这个功能,主要分为基础,图片处理,时间处理,合并处理几块,name_id是上面讲的id用法,这里就不累言了

属性类型默认值功能
nameStringnull列名,支持name_id
needMergebooleanfasle是否需要纵向合并单元格(用于含有list中,单个的单元格,合并list创建的多个row)
orderNumString“0”列的排序,支持name_id
replaceString[]{}值得替换 导出是{a_id,b_id} 导入反过来
savePathString“upload”导入文件保存路径,如果是图片可以填写,默认是upload/className/ IconEntity这个类对应的就是upload/Icon/
typeint1导出类型 1 是文本 2 是图片,3 是函数,10 是数字 默认是文本
widthdouble10列宽
heightdouble10列高,后期打算统一使用@ExcelTarget的height,这个会被废弃,注意
isStatisticsbooleanfasle自动统计数据,在追加一行统计,把所有数据都和输出 这个处理会吞没异常,请注意这一点
isHyperlinkbooleanfalse超链接,如果是需要实现接口返回对象
isImportFieldbooleantrue校验字段,看看这个字段是不是导入的Excel中有,如果没有说明是错误的Excel,读取失败,支持name_id
exportFormatString“”导出的时间格式,以这个是否为空来判断是否需要格式化日期
importFormatString“”导入的时间格式,以这个是否为空来判断是否需要格式化日期
formatString“”时间格式,相当于同时设置了exportFormat 和 importFormat
databaseFormatString“yyyyMMddHHmmss”导出时间设置,如果字段是Date类型则不需要设置 数据库如果是string 类型,这个需要设置这个数据库格式,用以转换时间格式输出
numFormatString“”数字格式化,参数是Pattern,使用的对象是DecimalFormat
imageTypeint1导出类型 1 从file读取 2 是从数据库中读取 默认是文件 同样导入也是一样的
suffixString“”文字后缀,如% 90 变成90%
isWrapbooleantrue是否换行 即支持\n
mergeRelyint[]{}合并单元格依赖关系,比如第二列合并是基于第一列 则{0}就可以了
mergeVerticalbooleanfasle纵向合并内容相同的单元格
fixedIndexint-1对应excel的列,忽略名字
isColumnHiddenbooleanfalse导出隐藏列

@ExcelTarget参数

限定一个到处实体的注解,以及一些通用设置,作用于最外面的实体

属性类型默认值功能
valueStringnull定义ID
heightdouble10设置行高
fontSizeshort11设置文字大小

@ExcelEntity

标记是不是导出excel 标记为实体类,一遍是一个内部属性类,标记是否继续穿透,可以自定义内部id

属性类型默认值功能
idStringnull定义ID

@ExcelCollection

一对多的集合注解,用以标记集合是否被数据以及集合的整体排序

属性类型默认值功能
idStringnull定义ID
nameStringnull定义集合列名,支持nanm_id
orderNumint0排序,支持name_id
typeClass<?>ArrayList.class导入时创建对象使用

@ExcelIgnore

忽略这个属性,多使用需循环引用中,无需多解释吧^^,添加此注解后此元素不会被导出excel

导出

搭建一个简单的项目

新建实体类user

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.Data;

import java.util.Date;
import java.io.Serializable;

/**
 * @author Hmoumou
 */
@Data
@ExcelTarget("users")
public class User implements Serializable {

    @Excel(name = "身份标识")
    private Integer id;
    @Excel(name = "姓名")
    private String name;
    @Excel(name = "年龄")
    private Integer age;
    @Excel(name = "生日")
    private Date dir;

}

image-20210106112034729

image-20210106123818441

新建测试类实现方法

package com.easypoiTry;

import static org.junit.Assert.assertTrue;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.easypoiTry.entity.User;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Unit test for simple App.
 */
public class AppTest 
{
    /**
     * Rigorous Test :-)
     */

//    模拟数据库记录
    public List<User> getUsers(){
        List<User> users = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            User user = new User();
            user.setId(i);
            user.setName("Hmoumou_"+i);
            user.setAge(20+i);
            user.setDir(new Date());
            if (i%2==0){
                user.setStatus("1");
                user.setHobby(Arrays.asList("睡觉","吃饭"));
            }else{
                user.setStatus("0");
                user.setHobby(Arrays.asList("嫦娥","游泳"));
            }
            users.add(user);
        }
        return users;
    }

//    导出excel表
    @Test
    public void testExport() throws IOException {
//        获取数据
        List<User> users = getUsers();
//      导出excel
//        参数1excelParams:导出配置对象;参数2:导出的类型;参数3:导出的数据集合
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户列表测试", "用户信息"), User.class, users);
//      将excel写入指定位置
        FileOutputStream outputStream = new FileOutputStream("C:\\Users\\H某人\\Desktop\\测试excel输出.xls");
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();
    }

    @Test
    public void shouldAnswerWithTrue()
    {
        assertTrue( true );
    }
}

导出list集合

默认样式如下

image-20210106131049276

如果不想使用中括号,可以操作如下

    @ExcelIgnore
    private List<String> hobby;

    @Excel(name = "爱好",width = 20.0,orderNum = "5")
    private String hobbystr;

//可以通过hobbystr转换一下
    public String getHobbystr() {
        StringBuilder sb = new StringBuilder();
        hobby.forEach(e->{
            sb.append(e).append("、");
        });

        return sb.toString();
    }

image-20210106132721729

一对一关系对象导入

image-20210106134331513

image-20210106134345161

image-20210106134358980

package com.easypoiTry.entity;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.Data;

import java.io.Serializable;

@Data
@ExcelTarget("cards")
public class Card implements Serializable {
    @Excel(name = "身份证号码",width = 30.0,orderNum = "6")
    private String cardId;
    @Excel(name = "地址",width = 40.0,orderNum = "7")
    private String address;
}

package com.easypoiTry.entity;


import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelEntity;
import cn.afterturn.easypoi.excel.annotation.ExcelIgnore;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.Data;

import java.util.Date;
import java.io.Serializable;
import java.util.List;

/**
 * @author Hmoumou
 */
@Data
@ExcelTarget("users")
public class User implements Serializable {

    @Excel(name = "身份标识",orderNum = "0")
    private Integer id;
    @Excel(name = "姓名",orderNum = "1")
    private String name;
//    suffix添加后缀
    @Excel(name = "年龄",orderNum = "3",suffix = "岁")
    private Integer age;
//    width是输出时excel的列宽设置,orderNum是输出顺序
    @Excel(name = "生日",width = 20.0,orderNum = "2",format = "yyyy-MM-dd HH:mm:ss")
    private Date dir;
//replace中利用前值替换后值
    @Excel(name = "状态",replace = {"注册_1","未注册_0"},orderNum = "4")
    private String status;

//    @Excel(name = "爱好",orderNum = "5",width = 20.0)
    @ExcelIgnore
    private List<String> hobby;

    @Excel(name = "爱好",width = 20.0,orderNum = "5")
    private String hobbystr;

    public String getHobbystr() {
        StringBuilder sb = new StringBuilder();
        hobby.forEach(e->{
            sb.append(e).append("、");
        });

        return sb.toString();
    }

    @ExcelEntity //标识一对一关系
    private Card card;
}

package com.easypoiTry;

import static org.junit.Assert.assertTrue;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.easypoiTry.entity.Card;
import com.easypoiTry.entity.User;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Unit test for simple App.
 */
public class AppTest 
{
    /**
     * Rigorous Test :-)
     */

//    模拟数据库记录
    public List<User> getUsers(){
        List<User> users = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            User user = new User();
            user.setId(i);
            user.setName("Hmoumou_"+i);
            user.setAge(20+i);
            user.setDir(new Date());
            Card card = new Card();
            card.setCardId("428758188805984585");
            card.setAddress("遍地是家");
            user.setCard(card);
            if (i%2==0){
                user.setStatus("1");
                user.setHobby(Arrays.asList("睡觉","吃饭"));
            }else{
                user.setStatus("0");
                user.setHobby(Arrays.asList("嫦娥","游泳"));
            }
            users.add(user);
        }
        return users;
    }

//    导出excel表
    @Test
    public void testExport() throws IOException {
//        获取数据
        List<User> users = getUsers();
//      导出excel
//        参数1excelParams:导出配置对象;参数2:导出的类型;参数3:导出的数据集合
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户列表测试", "用户信息"), User.class, users);
//      将excel写入指定位置
        FileOutputStream outputStream = new FileOutputStream("C:\\Users\\H某人\\Desktop\\测试excel输出.xls");
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();
    }

    @Test
    public void shouldAnswerWithTrue()
    {
        assertTrue( true );
    }
}

一对多集合导出

image-20210106135847534

image-20210106135859567

image-20210106135913624

image-20210106135924676

图片导出

image-20210106141433340

image-20210106141443074

//    type为2才能被是被为图片显示
    @Excel(name = "头像信息",orderNum = "9",width = 60.0,height = 60.0,type = 2,imageType = 1)
    private String photo;

大数据导出

使用exportBigExcel

导入

简单导入实现

创建对应实体类

package com.easypoiTry.entity;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
@ExcelTarget("imps")
public class emp implements Serializable {

    @Excel(name = "编号")
    private String id;
    @Excel(name = "姓名")
    private String name;
    @Excel(name = "生日")
    private Date bir;
    @Excel(name = "年龄")
    private Integer age;
    @Excel(name = "状态",replace = {"激活_1","锁定_0"})
    private String status;

}

写测试类

    //导入普通数据
    @Test
    public void testImport() throws Exception {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        List<emp> emps = ExcelImportUtil.importExcel(new FileInputStream("C:\\Users\\H某人\\Desktop\\导入测试数据.xls"), emp.class, params);
        emps.forEach(System.out::println);
    }

导入技巧

image-20210106145937683

springboot项目实战

初始化项目

新建springboot项目

image-20210106152307461

image-20210106152342510

引入相关依赖

  • mybatis
  • mysql
  • druid
  • easypoi
  • thymeleaf
  • lombok
 <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
     <!--        引入相关依赖-->
<!--        引入mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
<!--        引入mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
<!--        引入druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.19</version>
        </dependency>
<!--        引入thymeleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--    easyPOI依赖引入-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>4.2.0</version>
        </dependency>

编写配置文件application.yml

server:
  port: 8989
spring:
  thymeleaf:
    cache: false
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/easypoi?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8
    username: root
    password: root
  application:
    name: easypoi
mybatis:
  mapper-locations: classpath:com/easypoitry/easypoi_springboot/mapper/*.xml
  type-aliases-package: com.easypoi.easypoi_springboot.entity

项目结构

image-20210106154902079

简易前端

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
        integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>

<body>
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <h1>选择Excel文件导入到数据中</h1>
                <form action="" method="POST" class="form-inline">
                    <div class="form-group">
                        <input type="file" class="form-control" name="excelFile">
                        <input type="submit" class="btn btn-danger" value="导入数据">
                    </div>
                </form>
            </div>
            <div class="col-md-12">
                <h1>显示导入数据列表</h1>
                <table class="table table-bordered">
                    <tr>
                        <th>编号</th>
                        <th>头像</th>
                        <th>姓名</th>
                        <th>年龄</th>
                        <th>生日</th>
                    </tr>
                    <tr>
                        <td>1</td>
                        <td><img src="" alt=""></td>
                        <td>Hmouren</td>
                        <td>23</td>
                        <td>2021.01.01</td>
                    </tr>
                    <tr>
                        <td>1</td>
                        <td><img src="" alt=""></td>
                        <td>Hmouren</td>
                        <td>23</td>
                        <td>2021.01.01</td>
                    </tr>
                </table>
                <hr>
                <input type="button" class="btn btn-info" value="导出excel">
            </div>
        </div>
    </div>
</body>

</html>

建库建表

/*
Navicat MySQL Data Transfer

Source Server         : 201705092006
Source Server Version : 50726
Source Host           : localhost:3306
Source Database       : easypoi

Target Server Type    : MYSQL
Target Server Version : 50726
File Encoding         : 65001

Date: 2021-01-06 16:19:52
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
  `id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `bir` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `habbys` varchar(255) DEFAULT NULL,
  `no` varchar(255) DEFAULT NULL,
  `photo` varchar(255) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_user
-- ----------------------------

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以帮你输出一篇Selenium学习笔记。以下是一份简单的学习笔记供参考: Selenium学习笔记 Selenium是一款用于自动化Web应用程序测试的强大工具。它提供了一组丰富的API,可以与各种编程语言(如Python、Java、C#等)结合使用,以实现Web应用程序的自动化测试。 1. 安装和配置 首先,你需要安装Selenium库。如果你使用Python,可以使用以下命令安装: ``` pip install selenium ``` 安装完成后,你还需要下载并配置相应的浏览器驱动程序。Selenium支持各种浏览器,如Chrome、Firefox、Safari等。你可以在Selenium官方网站上找到相应的驱动程序下载链接。 2. 基本操作 Selenium提供了一系列方法来模拟用户在浏览器中的操作,如打开URL、点击元素、输入文本等。以下是一些常用的基本操作示例: - 打开浏览器并访问URL: ```python from selenium import webdriver driver = webdriver.Chrome() # 使用Chrome浏览器驱动 driver.get("https://www.example.com") # 打开指定URL ``` - 查找元素并操作: ```python element = driver.find_element_by_id("element_id") # 根据元素ID查找 element.click() # 点击元素 input_element = driver.find_element_by_name("input_name") # 根据元素名称查找 input_element.send_keys("Hello, World!") # 输入文本 ``` - 执行JavaScript代码: ```python driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # 滚动到页面底部 ``` 3. 高级操作 Selenium还提供了一些高级操作,如处理弹窗、切换窗口、等待元素加载等。以下是一些常用的高级操作示例: - 处理弹窗: ```python alert = driver.switch_to.alert # 切换到弹窗 alert.accept() # 接受弹窗 confirm = driver.switch_to.alert # 切换到确认框 confirm.dismiss() # 取消确认框 ``` - 切换窗口: ```python window_handles = driver.window_handles # 获取所有窗口句柄 driver.switch_to.window(window_handles[-1]) # 切换到最后一个窗口 ``` - 等待元素加载: ```python from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "element_id")) ) ``` 4. 测试框架 Selenium还可以与测试框架(如Pytest、JUnit等)结合使用,以便更好地组织和管理测试用例。你可以使用测试框架提供的断言方法来验证测试结果。 ```python import pytest def test_example(): driver = webdriver.Chrome() driver.get("https://www.example.com") assert "Example Domain" in driver.title # 断言页面标题包含指定文本 driver.quit() ``` 以上是一份简单的Selenium学习笔记,希望对你有所帮助。当然,Selenium还有很多其他功能和用法,你可以进一步深入学习和探索。祝你学习愉快!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值