在POI中从Excel读取元素时出错

时间:2016-09-29 06:17:08

标签: java excel apache-poi

我已经编写了下面的代码来从Excel中读取元素。但是在运行此代码时出现错误。任何人都可以帮我这个吗?

package testUtilities;

import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.text.DecimalFormat; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Map; 

import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.streaming.SXSSFRow.CellIterator;
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 


@SuppressWarnings("unused")
public class ReadLocatorsFromExcel {


    public static void main (String args[]) throws IOException {

        FileInputStream file = new FileInputStream(new File("/home/suvin/Documents/SeleniumWebdriver/Script/PaytmAutomationFramework/src/testUtilities/TestData.xlsx"));
            XSSFWorkbook wb = new XSSFWorkbook(file);
            XSSFSheet sheet = wb.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext())
            {
                Row nexRow = rowIterator.next();
                Iterator<Cell> celIterator = nexRow.cellIterator();
                while (celIterator.hasNext())
                {
                Cell cell = celIterator.next();
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue());
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue());
                    break;
            }
                System.out.print(" - ");
                }
                System.out.println();
            }

             wb.close();
             file.close();
        }




    }

我在此代码中从行

中获得了已弃用方法的异常
case Cell.CELL_TYPE_STRING:
                        System.out.print(cell.getStringCellValue());
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        System.out.print(cell.getBooleanCellValue());
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.print(cell.getNumericCellValue());
                        break;

我们可以在替代方案中使用什么?

1 个答案:

答案 0 :(得分:0)

您很可能错过了必需的jar文件。用maven?加上这个:

<dependency>
   <groupId>org.apache.xmlbeans</groupId>
   <artifactId>xmlbeans</artifactId>
   <version>2.6.0</version>
</dependency>

到你的pom.xml。