前几天写了怎么导出Excel,今天要做的是Excel导入数据。Excel导入是个比较常见的场景,一般公司都有工具类,
笔者还是自己动手写了一个,方便后续参考。
在导入的过程中,解决了当数字较长时导进来变成了科学计数法的问题。虽然网上好多解决方案,但我发现当确定该单元格的内容是数字后,直接把单元格类型设为字符串类型就可以了,不过07版本以后的excel还是要自己手动转换下。具体见下面代码加粗部分。
一、maven依赖
<!-- poi的jar包 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>11
1
<!-- poi的jar包 -->2
<dependency>3
<groupId>org.apache.poi</groupId> 4
<artifactId>poi</artifactId>5
<version>3.15</version>6
</dependency>7
<dependency>8
<groupId>org.apache.poi</groupId>9
<artifactId>poi-ooxml</artifactId>10
<version>3.15</version>11
</dependency>二、代码
/**
* 解析Excel文件的方法
* @param excelFile Excel文件
* @param fileName 文件名
* @return List<ArrayList<String>>
* @throws IOException
*/
public static List<ArrayList<String>> parseExcel(File excelFile,String fileName) throws IOException{
// 判断文件格式
boolean isExcel03 = fileName.matches(".+\.(?i)(xls)");//这里的(?i)代表忽略大小写
FileInputStream fis = new FileInputStream(excelFile);
// 读取工作簿
Workbook wb = isExcel03 ? new HSSFWorkbook(fis) : new XSSFWorkbook(fis);
// 读取工作表
Sheet sheet = wb.getSheetAt(0);
List<ArrayList<String>> resultData = new ArrayList<ArrayList<String>>(); //存放返回数据
// 开始每行的读取
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if(null != row) {
ArrayList<String> rowData = new ArrayList<>();
short lastCellNum = row.getLastCellNum();
for (int j = 0; j < lastCellNum; j++) {
Cell cell = row.getCell(j);
String cellValue = null;
if(null != cell){
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BLANK: // 空白
cellValue = null;
break;
case Cell.CELL_TYPE_STRING: // 文本
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC: // 数字或者日期
if(DateUtil.isCellDateFormatted(cell)){ // 是否是日期
Date date = cell.getDateCellValue();
cellValue = date == null ? null : new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
} else {
//防止变成因为数字太长变成科学计数法
cell.setCellType(CellType.STRING);
if(isExcel03){
cellValue = cell.getStringCellValue();
} else {
BigDecimal bd = new BigDecimal(cell.getStringCellValue());
cellValue = bd.toPlainString();
}
}
break;
case Cell.CELL_TYPE_BOOLEAN: // 布尔型
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
default:
cellValue = null;
break;
}
}
rowData.add(cellValue); // 每个单元格数据存入行数据集合
}
resultData.add(rowData); // 每行数据存入返回数据集合
}
}
// 关流
wb.close();
fis.close();
return resultData;
}1
/**2
* 解析Excel文件的方法3
* @param excelFile Excel文件4
* @param fileName 文件名5
* @return List<ArrayList<String>>6
* @throws IOException7
*/8
public static List<ArrayList<String>> parseExcel(File excelFile,String fileName) throws IOException{9
// 判断文件格式10
boolean isExcel03 = fileName.matches(".+\.(?i)(xls)");//这里的(?i)代表忽略大小写11
FileInputStream fis = new FileInputStream(excelFile);12
// 读取工作簿13
Workbook wb = isExcel03 ? new HSSFWorkbook(fis) : new XSSFWorkbook(fis);14
// 读取工作表15
Sheet sheet = wb.getSheetAt(0); 16
17
List<ArrayList<String>> resultData = new ArrayList<ArrayList<String>>(); //存放返回数据18
// 开始每行的读取19
for (int i = 0; i <= sheet.getLastRowNum(); i++) {20
Row row = sheet.getRow(i);21
if(null != row) {22
ArrayList<String> rowData = new ArrayList<>();23
short lastCellNum = row.getLastCellNum();24
for (int j = 0; j < lastCellNum; j++) {25
Cell cell = row.getCell(j);26
String cellValue = null;27
if(null != cell){28
switch (cell.getCellType()) {29
case Cell.CELL_TYPE_BLANK: // 空白30
cellValue = null;31
break;32
case Cell.CELL_TYPE_STRING: // 文本33
cellValue = cell.getStringCellValue();34
break;35
case Cell.CELL_TYPE_NUMERIC: // 数字或者日期36
if(DateUtil.isCellDateFormatted(cell)){ // 是否是日期37
Date date = cell.getDateCellValue();38
cellValue = date == null ? null : new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);39
} else {40
//防止变成因为数字太长变成科学计数法41
cell.setCellType(CellType.STRING);42
if(isExcel03){43
cellValue = cell.getStringCellValue();44
} else {45
BigDecimal bd = new BigDecimal(cell.getStringCellValue());46
cellValue = bd.toPlainString();47
}48
}49
break;50
case Cell.CELL_TYPE_BOOLEAN: // 布尔型51
cellValue = String.valueOf(cell.getBooleanCellValue());52
break;53
default:54
cellValue = null;55
break;56
}57
}58
rowData.add(cellValue); // 每个单元格数据存入行数据集合59
}60
resultData.add(rowData); // 每行数据存入返回数据集合61
}62
}63
// 关流64
wb.close();65
fis.close();66
return resultData;67
} 这个代码其实很简单的,导入这里判读了一下是什么类型的文件,因此需要传入文件名。
待续。。。