Featured image of post 【合集】EasyPOI-读取excel

【合集】EasyPOI-读取excel

相关文章

【合集】EasyPOI-写出excel

【合集】EasyExcel-读取excel

【合集】EasyExcel-写出excel

依赖

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<!-- https://mvnrepository.com/artifact/cn.afterturn/easypoi-spring-boot-starter -->
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-spring-boot-starter</artifactId>
    <version>4.5.0</version>
    <exclusions>
        <exclusion>
            <artifactId>hutool-all</artifactId>
            <groupId>cn.hutool</groupId>
        </exclusion>
    </exclusions>
</dependency>

Spring-Web读

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;

/**
 * easyPoi读取文件
 */
private static void easyPoiReadExcel(MultipartFile file) {
    try {
        InputStream inputStream = file.getInputStream();
        ImportParams params = new ImportParams();
        params.setTitleRows(0);
        params.setHeadRows(1);
        List<DemoVO> voList = ExcelImportUtil.importExcel(inputStream, DemoVO.class, params);
        Assert.notEmpty(voList, "文件数据不可为空!");
    } catch (Exception e) {
        log.error("读取Excel文件失败", e);
        throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "读取Excel文件失败");
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DemoEasyPoiExcelVO {

//    @Excel(name = "开始时间",width = 15.0,importFormat = "HH:mm",exportFormat = "HH:mm")

    // 隐藏主键列
    @Excel(name = "id", width = 20.0, isColumnHidden = true)
    private Long id;

    @Excel(name = "序号", width = 20.0)
    private Integer number;

    @Excel(name = "开始日", format = "yyyy-MM-dd", width = 20.0)
    private LocalDate startDate;

    @Excel(name = "开始时间", format = "yyyy-MM-dd HH:mm:ss", width = 20.0)
    private LocalDateTime startTime;

    @Excel(name = "换电模式*", width = 20.0, replace = {"后背电池换电_0", "底盘电池换电_1", "_null"})
    private String swapMode;

    @Excel(name = "金额", numFormat = "0.00")
    private BigDecimal amount;

}
皖ICP备2024056275号-1
发表了80篇文章 · 总计150.57k字
本站已稳定运行