public class ReadExcleutil {
private static final Logger log = Logger.getLogger(String.valueOf(ReadExcleutil.class));
public static List<JSONObject> getExcelData(MultipartFile file, int startRow,int endRow,int startCell,int endCell) throws IOException, InvalidFormatException {
int resultSize = 0;
List<JSONObject> resultData = new ArrayList<>();
if (!checkFile(file)) {
log.info("上传的excel文件格式有问题");
return resultData;
}
InputStream ins = file.getInputStream();
Workbook workbook = WorkbookFactory.create(ins);
if (workbook != null) {
Sheet sheet = workbook.getSheetAt(0);
if (sheet == null) {
return resultData;
}
resultSize = sheet.getLastRowNum() + 1;
int firstRowNum = sheet.getFirstRowNum();
int lastRowNum = sheet.getLastRowNum();
if(endRow>0){
lastRowNum = endRow;
}
for (int rowNum = firstRowNum + startRow; rowNum <= lastRowNum; rowNum++) {
Row row = sheet.getRow(rowNum);
if (rowIsEmpty(row)) {
break;
}
int firstCellNum = row.getFirstCellNum();
if(startCell>0){
firstCellNum = startCell;
}
int lastCellNum = row.getLastCellNum();
if(endCell>0){
lastCellNum = endCell;
}
JSONObject js = new JSONObject();
for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {
Cell cell = row.getCell(cellNum);
js.put(String.valueOf(cellNum),getCellValue(cell));
}
resultData.add(js);
}
workbook.close();
}
return resultData;
}
public static boolean checkFile(MultipartFile file) throws IOException {
if (null == file) {
log.info("文件不存在!");
return false;
}
String fileName = file.getOriginalFilename();
if (!fileName.endsWith("xls") && !fileName.endsWith("xlsx")) {
log.info(fileName + "不是excel文件");
return false;
}
return true;
}
public static String getCellValue(Cell cell) {
String cellValue = "";
if (cell == null) {
return cellValue;
}
switch (cell.getCellTypeEnum()) {
case NUMERIC:
cellValue = stringDateProcess(cell);
break;
case STRING:
cellValue = String.valueOf(cell.getStringCellValue());
break;
case BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case FORMULA:
cellValue = String.valueOf(cell.getCellFormula());
break;
case BLANK:
cellValue = "";
break;
case ERROR:
cellValue = "非法字符";
break;
default:
cellValue = "未知类型";
break;
}
return cellValue;
}
public static String stringDateProcess(Cell cell) {
String result = new String();
if (HSSFDateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat sdf = null;
if (cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")) {
sdf = new SimpleDateFormat("HH:mm");
} else {
sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
}
Date date = cell.getDateCellValue();
result = sdf.format(date);
} else if (cell.getCellStyle().getDataFormat() == 58) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
double value = cell.getNumericCellValue();
Date date = DateUtil
.getJavaDate(value);
result = sdf.format(date);
} else {
double value = cell.getNumericCellValue();
CellStyle style = cell.getCellStyle();
DecimalFormat format = new DecimalFormat();
String temp = style.getDataFormatString();
if (temp.equals("General")) {
format.applyPattern("0");
}
result = format.format(value);
}
return result;
}
public static boolean rowIsEmpty(Row row) {
if (null == row) {
return true;
}
for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
Cell cell = row.getCell(c);
if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) {
return false;
}
}
return true;
}
}