PIO opera Excel y juzga la versión de Excel a través del flujo de archivos

import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;

/**
 * @program: Ecology
 * @description: this is a class
 * @author: Mr.zeng
 * @create: 2021-03-12 11:34
 **/

public class XlsImpUtil {
    
    
    public static Workbook create(InputStream inp) throws IOException, InvalidFormatException, org.apache.poi.openxml4j.exceptions.InvalidFormatException {
    
    
        if (!inp.markSupported()) {
    
    
            inp = new PushbackInputStream(inp, 8);
        }

        /*POI操作Excel文件,通过文件流判断Excel的版本*/
        //2003及以下
       if (POIFSFileSystem.hasPOIFSHeader(inp)) {
    
    
            return new HSSFWorkbook(inp);
        }
        //2007及以上
        if (POIXMLDocument.hasOOXMLHeader(inp)) {
    
    
            return new XSSFWorkbook(OPCPackage.open(inp));
        }
    
   throw new IllegalArgumentException("你的excel版本目前poi解析不了");
    }
}

Supongo que te gusta

Origin blog.csdn.net/weixin_42292697/article/details/114916697
Recomendado
Clasificación