阅读Excel文档时出现问题(Java代码)

阅读Excel文档时出现问题(Java代码),java,exception,apache-poi,Java,Exception,Apache Poi,我有一些读取Excel数据的Java代码。在运行Java代码时,它显示以下错误。帮我解决同样的问题。另外,我需要知道读取.xlsx文件的其他方法 (一个小编辑)我如何打印带有各自列的行。例如: Age 19 20 21 Salary 35k 20k 40k . . . 线程“main”中出现异常 org.apache.poi.poifs.filesystem.OfficeXmlFileException:提供的 数据似乎位于Office 2007+XML中。你是在说这个角色 处理OLE2 O

我有一些读取Excel数据的Java代码。在运行Java代码时,它显示以下错误。帮我解决同样的问题。另外,我需要知道读取.xlsx文件的其他方法

(一个小编辑)我如何打印带有各自列的行。例如:

Age
19
20
21

Salary
35k
20k
40k
.
.
.
线程“main”中出现异常 org.apache.poi.poifs.filesystem.OfficeXmlFileException:提供的 数据似乎位于Office 2007+XML中。你是在说这个角色 处理OLE2 Office文档的POI的一部分。你需要打个电话 处理该数据的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.poifsffilesystem.(poifsffilesystem.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代码如下所示:

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();
    }
}
}
导入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;
公共课精读{
公共静态void main(字符串[]args){
试一试{
FileInputStream file=newfileinputstream(新文件(“C:/Users/vinayakp/Desktop/Book.xlsx”);
HSSF工作簿=新的HSSF工作簿(文件);
HSSFSheet sheet=工作簿。getSheetAt(0);
迭代器rowIterator=sheet.Iterator();
while(roweiterator.hasNext()){
行=行迭代器。下一步();
迭代器cellIterator=row.cellIterator();
while(cellIterator.hasNext()){
Cell=cellIterator.next();
开关(cell.getCellType()){
case Cell.Cell\u类型\u布尔值:
System.out.print(cell.getBooleanCellValue()+“\t\t”);
打破
case Cell.Cell\u类型\u数值:
System.out.print(cell.getNumericCellValue()+“\t\t”);
打破
case Cell.Cell\u类型\u字符串:
System.out.print(cell.getStringCellValue()+“\t\t”);
打破
}
}
System.out.println(“”);
}
file.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOAE异常){
ae.printStackTrace();
}
}
}

您使用了错误的类来读取文件
HSSFWorkbook
用于旧excel格式。改用
XSSFWorkbook

编辑: 抄袭自。 你也这么做了吗

try { 
System.out.println("destDir==> "+destDir); 
XSSFWorkbook workBook = new XSSFWorkbook(destDir); 
XSSFSheet sheet = workBook.getSheetAt(0); 
totalRows = sheet.getPhysicalNumberOfRows(); 
System.out.println("total no of rows >>>>"+totalRows); 

} catch (IOException e) { 
e.printStackTrace(); 
} 
编辑2:
删除以前的导入类后,请从此

了解apache POI,然后尝试添加

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());
                } 
            }
        }    
     }
import org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.Sheet;
导入org.apache.poi.ss.usermodel.工作簿;
导入org.apache.poi.ss.usermodel.WorkbookFactory;
私有静态无效读取(字符串路径){
工作簿=空;
FileInputStream fis=null;
试一试{
文件源=新文件(路径);
if(source.exists()){
fis=新文件输入流(源);
工作簿=WorkbookFactory.create(源);
}否则{
showMessageDialog(null,“文件路径不存在”,“错误”,JOptionPane.Error\u消息);
}       
Sheet=null;
int lastRowNum=0;
int numSheets=workbook.getNumberOfSheets();
对于(int i=0;i0){
lastRowNum=sheet.getLastRowNum();
int lastCellNum=0;
对于(行:页){
员工emp=新员工();
int numocell=row.getPhysicalNumberOfCells();
System.out.println(“numocell::”+numocell);
字符串stringValues[]=新字符串[numOfCell];
用于(单元格:行){
//cell=row.getCell(cellIndex);
int cellIndex=cell.getColumnIndex();
logger.info(“cellIndex::”+cellIndex);
开关(cell.getCellType()){
case Cell.Cell_类型_公式:
//printValue=“FORMULA value=“+cell.getCellFormula();
stringValues[cellIndex]=cell.getCellFormula();
打破
case Cell.Cell\u类型\u数值:
//printValue=“NUMERIC value=“+cell.getNumericCellValue();
System.out.println(“值是数字的::”+cell.getNumericCellValue());
stringValues[cellIndex]=String.valueOf(cell.getNumericCellValue());
打破
case Cell.Cell\u类型\u字符串:
//printValue=“STRING value=“+cell.getStringCellValue();
stringValues[cellIndex]=cell.getStringCellValue();
打破
case Cell.Cell\u类型\u空白:
//printValue=“STRING value=“+cell.getStringCellValue();
stringValues[cellIndex]=cell.getStringCellValue();