package com.minerva.stresstest.common.util;
import com.minerva.stresstest.objs.dataobject.HumitureDO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.StringUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
public class ParserExcelUtil {
private Workbook workbook;
public ParserExcelUtil() {
}
public ParserExcelUtil(File excelFile) throws IOException {
try {
this.workbook = new XSSFWorkbook(new FileInputStream(excelFile));
} catch (Exception var3) {
this.workbook = new HSSFWorkbook(new FileInputStream(excelFile));
}
}
public ParserExcelUtil(InputStream in) throws IOException {
try {
this.workbook = new XSSFWorkbook(in);
} catch (Exception var3) {
this.workbook = new HSSFWorkbook(in);
}
}
public static List<List<CellValue>> listCellValueList(File file) {
try (InputStream is = FileUtils.openInputStream(file)){
return listCellValueList(file.getName(), is, 0, 1);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static List<List<CellValue>> listCellValueList(File file, Integer sheetNumber, Integer skipRows) {
try (InputStream is = FileUtils.openInputStream(file)){
return listCellValueList(file.getName(), is, sheetNumber, skipRows);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static List<List<CellValue>> listCellValueList(String fileName, InputStream is) throws IOException {
return listCellValueList(fileName, is, 0, 1);
}
public static List<List<CellValue>> listCellValueList(String fileName, InputStream is, Integer sheetNumber, Integer skipRows) throws IOException {
try {
List<List<ParserExcelUtil.CellValue>> rowDatas;
if ("xls".equals(fileName.substring(fileName.lastIndexOf(".") + 1))) {
ParserExcelUtil parserExcelUtil = new ParserExcelUtil();
parserExcelUtil.excel2003SheetParser(is);
rowDatas = parserExcelUtil.getDatasInSheet2CellValue(sheetNumber, skipRows);
} else {
rowDatas = new ParserExcelUtil(is).getDatasInSheet2CellValue(sheetNumber, skipRows);
}
return rowDatas;
} catch (Exception e) {
throw new IOException("解析excel失败");
}
}
public void excel2003SheetParser(InputStream in) throws IOException {
try {
workbook = new HSSFWorkbook(in);
}catch (Exception e){
workbook = new XSSFWorkbook(in);
}
}
public void excel2007SheetParser(InputStream in) throws IOException {
workbook = new XSSFWorkbook(in);
}
public List<List<CellValue>> getDatasInSheet2CellValue(int sheetNumber, int skipRows) {
List<List<CellValue>> result = new ArrayList<List<CellValue>>();
Sheet sheet = workbook.getSheetAt(sheetNumber);
int rowCount = sheet.getLastRowNum();
CellReference cellReference = new CellReference("A4");
boolean flag = false;
for (int i = cellReference.getRow(); i <= sheet.getLastRowNum();) {
Row r = sheet.getRow(i);
if(r == null){
sheet.shiftRows(i+1, sheet.getLastRowNum(),-1);
continue;
}
flag = false;
for(Cell c : r){
if(c.getCellType() != Cell.CELL_TYPE_BLANK){
flag = true;
break;
}
}
if(flag){
i++;
continue;
}else{
if(i == sheet.getLastRowNum()){
sheet.removeRow(r);
}else{
sheet.shiftRows(i+1, sheet.getLastRowNum(),-1);
}
}
}
System.out.println("有效行数:"+(sheet.getLastRowNum()));
rowCount=sheet.getLastRowNum();
int rowIndex = skipRows == 0 ? sheet.getFirstRowNum() : skipRows;
if (rowCount < 1) {
return result;
}
for (; rowIndex <= rowCount; rowIndex++) {
Row row = sheet.getRow(rowIndex);
if (row != null) {
List<CellValue> rowData = new ArrayList<CellValue>();
int columnCount = row.getLastCellNum();
int columnIndex = row.getFirstCellNum();
for (; columnIndex < columnCount; columnIndex++) {
Cell cell = row.getCell(columnIndex);
if (cell!=null){
cell.setCellType(Cell.CELL_TYPE_STRING);
}
Object cellStr = this.getCellString(cell);
if(cell!=null&&!"".equals(cell)&&!"".equals(cell.getStringCellValue())){
CellValue cellvalue = new CellValue(columnIndex, cellStr);
rowData.add(cellvalue);
}
}
result.add(rowData);
}
}
return result;
}
private Object getCellString(Cell cell) {
Object result = null;
if (null == cell) {
return null;
}
if (null != cell) {
int cellType = cell.getCellType();
switch (cellType) {
case Cell.CELL_TYPE_STRING:
result = cell.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
result = getNumbericValue2String(cell);
break;
case Cell.CELL_TYPE_FORMULA:
result = getNumbericValue2String(cell);
break;
case Cell.CELL_TYPE_ERROR:
result = "";
break;
case Cell.CELL_TYPE_BOOLEAN:
result = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_BLANK:
result = "";
break;
default:
result = "";
}
}
return result;
}
private String getNumbericValue2String(Cell cell){
if(HSSFDateUtil.isCellDateFormatted(cell)){
Date d = cell.getDateCellValue();
DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = formater.format(d);
return format;
}else{
DecimalFormat df = new DecimalFormat("0.00");
String whatYourWant = df.format(cell.getNumericCellValue());
return whatYourWant;
}
}
public String getSheetName(Integer index)throws IOException {
String sheetName = workbook.getSheetAt(index).getSheetName();
return sheetName;
}
public class CellValue{
private int index;
private Object value;
private CellValue() {
super();
}
private CellValue(int index, Object value) {
super();
this.index = index;
this.value = value;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
}