一、从excel里导入数据,首先是判断文件是否是一个excel文件
import java.io.InputStream; import java.text.DecimalFormat; import java.util.Objects; import org.apache.poi.poifs.filesystem.FileMagic; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.springframework.web.multipart.MultipartFile; public static boolean isExcelFile(InputStream inputStream) { boolean result = false; try { FileMagic fileMagic = FileMagic.valueOf(inputStream); if (Objects.equals(fileMagic, FileMagic.OLE2) || Objects.equals(fileMagic, FileMagic.OOXML)) { result = true; } } catch (Exception e) { e.printStackTrace(); } return result; }
pom poi 至少是3。17
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency>
参考:https://blog.csdn.net/luoyanjiewade/article/details/85268966