org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException Java Examples

The following examples show how to use org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReaderUtil.java    From zerocell with Apache License 2.0 6 votes vote down vote up
/**
 * Reads a list of POJOs from the given excel file.
 *
 * @param path Excel file to read from
 * @param sheetName The sheet to extract from in the workbook
 * @param reader The reader class to use to load the file from the sheet
 */
public static void process(String path, String sheetName, ZeroCellReader reader) {
    if (path == null || path.trim().isEmpty()) {
        throw new IllegalArgumentException("'path' must be given");
    }

    File file = new File(path);
    if (file.exists() && file.isDirectory()) {
        throw new IllegalArgumentException("path must not be a directory");
    }
    try (OPCPackage opcPackage = OPCPackage.open(file.getAbsolutePath(), PackageAccess.READ)) {
        process(opcPackage, sheetName, reader);
    } catch(InvalidFormatException | EmptyFileException | NotOfficeXmlFileException ife) {
        throw new ZeroCellException(ERROR_NOT_OPENXML);
    } catch (IOException ioe) {
        throw new ZeroCellException("Failed to process file", ioe);
    }
}
 
Example #2
Source File: ReaderUtil.java    From zerocell with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a list of POJOs from the given excel file.
 *
 * @param file Excel file to read from
 * @param sheetName The sheet to extract from in the workbook
 * @param reader The reader class to use to load the file from the sheet
 */
public static void process(File file, String sheetName, ZeroCellReader reader) {
    try (OPCPackage opcPackage = OPCPackage.open(file, PackageAccess.READ)) {
        process(opcPackage, sheetName, reader);
    } catch(InvalidFormatException | EmptyFileException | NotOfficeXmlFileException ife) {
        throw new ZeroCellException(ERROR_NOT_OPENXML);
    } catch (IOException ioe) {
        throw new ZeroCellException("Failed to process file", ioe);
    }
}
 
Example #3
Source File: ExcelPerfModeReader.java    From azeroth with Apache License 2.0 5 votes vote down vote up
private List<String> readAsXLS(String path) {
    try {
        XLS2CSV xls2csv = new XLS2CSV(path, -1);
        return xls2csv.process();
    } catch (Exception e) {
        if (e instanceof NotOLE2FileException || e instanceof NotOfficeXmlFileException || e instanceof OfficeXmlFileException) {
            throw new ExcelOperBaseException("请选择正确格式excel文件");
        }
        if (e instanceof IOException) {
            throw new ExcelOperBaseException("文件读取失败");
        }
        throw new RuntimeException(e);
    }
}
 
Example #4
Source File: ExcelPerfModeReader.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
private List<String> readAsXLS(String path){
	try {				
		XLS2CSV xls2csv = new XLS2CSV(path, -1);
		return xls2csv.process();
	} catch (Exception e) {
		if(e instanceof NotOLE2FileException || e instanceof NotOfficeXmlFileException || e instanceof OfficeXmlFileException){
			throw new ExcelOperBaseException("请选择正确格式excel文件");
		}
		if(e instanceof IOException){
			throw new ExcelOperBaseException("文件读取失败");
		}
		throw new RuntimeException(e);
	}
}
 
Example #5
Source File: ExcelReaderService.java    From abixen-platform with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Workbook openFileAsWorkBook(final MultipartFile file, final FileParserMessage msg) throws IOException {
    try {
        final String fileName = file.getOriginalFilename();
        if (XLSX_FILE_EXTENSION.equals(fileName.substring(fileName.lastIndexOf(".")))) {
            return new XSSFWorkbook(file.getInputStream());
        }
        if (XLS_FILE_EXTENSION.equals(fileName.substring(fileName.lastIndexOf(".")))) {
            return new HSSFWorkbook(file.getInputStream());
        }
        throw new NotOfficeXmlFileException("Not recognized type");
    } catch (NotOfficeXmlFileException e) {
        msg.addFileParseError(new FileParseError(0, "Invalid file type"));
        throw new IOException();
    }
}
 
Example #6
Source File: ReaderUtil.java    From zerocell with Apache License 2.0 4 votes vote down vote up
/**
 * Processes data from an Excel file contained in the OPCPackage using the
 * reader implementation
 * <p>
 * Please note that the process will read data from the first sheet in the
 * File when if sheet name is not specified
 * (i.e. the sheet name defaults to the {@link EntityHandler.DEFAULT_SHEET})
 * </p>
 * @param opcPackage the OpenXML OPC Package
 * @param sheetName The sheet name
 * @param reader the reader implementation that handles the entity mapping
 */
private static void process(OPCPackage opcPackage, String sheetName, ZeroCellReader reader) {
    try {
        DataFormatter dataFormatter = new DataFormatter();
        ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(opcPackage);
        XSSFReader xssfReader = new XSSFReader(opcPackage);
        StylesTable stylesTable = xssfReader.getStylesTable();
        InputStream sheetInputStream = null;
        XSSFReader.SheetIterator sheets = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
        while (sheets.hasNext()) {
            sheetInputStream = sheets.next();

            if (EntityHandler.DEFAULT_SHEET.equalsIgnoreCase(sheetName)) {
                break;
            }
            if (sheets.getSheetName().equalsIgnoreCase(sheetName)) {
                break;
            } else {
                sheetInputStream = null;
            }
        }

        if (Objects.isNull(sheetInputStream)) {
            throw new SheetNotFoundException(sheetName);
        }

        XMLReader xmlReader = SAXHelper.newXMLReader();
        xmlReader.setContentHandler(new XSSFSheetXMLHandler(stylesTable, strings, reader, dataFormatter, false));
        xmlReader.parse(new InputSource(sheetInputStream));
        sheetInputStream.close();
        xmlReader = null;
        sheetInputStream = null;
        stylesTable = null;
        strings = null;
        xssfReader = null;
    } catch(InvalidFormatException | EmptyFileException | NotOfficeXmlFileException ife) {
        throw new ZeroCellException(ERROR_NOT_OPENXML);
    } catch(SheetNotFoundException ex) {
        throw new ZeroCellException(ex.getMessage());
    } catch (ZeroCellException ze) {
        throw ze; // Rethrow the Exception
    } catch (Exception e) {
        throw new ZeroCellException("Failed to process file", e);
    }
}