123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package cn.efunbox.base.util;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.CellType;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.IOException;
- import java.util.Objects;
- /**
- * ExcelUtil
- * Created by xusq on 2020/9/14.
- */
- @Slf4j
- public class ExcelUtil {
- private final static String EXCEL_2003 = ".xls"; //2003- 版本的excel
- private final static String EXCEL_2007 = ".xlsx"; //2007+ 版本的excel
- public static Workbook getWorkbook(MultipartFile file) {
- Workbook wb = null;
- String fileName = file.getOriginalFilename();
- String fileType = fileName.substring(fileName.lastIndexOf("."));
- try {
- if (EXCEL_2003.equals(fileType)) {
- wb = new HSSFWorkbook(file.getInputStream()); //2003-
- } else if (EXCEL_2007.equals(fileType)) {
- wb = new XSSFWorkbook(file.getInputStream()); //2007+
- }
- } catch (IOException e) {
- log.error("find input stream is error , {}",e.getMessage(),e);
- }
- return wb;
- }
- /**
- * 单表值获取
- *
- * @param cell 获取
- * @return 类型
- */
- public static String getCell(Cell cell) {
- if (Objects.nonNull(cell)) {
- CellType cellType = cell.getCellTypeEnum();
- if (cellType == CellType.STRING) {//字符串类型
- //System.out.println(cell.getStringCellValue());
- return cell.getStringCellValue().trim();
- }
- if (cellType == CellType.NUMERIC) {//数字类型
- long numericCellValue = (long) cell.getNumericCellValue();
- //System.out.println(numericCellValue);
- return numericCellValue + "";
- }
- }
- return null;
- }
- }
|