使用 Apache POI 确定 MS Excel 文件类型 [英] Determine MS Excel file type with Apache POI

查看:31
本文介绍了使用 Apache POI 确定 MS Excel 文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 Apache POI 中确定 MS Office Excel 文件类型?我需要知道 Excel 文件的格式是什么:Excel '97(-2007) (.xls) 或 Excel 2007 OOXML (.xlsx).

Is there a way to determine MS Office Excel file type in Apache POI? I need to know in what format is the Excel file: in Excel '97(-2007) (.xls) or Excel 2007 OOXML (.xlsx).

我想我可以这样做:

int type = PoiTypeHelper.getType(file);
switch (type) {
case PoiType.EXCEL_1997_2007:
   ...
   break;
case PoiType.EXCEL_2007:
   ...
   break;
default:
   ...
}

谢谢.

推荐答案

提升对答案的评论...

Promoting a comment to an answer...

如果你要对文件做一些特别的事情,那么 rjokelai 的答案 就是这样做的方法.

If you're going to be doing something special with the files, then rjokelai's answer is the way to do it.

但是,如果您只是打算使用 HSSF/XSSF/Common SS 用户模型,那么让 POI 为您完成它并使用 WorkbookFactory 为您检测并打开类型.你会做这样的事情:

However, if you're just going to be using the HSSF / XSSF / Common SS usermodel, then it's much simpler to have POI do it for you, and use WorkbookFactory to have the type detected and opened for you. You'd do something like:

 Workbook wb = WorkbookFactory.create(new File("something.xls"));

 Workbook wb = WorkbookFactory.create(request.getInputStream());

然后,如果您需要做一些特殊的事情,请测试它是 HSSFWorkbook 还是 XSSFWorkbook.打开文件时,尽可能使用文件而不是 InputStream加快速度并节省内存.

Then if you needed to do something special, test if it's a HSSFWorkbook or XSSFWorkbook. When opening the file, use a File rather than an InputStream if possible to speed things up and save memory.

如果您根本不知道您的文件是什么,请使用 Apache Tika 进行检测 -它可以为您检测大量不同的文件格式.

If you don't know what your file is at all, use Apache Tika to do the detection - it can detect a huge number of different file formats for you.

这篇关于使用 Apache POI 确定 MS Excel 文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆