Excel数据导入数据库

时间:2023-03-09 21:30:22
Excel数据导入数据库

maven依赖

 <!--excel相关依赖-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
</dependency> <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.8-beta4</version>
</dependency> <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>

工具类

 package com.yangche.utils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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 java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; public class ExcelUtil {
private final static String excel2003L =".xls"; //2003- 版本的excel
private final static String excel2007U =".xlsx"; //2007+ 版本的excel
/**
* 描述:获取IO流中的数据,组装成List<List<Object>>对象
* @param in,fileName
* @return
* @throws IOException
*/
public List<List<Object>> getBankListByExcel(InputStream in,String fileName) throws Exception{
List<List<Object>> list = null;
//创建Excel工作薄
Workbook work = this.getWorkbook(in,fileName);
if(null == work){
throw new Exception("创建Excel工作薄为空!");
}
Sheet sheet = null; //页数
Row row = null; //行数
Cell cell = null; //列数
list = new ArrayList<List<Object>>();
//遍历Excel中所有的sheet
// 将最大的列数记录下来
int lastCellNum = 0;
for (int i = 0; i < work.getNumberOfSheets(); i++) {
sheet = work.getSheetAt(i);
if(sheet==null){continue;}
//遍历当前sheet中的所有行
for (int j = sheet.getFirstRowNum()+1; j <= sheet.getLastRowNum(); j++) {//+1是为了忽略第一行的值(表头信息的东西,就不要了)根据需要来,如果你要这个表头信息就别加1
row = sheet.getRow(j);
if(row==null||row.getFirstCellNum()==j){continue;}
//遍历所有的列
List<Object> li = new ArrayList<Object>();
// 比较当前行的列数跟表的最大的列数
if (j == sheet.getFirstRowNum()) {
// 将第一行的列数设为最大
lastCellNum = row.getLastCellNum();
}else {
lastCellNum = lastCellNum > row.getLastCellNum() ? lastCellNum : row.getLastCellNum();
}
for (int y = row.getFirstCellNum(); y < lastCellNum; y++) {
cell = row.getCell(y);
li.add(this.getValue(cell));
}
list.add(li);
}
}
return list;
} /**
* 描述:根据文件后缀,自适应上传文件的版本
* @param inStr,fileName
* @return
* @throws Exception
*/
public Workbook getWorkbook(InputStream inStr,String fileName) throws Exception{
Workbook wb = null;
String fileType = fileName.substring(fileName.lastIndexOf("."));
if(excel2003L.equals(fileType)){
wb = new HSSFWorkbook(inStr); //2003-
}else if(excel2007U.equals(fileType)){
wb = new XSSFWorkbook(inStr); //2007+
}else{
throw new Exception("解析的文件格式有误!");
}
return wb;
}
/**
* 描述:对表格中数值进行格式化
* @param cell
* @return
*/
//解决excel类型问题,获得数值
public String getValue(Cell cell) {
String value = "";
if(null==cell){
return value;
}
switch (cell.getCellType()) {
//数值型
case Cell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
//如果是date类型则 ,获取该cell的date值
Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
// 根据自己的实际情况,excel表中的时间格式是yyyy-MM-dd HH:mm:ss还是yyyy-MM-dd,或者其他类型
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 由于方法的返回值类型为String,这里将Date类型转为String,便于统一返回数据
value = format.format(date);;
}else {// 纯数字
BigDecimal big=new BigDecimal(cell.getNumericCellValue());
value = big.toString();
//解决1234.0 去掉后面的.0
if(null!=value&&!"".equals(value.trim())){
String[] item = value.split("[.]");
if(1<item.length&&"0".equals(item[1])){
value=item[0];
}
}
}
break;
//字符串类型
case Cell.CELL_TYPE_STRING:
value = cell.getStringCellValue().toString();
break;
// 公式类型
case Cell.CELL_TYPE_FORMULA:
//读公式计算值
value = String.valueOf(cell.getNumericCellValue());
if (value.equals("NaN")) {// 如果获取的数据值为非法值,则转换为获取字符串
value = cell.getStringCellValue().toString();
}
break;
// 布尔类型
case Cell.CELL_TYPE_BOOLEAN:
value = " "+ cell.getBooleanCellValue();
break;
default:
value = cell.getStringCellValue().toString();
}
if("null".endsWith(value.trim())){
value="";
}
return value;
}
}

以下是用法:

Excel数据导入数据库

在测试类中进行测试

     @Test
public void excelReadTwo()throws Exception {
String filepath = "C:\\Users\\Administrator\\Desktop\\haha.xlsx";
FileInputStream inputStream = new FileInputStream(new File(filepath));
ExcelUtil excelUtil = new ExcelUtil();
List<List<Object>> bankListByExcel = excelUtil.getBankListByExcel(inputStream, "haha.xlsx");
System.out.println(bankListByExcel);
}

输出的信息为:

[[杨彻, 10, 男], [吴竞, , 女], [张三, 11, 女], [李四, 89, 女, ], [王五, , 女, ], [赵六, 44, 女, ]] 因为有多个sheet,所有sheet中的值都会遍历,可以灵活改变,自己改工具类中的sheet代码。

如果有空值,两个逗号中间是空的,表头的字段没有是因为忽略了sheet有值的第一行。一般会配置一个表,存和excel文件对应的字段,排序,还有和domain对应的字段,利用反射机制(通过拿到domain的变量名,获取其get或set方法并把excel中的值放进去)表的每一行都对应一个domain的实体。然后再存到数据库中。我的博客中会贴出反射机制的用法。

如果是前端页面的话,可以直接拿到io流,不需要存临时文件,直接把excel数据存到数据库(如果这个上传的文件不需要的话)

 public void paasCmdbHostExcelFileUpload(CommonsMultipartFile excelFile){
if(excelFile==null){
System.out.println("未选择任何文件");
}
String fileName=excelFile.getOriginalFilename();
InputStream inputStream=null;
try {
inputStream=excelFile.getInputStream();
} catch (IOException e) {
System.out.println("获取文件流失败");
e.printStackTrace();
}
//到此已经获得了需要的输入流和文件名,可以直接传到excel工具类使用,导入数据库的代码应该是非常简单的。
}