Java-Excel导入(参数:kay=文件地址,value=filepath)
ExcelUtil
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.xmlbeans.impl.piccolo.io.FileFormatException;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.util.*;
@Slf4j
public class ExcelUtil {
public static final String EXTENSION_XLS = "xls";
public static final String EXTENSION_XLSX = "xlsx";
/**
* <pre>
* 取得Workbook对象(xls和xlsx对象不同,不过都是Workbook的实现类) xls:HSSFWorkbook
* xlsx:XSSFWorkbook
* @param filePath
* @return
* @throws IOException </pre>
*/
private static Workbook getWorkbook(String filePath) throws IOException {
Workbook workbook = null;
// 检查
preReadCheck(filePath);
InputStream is = new FileInputStream(filePath);
// 获取workbook对象
if (filePath.endsWith(EXTENSION_XLS)) {
workbook = new HSSFWorkbook(is);
} else if (filePath.endsWith(EXTENSION_XLSX)) {
workbook = new XSSFWorkbook(is);
}
is.close();
return workbook;
}
/**
* 文件检查
*
* @param filePath
* @throws FileNotFoundException
* @throws FileFormatException
*/
private static void preReadCheck(String filePath) throws FileNotFoundException, FileFormatException {
// 常规检查
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException("传入的文件不存在:" + filePath);
}
if (!(filePath.endsWith(EXTENSION_XLS) || filePath.endsWith(EXTENSION_XLSX))) {
throw new FileFormatException("传入的文件不是excel");
}
}
/**
* 读取excel文件内容
*
* @param filePath
* @throws FileNotFoundException
* @throws FileFormatException
*/
public static List<Map<String, String>> readAllExcel(String filePath) throws FileNotFoundException, FileFormatException {
List<Map<String, String>> res = new ArrayList<>();
Workbook workbook = null;
try {
workbook = getWorkbook(filePath);
// 读文件 一个sheet一个sheet地读取
for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
Sheet sheet = workbook.getSheetAt(numSheet);
if (sheet == null) {
continue;
}
log.info("=======================" + sheet.getSheetName() + "=========================");
int firstRowIndex = sheet.getFirstRowNum();
int lastRowIndex = sheet.getLastRowNum();
// 读取首行 即,表头
Row firstRow = sheet.getRow(firstRowIndex);
if (firstRow != null) {
for (int i = firstRow.getFirstCellNum(); i <= firstRow.getLastCellNum(); i++) {
Cell cell = firstRow.getCell(i);
String cellValue = getCellValue(cell, true);
}
// 读取数据行
for (int rowIndex = firstRowIndex + 1; rowIndex <= lastRowIndex; rowIndex++) {
Map<String, String> map = new HashMap<>();
Row currentRow = sheet.getRow(rowIndex);// 当前行
int firstColumnIndex = currentRow.getFirstCellNum(); // 首列
int lastColumnIndex = currentRow.getLastCellNum();// 最后一列
String s = "";
for (int columnIndex = firstColumnIndex; columnIndex <= lastColumnIndex; columnIndex++) {
Cell currentCell = currentRow.getCell(columnIndex);// 当前单元格
String currentCellValue = getCellValue(currentCell, true);// 当前单元格的值
map.put("cell" + columnIndex, currentCellValue);
s += currentCellValue + "\t";
}
log.info(s);
res.add(map);
}
log.info("======================================================");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (workbook != null) {
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return res;
}
/**
* 测试入口
*/
public static void main(String[] args) throws FileFormatException, FileNotFoundException { } }