Differentiate excel file types based on bytes, not extension
I currently have a method that receives a byte array of an excel file. It worked great, and I had it differentiating the file types (.xls
,开发者_JAVA百科 and .xlsx
) based on the first two bytes in the file. If it's an .xlsx
it started with a PK. Unfortunately, now I am getting .xlsm
files sent, and I don't know what byte differs to tell them apart. I don't have the filename at this point to get the extension.
The only difference between .xlsx and .xlsm is that the latter may include Macros. I'm quite sure you need further parsing of the file to find that out.
Note that the magic number "PK" (actually it is PK\x03\x04) in the beginning of the .xlsx refers to ZIP compressed data. To further parse the file, you first have to decode (uncompress) it according to ZIP file format.
精彩评论