要搞清楚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,接下來我們就來看看具體的判斷邏輯吧。
第一步:獲取文件的FileMagic值
FileMagic類提供了2種方式來獲取FileMagic值:
1,從字節流中獲取FileMagic值
// 字節流判斷邏輯
public static FileMagic valueOf(byte[] magic) {
for (FileMagic fm : values()) {
int i=0;
boolean found = true;
for (byte[] ma : fm.magic) {
for (byte m : ma) {
byte d = magic[i++];
// 看字節流中是否包含指定的二進制數(暗號)
if (!(d == m || (m == 0x70 && (d == 0x10 || d == 0x20 || d == 0x40)))) {
found = false;
break;
}
}
// 如果包含指定的二進制數(暗號),則返回
if (found) {
return fm;
}
}
}
// 如果不包含指定的二進制數(暗號),則返回UNKNOWN,說明這個文件無法獲取到FileMagic
return UNKNOWN;
}
2,從輸入流中獲取FileMagic值
/**
* 從文件輸入流中獲取FileMagic
*
* Get the file magic of the supplied InputStream (which MUST
* support mark and reset).<p>
*
* If unsure if your InputStream does support mark / reset,
* use {@link #prepareToCheckMagic(InputStream)} to wrap it and make
* sure to always use that, and not the original!<p>
*
* Even if this method returns {@link FileMagic#UNKNOWN} it could potentially mean,
* that the ZIP stream has leading junk bytes
*
* @param inp An InputStream which supports either mark/reset
*/
public static FileMagic valueOf(InputStream inp) throws IOException {
if (!inp.markSupported()) {
throw new IOException("getFileMagic() only operates on streams which support mark(int)");
}
// Grab the first 8 bytes 抓取文件流的前8個字節
byte[] data = IOUtils.peekFirst8Bytes(inp);
return FileMagic.valueOf(data);
}
好了,這是第一步,我們拿到了FileMagic的值。接下來看如果根據FileMagic的值,判斷excel文件的格式。
第二步:根據根據FileMagic的值,判斷excel文件的格式
注意:即使用戶指定了excelType,但是poi實際上可能並不會信任。poi判斷文件格式的邏輯就是FileMagic。
public static ExcelTypeEnum valueOf(File file, InputStream inputStream, ExcelTypeEnum excelType) {
try {
// 根據excel文件的魔法二進制數值,判斷excel文件格式。
// 注意:即使用戶指定了excelType,但是poi實際上可能並不會相信。poi判斷文件格式的邏輯就是FileMagic
FileMagic fileMagic;
// 1,優先使用BufferedInputStream流來獲取FileMagic
if (file != null) {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
try {
fileMagic = FileMagic.valueOf(bufferedInputStream);
} finally {
bufferedInputStream.close();
}
// 如果FileMagic的值,既不是OLE2,也不是OOXML,則使用文件後綴名來判斷文件格式
if (!FileMagic.OLE2.equals(fileMagic) && !FileMagic.OOXML.equals(fileMagic)) {
String fileName = file.getName();
if (fileName.endsWith(XLSX.getValue())) {
return XLSX;
} else if (fileName.endsWith(XLS.getValue())) {
return XLS;
} else {
throw new ExcelCommonException("Unknown excel type.");
}
}
// 2,如果沒有傳入File對象,則使用InputStream流來獲取FileMagic
} else {
fileMagic = FileMagic.valueOf(inputStream);
}
// 如果FileMagic的值是OLE2,則說明這個文件是XLS格式
if (FileMagic.OLE2.equals(fileMagic)) {
return XLS;
}
// 如果FileMagic的值是OOXML,則說明這個文件是XLSX格式
if (FileMagic.OOXML.equals(fileMagic)) {
return XLSX;
}
} catch (IOException e) {
if (excelType != null) {
return excelType;
}
throw new ExcelCommonException(
"Convert excel format exception.You can try specifying the 'excelType' yourself", e);
}
// 3,如果根據FileMagic沒有判斷出來,沒辦法,這時候就選擇相信用戶,直接返回用戶指定的excelType
if (excelType != null) {
return excelType;
}
throw new ExcelCommonException(
"Convert excel format exception.You can try specifying the 'excelType' yourself");
}
通過這個源碼,我們也可以發現,poi其實只能處理xls和xlsx格式的excel,其他格式的excel一律無法處理。
這個其實也是微軟的產品讓人詬病的一個原因,雖然提供了很多炫酷的可視化操作,但是由於太過繁雜,應用程序解析起來
顯得很吃力。
我們來看看excel的格式有多少種?
確實很豐富,但是有時候,複雜反而不利於一統江湖。簡與繁,各有利弊。這裏就不發感慨了。OK ,今天就分享到這裏。下次見!
最近在研究阿里的Easyexcel,發現在判斷excel文件格式上,阿里複用的就是poi的邏輯,通過FileMagic來判斷的。