实例化时异常: EOF in header
错误代码:
try { workBook = new HSSFWorkbook(file); } catch { try { workBook = new XSSFWorkbook(file); } catch { throw new Exception("文件格式不被支持!"); } }
当用HSSFWorkbook 实例化失败后,再尝试用XSSFWorkbook实例。如果是 xlsx 格式,就会报 EOF in header。
正确做法,如果是 xlsx 格式,直接用XSSFWorkbook实例。
IWorkbook workBook = null; if (contentType == "application/vnd.ms-excel") { workBook = new HSSFWorkbook(file); } else if (contentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { workBook = new XSSFWorkbook(file); }
--