1、添加依賴java
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
2、工具類
根據文件後綴判斷 2003 || 2007 || 2010 格式。apache
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;xss
import java.io.InputStream;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;工具
public class ExcelUtils {.net
private static Workbook wb;
private static Sheet sheet;
private static Row row;excel
private static final String EXCEL_XLS = "xls";
private static final String EXCEL_XLSX = "xlsx";orm
/**
* 讀取表頭
*
* @param inputStream inputStream
* @param suffix file suffix
* @return map <index,value>
*/
public static Map<Integer, String> readExcelTitle(InputStream inputStream, String suffix) {
getWorkbook(inputStream, suffix);
sheet = wb.getSheetAt(0);
row = sheet.getRow(0);
// 標題總列數
int colNum = row.getPhysicalNumberOfCells();
Map<Integer, String> map = new HashMap<>();
for (int i = 0; i < colNum; i++) {
map.put(i, row.getCell(i).getStringCellValue());
}
return map;
}xml
/**
* 讀取excel內容
*
* @param inputStream 文件
* @return Map<行, Map < 下標, Object>>
*/
public static Map<Integer, Map<Integer, String>> readExcelContent(InputStream inputStream, String suffix) {
getWorkbook(inputStream, suffix);
Map<Integer, Map<Integer, String>> content = new HashMap<>();
sheet = wb.getSheetAt(0);
// 獲得總行數
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
// 正文內容應該從第二行開始,第一行爲表頭的標題
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 0;
Map<Integer, String> cellValue = new HashMap<>();
while (j < colNum) {
String obj = getCellFormatValue(row.getCell(j));
cellValue.put(j, obj);
j++;
}
content.put(i, cellValue);
}
return content;
}blog
private static String getCellFormatValue(Cell cell) {
String cellValue = "";
if (cell != null) {
// 判斷當前Cell的Type
switch (cell.getCellType()) {
// 若是當前Cell的Type爲NUMERIC
case Cell.CELL_TYPE_NUMERIC:
case Cell.CELL_TYPE_FORMULA: {
// 判斷當前的cell是否爲Date
if (DateUtil.isCellDateFormatted(cell)) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss");
Instant instant = cell.getDateCellValue().toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
cellValue = dateTimeFormatter.format(localDateTime);
} else {
// 若是是純數字
// 取得當前Cell的數值
cellValue = String.valueOf(cell.getNumericCellValue());
}
break;
}
// 若是當前Cell的Type爲STRING
case Cell.CELL_TYPE_STRING:
// 取得當前的Cell字符串
cellValue = cell.getRichStringCellValue().getString();
break;
default:
// 默認的Cell值
cellValue = "";
}
}
return cellValue;
}字符串
private static void getWorkbook(InputStream inputStream, String suffix) {
try { //2003 if (EXCEL_XLS.equals(suffix)) { wb = new HSSFWorkbook(inputStream); //2007/2010 } else if (EXCEL_XLSX.equals(suffix)) { wb = new XSSFWorkbook(inputStream); } } catch (Exception e) { e.printStackTrace(); } finally { try { inputStream.close(); } catch (Exception e) { e.printStackTrace(); } } }