要搞清楚POI如何判断excel文件格式,我们要先搞清楚FileMagic。
FileMagic是什么呢?看官方解释:
The file magic number, i.e. the file identification based on the first bytes of the file
文件魔法数字,即基于文件第一个字节的文件标识。说白了,excel文件的格式,是由文件的第一个字节的值来决定的。当然,这个字节我们看不到,但是应用程序API能“看到”。
这个问题,我们在平常使用过程中,经常遇到。明明我的excel文件是.xls结尾的,为什么poi提示我:Convert excel format exception.You can try specifying the 'excelType' yourself
原因是:poi是根据FileMagic来判断excel格式,而不是根据后缀名来判断excel格式。
OK,接下来我们就来看看具体的判断逻辑吧。