“Can‘t open workbook - unsupported file type: XML“

java开发,增删改查,涉及到导入excel时,有的excel导入失败提示"Can't open workbook - unsupported file type: XML"。着急赶工期,告诉客户先把excel另存为xls格式,再重新导入。现在有点空余时间,好好研究了下,解决了这个问题。直接上操作啊.

import com.spire.xls.FileFormat;
import org.apache.poi.EmptyFileException;
import org.apache.poi.poifs.filesystem.FileMagic;
//获取文件输入流
InputStream checkis = FileMagic.prepareToCheckMagic(file.getInputStream());
byte[] emptyFileCheck = new byte[1];
checkis.mark(emptyFileCheck.length);
if (checkis.read(emptyFileCheck) < emptyFileCheck.length) {
    throw new EmptyFileException();
} else {
    checkis.reset();
    FileMagic fm = FileMagic.valueOf(checkis);
   //开始判断文件是不是xml格式
    if (FileMagic.XML == fm) {
        com.spire.xls.Workbook workbook=new com.spire.xls.Workbook();
        workbook.loadFromXml(file.getInputStream());
        workbook.saveToFile(fileName, FileFormat.Version2013);
       //接下来开始处理导入excel文件
        ****
    }
}

参考了 poi读取excel报错Your InputStream was neither an OLE2 stream, nor an OOXML stream-CSDN博客

你可能感兴趣的:(maven,java)