ISSU在读取Excel文档(JAVA的code) [英] Issu while reading excel document (java code)
问题描述
我有一个java code后者读取Excel数据。在运行Java code它显示了以下错误。帮我解决一样。此外,我需要知道阅读的的.xlsx 的文件中的其他方法。谢谢
(小编辑)我怎么可以打印各自的列行。对于如
年龄
19
20
21薪水
35K
20K
40K
。
。
。
异常线程main
org.apache.poi.poifs.filesystem.OfficeXmlFileException:提供
数据似乎是在Office 2007+ XML。您呼叫的一部分
POI的与OLE2 Office文档的交易。你需要调用
POI的不同部分来处理这个数据(例如XSSF而不是HSSF)
在
org.apache.poi.poifs.storage.HeaderBlock(HeaderBlock.java:131)
在
org.apache.poi.poifs.storage.HeaderBlock(HeaderBlock.java:104)
在
org.apache.poi.poifs.filesystem.POIFSFileSystem(POIFSFileSystem.java:138)
在
org.apache.poi.hssf.usermodel.HSSFWorkbook(HSSFWorkbook.java:322)
在
org.apache.poi.hssf.usermodel.HSSFWorkbook(HSSFWorkbook.java:303)
在ExcelRead.main(ExcelRead.java:18)
块引用>化java code是如下:
进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.FileNotFoundException;
进口java.io.IOException异常;
进口java.util.Iterator的;进口org.apache.poi.hssf.usermodel.HSSFSheet;
进口org.apache.poi.hssf.usermodel.HSSFWorkbook;
进口org.apache.poi.ss.usermodel.Cell;
进口org.apache.poi.ss.usermodel.Row;
公共类ExcelRead {
公共静态无效的主要(字串[] args){ 尝试{
的FileInputStream文件=新的FileInputStream(新文件(C:/Users/vinayakp/Desktop/Book.xlsx));
HSSFWorkbook工作簿=新HSSFWorkbook(文件);
HSSFSheet片= workbook.getSheetAt(0);
迭代器<行> rowIterator = sheet.iterator();
而(rowIterator.hasNext()){
鳞次栉比= rowIterator.next();
迭代&所述;电池> cellIterator = row.cellIterator();
而(cellIterator.hasNext()){
电池单元= cellIterator.next();
开关(cell.getCellType()){
案例Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue()+\\ t \\ t的);
打破;
案例Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue()+\\ t \\ t的);
打破;
案例Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue()+\\ t \\ t的);
打破;
}
}
的System.out.println();
}
file.close();
}赶上(FileNotFoundException异常五){
e.printStackTrace();
}赶上(IOException异常AE){
ae.printStackTrace();
}
}
}
解决方案删除previus导入类再添加后
进口org.apache.poi.ss.usermodel.Cell;
进口org.apache.poi.ss.usermodel.Row;
进口org.apache.poi.ss.usermodel.Sheet;
进口org.apache.poi.ss.usermodel.Workbook;
进口org.apache.poi.ss.usermodel.WorkbookFactory;
私有静态无效读(字符串路径){
工作簿簿= NULL;
FIS的FileInputStream = NULL;
尝试{
文件源=新的文件(路径);
如果(source.exists()){
FIS =新的FileInputStream(源);
工作簿= WorkbookFactory.create(源); }其他{
JOptionPane.showMessageDialog(NULL,文件路径是不存在的。,错误,JOptionPane.ERROR_MESSAGE);
} 苫布苫布= NULL;
INT lastRowNum = 0;
INT numSheets = workbook.getNumberOfSheets();
的for(int i = 0; I< numSheets;我++){
片= workbook.getSheetAt(ⅰ);
如果(sheet.getPhysicalNumberOfRows()大于0){
lastRowNum = sheet.getLastRowNum();
INT lastCellNum = 0;
对于(鳞次栉比:表){
员工EMP =新员工(); INT numOfCell = row.getPhysicalNumberOfCells();
的System.out.println(numOfCell ::+ numOfCell);
字符串stringValues [] =新的String [numOfCell]
(电池单元:行){
//细胞= row.getCell(cellIndex);
INT cellIndex = cell.getColumnIndex();
logger.info(cellIndex ::+ cellIndex);
开关(cell.getCellType()){ 案例Cell.CELL_TYPE_FORMULA:
// PRINTVALUE =FORMULA值=+ cell.getCellFormula();
stringValues [cellIndex] = cell.getCellFormula();
打破; 案例Cell.CELL_TYPE_NUMERIC:
// PRINTVALUE =数值=+ cell.getNumericCellValue();
的System.out.println(值是数字::+ cell.getNumericCellValue());
stringValues [cellIndex] =将String.valueOf(cell.getNumericCellValue());
打破; 案例Cell.CELL_TYPE_STRING:
// PRINTVALUE =的字符串值=+ cell.getStringCellValue();
stringValues [cellIndex] = cell.getStringCellValue();
打破; 案例Cell.CELL_TYPE_BLANK:
// PRINTVALUE =的字符串值=+ cell.getStringCellValue();
stringValues [cellIndex] = cell.getStringCellValue();
打破; 默认:
}
} }
}
}
} }赶上(InvalidFormatException E){
logger.error(e.getMessage());
e.printStackTrace();
}赶上(FileNotFoundException异常五){
e.printStackTrace();
logger.error(e.getMessage());
}赶上(IOException异常五){
logger.error(e.getMessage());
e.printStackTrace();
}
赶上(例外五){
logger.error(e.getMessage());
e.printStackTrace();
}
最后{
如果(FIS!= NULL){
尝试{
fis.close();
FIS = NULL;
}赶上(IOException异常ioEx){
logger.error(ioEx.getMessage());
}
}
}
}I have a java code which reads the Excel data. On running the java code it's showing the following error. Help me resolve the same. Also, i need to know other method of reading .xlsx file. THANKS
(A small edit) how i can print rows with their respective columns. For e.g.
Age 19 20 21 Salary 35k 20k 40k . . .
Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF) at org.apache.poi.poifs.storage.HeaderBlock.(HeaderBlock.java:131) at org.apache.poi.poifs.storage.HeaderBlock.(HeaderBlock.java:104) at org.apache.poi.poifs.filesystem.POIFSFileSystem.(POIFSFileSystem.java:138) at org.apache.poi.hssf.usermodel.HSSFWorkbook.(HSSFWorkbook.java:322) at org.apache.poi.hssf.usermodel.HSSFWorkbook.(HSSFWorkbook.java:303) at ExcelRead.main(ExcelRead.java:18)
The java code is as follows:
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Iterator; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; public class ExcelRead { public static void main(String[] args) { try { FileInputStream file = new FileInputStream(new File("C:/Users/vinayakp/Desktop/Book.xlsx")); HSSFWorkbook workbook = new HSSFWorkbook(file); HSSFSheet sheet = workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet.iterator(); while(rowIterator.hasNext()) { Row row = rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); while(cellIterator.hasNext()) { Cell cell = cellIterator.next(); switch(cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: System.out.print(cell.getBooleanCellValue() + "\t\t"); break; case Cell.CELL_TYPE_NUMERIC: System.out.print(cell.getNumericCellValue() + "\t\t"); break; case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + "\t\t"); break; } } System.out.println(""); } file.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException ae) { ae.printStackTrace(); } } }
解决方案After deleting previus importing class then add
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; private static void read(String path){ Workbook workbook = null; FileInputStream fis = null; try { File source = new File(path); if(source.exists()){ fis = new FileInputStream(source); workbook = WorkbookFactory.create(source); }else{ JOptionPane.showMessageDialog(null, "File path is not exist.", "Error", JOptionPane.ERROR_MESSAGE); } Sheet sheet = null; int lastRowNum = 0; int numSheets = workbook.getNumberOfSheets(); for(int i = 0; i < numSheets; i++) { sheet = workbook.getSheetAt(i); if(sheet.getPhysicalNumberOfRows() > 0) { lastRowNum = sheet.getLastRowNum(); int lastCellNum = 0; for(Row row : sheet) { Employee emp = new Employee(); int numOfCell = row.getPhysicalNumberOfCells(); System.out.println("numOfCell:: "+numOfCell); String stringValues [] = new String[numOfCell]; for(Cell cell : row) { // cell = row.getCell(cellIndex); int cellIndex = cell.getColumnIndex(); logger.info("cellIndex:: "+ cellIndex); switch (cell.getCellType()) { case Cell.CELL_TYPE_FORMULA: // printValue = "FORMULA value=" + cell.getCellFormula(); stringValues[cellIndex] = cell.getCellFormula(); break; case Cell.CELL_TYPE_NUMERIC: //printValue = "NUMERIC value=" + cell.getNumericCellValue(); System.out.println("Value is numeric:: "+ cell.getNumericCellValue()); stringValues[cellIndex] = String.valueOf(cell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: // printValue = "STRING value=" + cell.getStringCellValue(); stringValues[cellIndex] = cell.getStringCellValue(); break; case Cell.CELL_TYPE_BLANK: // printValue = "STRING value=" + cell.getStringCellValue(); stringValues[cellIndex] = cell.getStringCellValue(); break; default: } } } } } } } catch (InvalidFormatException e) { logger.error(e.getMessage()); e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); logger.error(e.getMessage()); } catch (IOException e) { logger.error(e.getMessage()); e.printStackTrace(); } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); fis = null; } catch (IOException ioEx) { logger.error(ioEx.getMessage()); } } } }
这篇关于ISSU在读取Excel文档(JAVA的code)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!