import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Excel工具类
*/
public class ExcelUtil {
private static Logger logger = LoggerFactory.getLogger(ExcelUtil.class);
/**
* 创建 Workbook(Excel 2003/2007/2010 都可以处理)
*
* @param inputStream
* @return
*/
public static Workbook createWorkbook(InputStream inputStream) {
Workbook workbook = null;
try {
workbook = WorkbookFactory.create(inputStream);
return workbook;
} catch (IOException e) {
logger.error("createWorkbook Exception:" + e.getMessage(), e);
} catch (InvalidFormatException e) {
logger.error("createWorkbook Exception:" + e.getMessage(), e);
}
return workbook;
}
/**
* 获取sheet的总数量
*
* @param workbook
* @return
*/
public static int getSheetCount(Workbook workbook) {
return workbook.getNumberOfSheets();
}
/**
* 获取第几个Sheet
*
* @param workbook
* @param index
* @return
*/
public static Sheet getSheet(Workbook workbook, int index) {
return workbook.getSheetAt(index);
}
/**
* 获取总行数(包含空行隔行,下标从0开始)
* <p>
* 还有个一获取总行数的方法 sheet.getPhysicalNumberOfRows()
* (获取物理总行数,不包含空行,隔行,不推荐使用)
*
* @param sheet
* @return
*/
public static int getRowCount(Sheet sheet) {
return sheet.getLastRowNum();
}
/**
* 判断 excel 文件后缀是否合法
*
* @param suffix
* @return
*/
public static boolean checkExcelSuffix(String suffix) {
if (!suffix.equals("xls") && !suffix.equals("xlsx") && !suffix.equals("csv")) {
return false;
}
return true;
}
/**
* 截取文件后缀
* 如:
* .txt 返回 txt
* .xls 返回 xls
*
* @param str
* @return
*/
public static String subStringExcel(String str) {
return str.substring(str.lastIndexOf(".") + 1);
}
/**
* 关闭输入流
*
* @param inputStream
*/
public static void closeInputStream(InputStream inputStream) {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
logger.error("closing inputStream Exception:" + e.getMessage(), e);
}
}
}
/**
* 关闭输出流
*
* @param outputStream
*/
public static void closeOutputStream(OutputStream outputStream) {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
logger.error("closing outputStream Exception: " + e.getMessage(), e);
}
}
}
/**
* 获取当前 sheet 中所有合并单元格
*
* @param sheet
* @return
*/
public static List<CellRangeAddress> getAllCellRangeAddress(Sheet sheet) {
List<CellRangeAddress> list = new ArrayList<CellRangeAddress>();
//获得一个sheet中合并单元格的数量
int sheetmergerCount = sheet.getNumMergedRegions();
for (int i = 0; i < sheetmergerCount; i++) {
CellRangeAddress ca = sheet.getMergedRegion(i);
list.add(ca);
}
return list;
}
/**
* 是否为合并单元格
*
* @param list 所有合并单元格
* @param sheet sheet
* @param row 第几行
* @param column 第几列
* @return
*/
public static boolean isMergedRegion(List<CellRangeAddress> list, Sheet sheet, int row, int column) {
logger.info("row:" + row + ",column:" + column);
if (list == null || list.size() == 0) {
return false;
}
logger.info("list:" + list.size());
for (int i = 0; i < list.size(); i++) {
CellRangeAddress range = sheet.getMergedRegion(i);
int firstColumn = range.getFirstColumn();
int lastColumn = range.getLastColumn();
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
logger.info("firstColumn:" + firstColumn + ",lastColumn:" + lastColumn + ",firstRow:" + firstRow + ",lastRow:" + lastRow);
if (row >= firstRow && row <= lastRow) {
if (column >= firstColumn && column <= lastColumn) {
return true;
}
}
}
return false;
}
/**
* 获取合并单元格中的值
*
* @param listCombineCell 所有合并单元格的集合
* @param cell cell
* @param sheet sheet
* @return
* @throws Exception
*/
public String getMergedRegionValue(List<CellRangeAddress> listCombineCell, Cell cell, Sheet sheet) throws Exception {
int firstC = 0;
int lastC = 0;
int firstR = 0;
int lastR = 0;
String cellValue = null;
for (CellRangeAddress ca : listCombineCell) {
//获得合并单元格的起始行, 结束行, 起始列, 结束列
firstC = ca.getFirstColumn();
lastC = ca.getLastColumn();
firstR = ca.getFirstRow();
lastR = ca.getLastRow();
if (cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR) {
if (cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC) {
Row fRow = sheet.getRow(firstR);
Cell fCell = fRow.getCell(firstC);
cellValue = getCellValue(fCell);
break;
}
} else {
cellValue = "";
}
}
return cellValue;
}
/**
* 获取excel数据
*
* @param cell cell
* @return
*/
public static String getCellValue(Cell cell) {
if (cell == null) {
return null;
}
cell.setCellType(Cell.CELL_TYPE_STRING);
return cell.getStringCellValue();
}
/**
* 获取指定行,指定列的值
*
* @param sheet sheet
* @param row 指定行
* @param column 指定列
* @return
*/
public static String getAppointCell(Sheet sheet, int row, int column) {
if (sheetIsNull(sheet)) {
return null;
}
Row r = sheet.getRow(row);
if (rowIsNull(r)) {
return null;
}
Cell cell = r.getCell(column);
if (!cellIsNull(cell)) {
return getCellValue(cell);
}
return null;
}
/**
* 判断 sheet 是否为空
*
* @param sheet
* @return
*/
public static boolean sheetIsNull(Sheet sheet) {
if (sheet == null) {
return true;
}
return false;
}
/**
* 判断 row 是否为空
*
* @param row
* @return
*/
public static boolean rowIsNull(Row row) {
if (row == null) {
return true;
}
return false;
}
/**
* 判断 cell 是否为空
*
* @param cell
* @return
*/
public static boolean cellIsNull(Cell cell) {
if (cell == null) {
return true;
}
return false;
}
/**
* 判断字符串是否为空
*
* @param str
* @return
*/
public static boolean stringIsNull(String str) {
return (str == null || "".equals(str));
}
/**
* excel 里面判断是否为时间类型
* <p>
* 返回 true 是时间类型
* 返回 false 不是时间类型
*
* @param str
* @return
*/
public static boolean checkExcelDateType(String str) {
if (stringIsNull(str)) {
return false;
}
try {
double num = Double.parseDouble(str);
Date date = DateUtil.getJavaDate(num);
if (date != null) {
return true;
}
} catch (Exception e) {
logger.info("conversion time failure Exception:" + e.getMessage());
return false;
}
return false;
}
/**
* 返回时间
*
* @param str
* @return
*/
public static Date getExcelDate(String str) {
return DateUtil.getJavaDate(Double.parseDouble(str));
}
}