本文整理汇总了Java中org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException类的典型用法代码示例。如果您正苦于以下问题:Java NotOfficeXmlFileException类的具体用法?Java NotOfficeXmlFileException怎么用?Java NotOfficeXmlFileException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NotOfficeXmlFileException类属于org.apache.poi.openxml4j.exceptions包,在下文中一共展示了NotOfficeXmlFileException类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。
示例1: extractFromXLSX
import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException; //导入依赖的package包/类
/**
Uses Apache POI to extract information from xlsx file into a 2D array.
Here is where we got our starter code: http://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/
Look at example number 4, Apache POI library – Reading an Excel file.
This file originally just printed data, that is why there are several commented out lines in the code.
We have repurposed this method to put all data into a 2D String array and return it.
*/
public String[][] extractFromXLSX(InputStream excelFile) throws NotOfficeXmlFileException {
try {
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
String[][] cellValues = new String[datatypeSheet.getLastRowNum() + 1]
[max(max(datatypeSheet.getRow(1).getLastCellNum(), datatypeSheet.getRow(2).getLastCellNum()),
datatypeSheet.getRow(3).getLastCellNum())];
for (Row currentRow : datatypeSheet) {
//cellValues[currentRow.getRowNum()] = new String[currentRow.getLastCellNum()];
for (Cell currentCell : currentRow) {
//getCellTypeEnum shown as deprecated for version 3.15
//getCellTypeEnum ill be renamed to getCellType starting from version 4.0
if (currentCell.getCellTypeEnum() == CellType.STRING) {
cellValues[currentCell.getRowIndex()][currentCell.getColumnIndex()] = currentCell.getStringCellValue();
} else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
cellValues[currentCell.getRowIndex()][currentCell.getColumnIndex()] =
Integer.toString((int) Math.round(currentCell.getNumericCellValue()));
}
}
}
return cellValues;
} catch (IOException e) {
System.out.println("EVERYTHING BLEW UP STOP STOP STOP");
e.printStackTrace();
return null; //TODO: this should not return null. This should continue.
}
}
开发者ID:UMM-CSci-3601-S17,项目名称:digital-display-garden-iteration-4-revolverenguardia-1,代码行数:43,代码来源:ExcelParser.java
示例2: readAsXLS
import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException; //导入依赖的package包/类
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);
}
}
开发者ID:warlock-china,项目名称:azeroth,代码行数:15,代码来源:ExcelPerfModeReader.java
示例3: testEmptyDoc
import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException; //导入依赖的package包/类
@Test(expected = NotOfficeXmlFileException.class)
public void testEmptyDoc() throws InvalidFormatException, IOException {
final File file = new File("resources/document/empty/empty-template.docx");
assertTrue(file.exists());
try (FileInputStream is = new FileInputStream(file);
OPCPackage oPackage = OPCPackage.open(is);
XWPFDocument document = new XWPFDocument(oPackage);) {
TokenIterator iterator = new TokenIterator(document);
assertTrue(!iterator.hasNext());
}
}
开发者ID:ObeoNetwork,项目名称:M2Doc,代码行数:13,代码来源:RunIteratorTests.java
示例4: readAsXLS
import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException; //导入依赖的package包/类
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);
}
}
开发者ID:vakinge,项目名称:jeesuite-libs,代码行数:15,代码来源:ExcelPerfModeReader.java
示例5: openFileAsWorkBook
import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException; //导入依赖的package包/类
private Workbook openFileAsWorkBook(MultipartFile file, FileParserMessage<DataFileColumn> msg) throws IOException {
try {
String fileName = file.getOriginalFilename();
if (".xlsx".equals(fileName.substring(fileName.lastIndexOf(".")))) {
return new XSSFWorkbook(file.getInputStream());
}
if (".xls".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();
}
}
开发者ID:abixen,项目名称:abixen-platform,代码行数:16,代码来源:ExcelParserServiceImpl.java
示例6: process
import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException; //导入依赖的package包/类
/**
* 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 (FileInputStream fis = new FileInputStream(file);
OPCPackage opcPackage = OPCPackage.open(fis)) {
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 (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(org.apache.poi.openxml4j.exceptions.InvalidFormatException | NotOfficeXmlFileException ife) {
throw new ZeroCellException("Cannot load file. The file must be an Excel 2007+ Workbook (.xlsx)");
} 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);
}
}
开发者ID:creditdatamw,项目名称:zerocell,代码行数:50,代码来源:ReaderUtil.java
注:本文中的org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论