我收到一条错误消息,说明静态字段" CELL_TYPE _ ***"应该以静态方式访问

时间:2017-04-02 09:44:16

标签: java apache-poi

当我将鼠标悬停在' CELL_TYPE _ ****'上时,收到错误消息方法getCellData()中的方法,表示'静态字段" CELL_TYPE _ ***"应该以静态的方式访问'并在他们身上划线。



        package excelSelenium;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class seleniumIntg {
	XSSFWorkbook workbook = null;
	XSSFSheet sheet = null;
	XSSFRow Row = null;
	XSSFCell Cell = null;
	WebDriver driver = null;
	
	

	@Test(dataProvider = "getData")
	public void doLogin(String username, String password)
	{
		System.setProperty("webdriver.chrome.driver","C://testing/chromedriver_win32/chromedriver.exe" );
		driver = new ChromeDriver();
	
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		driver.get("https://login.yahoo.com/config/login?.src=fpctx&.intl=in&.lang=en-IN&.done=https://in.yahoo.com/%3fp=us");
		driver.findElement(By.xpath("//input[@id='login-username']")).sendKeys(username);
		driver.findElement(By.xpath("//div[@class='mbr-login-submit']/button")).click();
		driver.findElement(By.xpath("//input[@id='login-passwd']")).sendKeys(password);
		
	}
	@DataProvider
	public Object[][] getData() throws IOException
	{
		FileInputStream fis = new FileInputStream("C://Users/Gaurav/Documents/testid.xlsx");
		workbook = new XSSFWorkbook(fis);
		sheet = workbook.getSheet("sheet1");
		int rowCount = sheet.getFirstRowNum()+sheet.getLastRowNum()+1;
		int colCount = sheet.getRow(0).getLastCellNum();
		System.out.println("Row count is:" +rowCount+ "Col count is:" +colCount);
		Object[][] data = new Object[rowCount-1][colCount];
		for(int rNum = 2; rNum<=rowCount; rNum++)
			for(int cNum = 0; cNum<colCount; cNum++)
			{
				System.out.println(getCellData("sheet1",cNum,rNum));
				data[rNum-2][cNum]=getCellData("sheet1",cNum,rNum);
				
			}
		return data;	
		
	}
	
	
	public String getCellData(String sheetName, int colNum, int rowNum)
	{
		try{
		if(rowNum<=0)
			return "";
		int index = workbook.getSheetIndex(sheetName);
		if(index == -1)
			return "";
		sheet =workbook.getSheetAt(index);
		Row = sheet.getRow(rowNum-1);
		if(Row==null)
			return "";
		Cell = Row.getCell(colNum);
		if(Cell==null)
			return "";
		else if(Cell.getCellType()==Cell.CELL_TYPE_STRING)
			return Cell.getStringCellValue();
		
		else if(Cell.getCellType()==Cell.CELL_TYPE_NUMERIC||Cell.getCellType()==Cell.CELL_TYPE_FORMULA)
			{String CellText = String.valueOf(Cell.getNumericCellValue());
		return CellText;}
		else if(Cell.getCellType()==Cell.CELL_TYPE_BLANK)
			return "";
		else return String.valueOf(Cell.getBooleanCellValue());
	}
		catch(Exception e)
		{
			e.printStackTrace();
			return "row"+rowNum+"col"+colNum+"Does not exist";
		}
		
			
		
		
	}
	
		// TODO Auto-generated method stub
		
	

}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:4)

在以下说明中:

else if(Cell.getCellType()==Cell.CELL_TYPE_STRING)

尽管有外表,Cell是一个实例,而不是一个类,因为你在类中声明了这个字段:

XSSFCell Cell = null;

但静态字段和方法不需要通过实例作为前缀来调用它们,而是由类调用。

因此,您应该为您引用的静态字段添加前缀:

else if(Cell.getCellType()==XSSFCell.CELL_TYPE_STRING)

为了避免这种误导性代码,在您实际使用类时,给出的印象是您正在使用实例作为前缀,请遵循Java代码约定,其中包括变量名称应以小写字符开头。

这样更好:

XSSFCell cell = null;
...
else if(cell.getCellType() == XSSFCell.CELL_TYPE_STRING)

作为旁注,自POI 3.15 beta 3起the CELL_TYPE_STRING static field is deprecated 建议您使用枚举org.apache.poi.ss.usermodel.CellType.STRING代替。

答案 1 :(得分:1)

首先,您应该重命名变量,以便它们以小写字母开头(例如cell而不是Cell)以避免混淆。

所以cell是继承自Cell的类XSSFCell的实例。

静态字段,例如CELL_TYPE_STRING在班级Cell中宣布 :所以你应该使用Cell.CELL_TYPE_STRING而不是cell.CELL_TYPE_STRING