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

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

问题描述

有没有办法来确定的Apache POI微软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).

我想我可以做这样的事情:

I suppose I could do something like this:

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...

如果你打算做的文件有些特别,那么<一个href=\"http://stackoverflow.com/questions/14522441/determine-ms-excel-file-type-with-apache-poi/14522512#14522512\">rjokelai's答案是做到这一点的方式。

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

不过,如果你只是将要使用HSSF / XSSF /通用SS的usermodel,那么它要简单得多具有POI为你做,并使用<一个href=\"http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/WorkbookFactory.html\">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提卡以做检测 - 它可以检测巨大为你的不同文件格式的数字。

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天全站免登陆