excel 导入导出 util
package com.soft.util;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
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 java.io.InputStream;
import java.util.*;
public class ExcelUtil {
// 2007版本前
public static final String XLS = ".XLS";
// 2007版本后
public static final String XLSX = ".XLSX";
// 导入excel表格的解析方法
public static List<Map<String,Object>> importExcel(String suffix, InputStream inputStream) throws Exception{
// 判断是否为错误输入
if(suffix == null || "".equals(suffix) || inputStream == null){
return null;
}
// 创建读取类
Workbook wb = null;
if(XLS.equalsIgnoreCase(suffix)){
wb = new HSSFWorkbook(inputStream);
}else{
wb = new XSSFWorkbook(inputStream);
}
// 获取第一个表
Sheet sheet = wb.getSheetAt(0);
// 总行数
int rowNum = sheet.getLastRowNum();
// 创建结果集
List<Map<String,Object>> list = null;
// 标题的数组
String[] title = null;
// 单行的数据
Map<String, Object> map = null;
// 循环并取出来所有的cell内容
for(int r = 0;r <= rowNum;r++){
if(list == null) {
list = new ArrayList<>();
}
// 第一行为标题行,不操作数据
if(r > 0 && map == null) {
map = new HashMap<>();
}
// 行对象
Row row = sheet.getRow(r);
// 获取某一个的最后一列(总列数)
int cellNum = row.getLastCellNum();
// 循环行中的列
for(int c = 0; c < cellNum; c++) {
// 单元格对象
Cell cell = row.getCell(c);
// 单元格中的内容,设置格式为String
cell.setCellType(CellType.STRING);
String value = cell.getStringCellValue();
// 代表第一行(标题行)
if(r == 0) {
if(title == null) {
title = new String[cellNum];
}
title[c] = value;
// 数据行
} else {
map.put(title[c], value);
}
}
// 将每一行的数据放到list中
if(map != null) {
list.add(map);
}
map = null;
}
// 关闭资源
wb.close();
inputStream.close();
return list;
}
public static Workbook createExcel(List<Map<String, Object>> list, String suffix) throws Exception {
// 传入的数据为空时直接返回null
if(list == null || list.size() == 0) {
return null;
}
// 如果没有提供后缀,则默认提供一个
if(suffix == null || "".equals(suffix)) {
// 2007以前版本
suffix = XLS;
}
// 获取excel对象
Workbook wb = null;
// 根据后缀创建响应的对象
if(XLS.equalsIgnoreCase(suffix)) {
wb = new HSSFWorkbook();
} else {
wb = new XSSFWorkbook();
}
// 创建sheet
Sheet sheet = wb.createSheet("peter");
// 防止null键值对的出现
Set<String> keySet = null;
// 真实的行数
int r = 0;
// 遍历数据
for(int i = 0; i < list.size();) {
// 创建行
Row row = sheet.createRow(r);
// 获取List中的单个Map
Map<String, Object> map = list.get(i);
// 遍历map集合
// 获取map中key的集合
// 只在第一次的时候遍历键值对
if(i == 0) {
keySet = map.keySet();
}
// 编辑set集合
Iterator<String> iterator = keySet.iterator();
System.out.println(keySet);
System.out.println(keySet.size());
// 单元格个数累加
int c = 0;
while(iterator.hasNext()) {
// 创建行中的列
Cell cell = row.createCell(c++);
// map的key也就是标题
String key = iterator.next();
// 如果是第一行,则添加标题
if(r == 0) {
cell.setCellValue(key);
// 如果不是第一行,则编写数据
} else {
// 如果获取不到值,则输入空
Object value = map.get(key);
if(value != null) {
cell.setCellValue((String)map.get(key));
}else {
cell.setCellValue(" ");
}
}
}
// 因为要生成标题行,所以要求list数据的第一条要获取两次。
// 如果不是第一行,那么再次获取list第一条数据,开始编辑数据。
if(r != 0) {
i++;
}
// 行累加
r++;
}
// 关闭资源
wb.close();
return wb;
}
}
poi包
<!--在pom文件添加excel文件导入导出支持-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
springboot启动类
//springboot启动类
package com.soft;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
//在启动类中覆盖原有的内置的tomcat,需要继承SpringBootServletInitializer,重写configure方法
@SpringBootApplication//标示为springBoot类型
@MapperScan("com.soft.mapper")//扫描mapper
@EnableTransactionManagement//开启事务管理
@EnableScheduling//开启定时任务
public class MainApplication/* extends SpringBootServletInitializer */{
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
/* @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(MainApplication.class);
}*/
}
pom文件依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!--要使用最新版本的thymeleaf需要额外配置版本信息-->
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.4.1</thymeleaf-layout-dialect.version>
</properties>
<!--SpringBoot的父依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- web环境的场景启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--tomcat场景启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!--Oracle驱动-->
<dependency>
<groupId>ojdbc</groupId>
<artifactId>ojdbc</artifactId>
<version>6.0</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!--Druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.17</version>
</dependency>
<!--log4j-->
<!--禁用自带的log4j-->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!–引入自定义的log4j–>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>-->
<!--实体类configure依赖包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--thymeleaf依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>