All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.hutool.poi.excel.ExcelFileUtil Maven / Gradle / Ivy

There is a newer version: 5.8.34
Show newest version
package cn.hutool.poi.excel;

import cn.hutool.core.io.IORuntimeException;
import org.apache.poi.poifs.filesystem.FileMagic;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Excel文件工具类
 * 
 * @author looly
 * @since 4.2.1
 */
public class ExcelFileUtil {
	// ------------------------------------------------------------------------------------------------ isXls
	/**
	 * 是否为XLS格式的Excel文件(HSSF)
* XLS文件主要用于Excel 97~2003创建 * * @param in excel输入流 * @return 是否为XLS格式的Excel文件(HSSF) */ public static boolean isXls(InputStream in) { /* * {@link java.io.PushbackInputStream} * PushbackInputStream的markSupported()为false,并不支持mark和reset * 如果强转成PushbackInputStream在调用FileMagic.valueOf(inputStream)时会报错 * {@link FileMagic} * 报错内容:getFileMagic() only operates on streams which support mark(int) * 此处修改成 final InputStream inputStream = FileMagic.prepareToCheckMagic(in) * @author kefan.qu */ final InputStream inputStream = FileMagic.prepareToCheckMagic(in); try { return FileMagic.valueOf(inputStream) == FileMagic.OLE2; } catch (IOException e) { throw new IORuntimeException(e); } } /** * 是否为XLSX格式的Excel文件(XSSF)
* XLSX文件主要用于Excel 2007+创建 * * @param in excel输入流 * @return 是否为XLSX格式的Excel文件(XSSF) */ public static boolean isXlsx(InputStream in) { if (false == in.markSupported()) { in = new BufferedInputStream(in); } try { return FileMagic.valueOf(in) == FileMagic.OOXML; } catch (IOException e) { throw new IORuntimeException(e); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy