Apache POI 4.0.1版本读取普通Excel文件(兼容 xls 和 xlsx)(一)
2021-03-05 12:28
标签:png rom 读取 temp ati switch example ooxml 列表 基于最新的 代码编写前准备-》 添加POI 4.0.1 maven依赖: 上代码:
* 当该行中有单元格的数据为空或不合法时,忽略该行的数据
*
* 结果: Apache POI 4.0.1版本读取普通Excel文件(兼容 xls 和 xlsx)(一) 标签:png rom 读取 temp ati switch example ooxml 列表 原文地址:https://www.cnblogs.com/weigy/p/12905857.htmlApache POI 4.0.1
版本来总结一下整个读取和写入Excel的过程。xml version="1.0" encoding="UTF-8"?>
project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
modelVersion>4.0.0modelVersion>
groupId>com.springbootedemogroupId>
artifactId>springboot-demoartifactId>
version>0.0.1-SNAPSHOTversion>
packaging>jarpackaging>
name>springboot-demoname>
description>Demo project for Spring Bootdescription>
parent>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-parentartifactId>
version>2.0.6.RELEASEversion>
relativePath/>
parent>
properties>
project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
java.version>1.8java.version>
properties>
dependencies>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-webartifactId>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-testartifactId>
scope>testscope>
dependency>
dependency>
groupId>org.jsoupgroupId>
artifactId>jsoupartifactId>
version>1.11.3version>
dependency>
dependency>
groupId>com.alibabagroupId>
artifactId>fastjsonartifactId>
version>1.2.41version>
dependency>
dependency>
groupId>org.apache.commonsgroupId>
artifactId>commons-lang3artifactId>
version>3.10version>
dependency>
dependency>
groupId>commons-beanutilsgroupId>
artifactId>commons-beanutilsartifactId>
version>1.8.3version>
dependency>
example-01.xlsx 文件 ,excel数据准备:
DataVo(封装数据VO类)
package com.springbootemaildemo.excel.b;
/**
* 读取Excel时,封装读取的每一行的数据
*/
public class DataVo {
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
ExcelReader(主要读取excel文件代码类)
package com.springbootemaildemo.excel.b;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
/**
* 读取Excel内容
*/
public class ExcelReader {
private static final Logger logger = LoggerFactory.getLogger(ExcelReader.class);
private static final String XLS = "xls";
private static final String XLSX = "xlsx";
/**
* 根据文件后缀名类型获取对应的工作簿对象
*
* @param inputStream 读取文件的输入流
* @param fileType 文件后缀名类型(xls或xlsx)
* @return 包含文件数据的工作簿对象
* @throws IOException
*/
public static Workbook getWorkbook(InputStream inputStream, String fileType) throws IOException {
Workbook workbook = null;
if (fileType.equalsIgnoreCase(XLS)) {
workbook = new HSSFWorkbook(inputStream);
} else if (fileType.equalsIgnoreCase(XLSX)) {
workbook = new XSSFWorkbook(inputStream);
}
return workbook;
}
/**
* 读取Excel文件内容
*
* @param fileName 要读取的Excel文件所在路径
* @return 读取结果列表,读取失败时返回null
*/
public static List
MyTest(测试类)
package com.springbootemaildemo.excel.b;
import org.apache.commons.collections4.CollectionUtils;
import java.util.List;
public class MyTest {
public static void main(String[] args) {
// 设定Excel文件所在路径
String excelFileName = "E:\\files\\example-01.xlsx";
// 读取Excel文件内容
List
文章标题:Apache POI 4.0.1版本读取普通Excel文件(兼容 xls 和 xlsx)(一)
文章链接:http://soscw.com/index.php/essay/60444.html