package com.cjavapy.utils.util;
import org.apache.poi.hssf.usermodel.HSSFCell;
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.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
public class ImportExcelUtils {
public static final Workbook createWorkbook(String filePath) throws IOException {
if (filePath.trim().toLowerCase().endsWith("xls")) {
return new XSSFWorkbook(new FileInputStream(filePath));
} else if (filePath.trim().toLowerCase().endsWith("xlsx")) {
return new XSSFWorkbook(new FileInputStream(filePath));
} else {
throw new IllegalArgumentException("不是有效的excel文件格式");
}
}
public static final Workbook createWorkbook(MultipartFile uploadFile) throws IOException {
return new XSSFWorkbook(uploadFile.getInputStream());
}
public static final Sheet getSheet(Workbook wb, String sheetName) {
return wb.getSheet(sheetName);
}
public static final Sheet getSheet(Workbook wb, int index) {
return wb.getSheetAt(index);
}
public static final List<Object[]> listFromSheet(Sheet sheet) {
List<Object[]> list = new ArrayList<Object[]>();
for (int r = sheet.getFirstRowNum(); r <= sheet.getLastRowNum(); r++) {
Row row = sheet.getRow(r);
if (row == null || row.getPhysicalNumberOfCells() == 0) continue;
Object[] cells = new Object[row.getLastCellNum()];
for (int c = row.getFirstCellNum(); c <= row.getLastCellNum(); c++) {
Cell cell = row.getCell(c);
if (cell == null) continue;
cells[c] = getValueFromCell(cell);
}
list.add(cells);
}
return list;
}
public static final Object getValueFromCell(Cell cell) {
if (cell == null) {
return null;
}
Object result = null;
if (cell instanceof HSSFCell) {
if (cell != null) {
int cellType = ((HSSFCell) cell).getCellType();
switch (cellType) {
case HSSFCell.CELL_TYPE_STRING:
result = ((HSSFCell) cell).getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
DecimalFormat df = new DecimalFormat("###.####");
result = df.format(((HSSFCell) cell).getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
result = ((HSSFCell) cell).getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
result = ((HSSFCell) cell).getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
result = null;
break;
case HSSFCell.CELL_TYPE_ERROR:
result = null;
break;
default:
System.out.println("枚举了所有类型");
break;
}
}
} else if (cell instanceof XSSFCell) {
if (cell != null) {
int cellType = ((XSSFCell) cell).getCellType();
switch (cellType) {
case XSSFCell.CELL_TYPE_STRING:
result = ((XSSFCell) cell).getRichStringCellValue().getString();
break;
case XSSFCell.CELL_TYPE_NUMERIC:
DecimalFormat df = new DecimalFormat("###.####");
result = df.format(((XSSFCell) cell).getNumericCellValue());
break;
case XSSFCell.CELL_TYPE_FORMULA:
result = ((XSSFCell) cell).getNumericCellValue();
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
result = ((XSSFCell) cell).getBooleanCellValue();
break;
case XSSFCell.CELL_TYPE_BLANK:
result = null;
break;
case XSSFCell.CELL_TYPE_ERROR:
result = null;
break;
default:
break;
}
}
}
return result;
}
public static final List<Object[]> importExcelBySheetIndex(String filePath, int sheetIndex
, int startRow, int startLine) throws Exception {
List<Object[]> resultList = null;
Workbook wb = createWorkbook(filePath);
Sheet sheet = ImportExcelUtils.getSheet(wb, sheetIndex);
if (sheet != null) {
List<Object[]> list = ImportExcelUtils.listFromSheet(sheet);
if (list != null && list.size() > 0) {
resultList = new ArrayList<Object[]>();
if (startLine <= list.size()) {
for (int i = startLine; i < list.size(); i++) {
int nullCount = 0;
Object[] rows = list.get(i);
if (rows != null && rows.length > 0) {
List<Object> resultObjects = new ArrayList<Object>();
for (int n = startRow; n < rows.length; n++) {
if (Assert.objIsEmpty(rows[n])) {
nullCount++;
}
resultObjects.add(rows[n]);
}
if (nullCount >= rows.length) {
break;
} else {
resultList.add(resultObjects.toArray());
}
}
}
}
}
}
return resultList;
}
}