當我將鼠標懸停在方法getCellData中的「CELL_TYPE _ ****」方法上時收到錯誤消息(),說'靜態字段'CELL_TYPE _ ***「應該以靜態方式訪問',並在它們上方劃線。我收到一條錯誤消息,指出應該以靜態方式訪問靜態字段「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 {
\t XSSFWorkbook workbook = null;
\t XSSFSheet sheet = null;
\t XSSFRow Row = null;
\t XSSFCell Cell = null;
\t WebDriver driver = null;
\t
\t
\t @Test(dataProvider = "getData")
\t public void doLogin(String username, String password)
\t {
\t \t System.setProperty("webdriver.chrome.driver","C://testing/chromedriver_win32/chromedriver.exe");
\t \t driver = new ChromeDriver();
\t
\t \t driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
\t \t driver.get("https://login.yahoo.com/config/login?.src=fpctx&.intl=in&.lang=en-IN&.done=https://in.yahoo.com/%3fp=us");
\t \t driver.findElement(By.xpath("//input[@id='login-username']")).sendKeys(username);
\t \t driver.findElement(By.xpath("//div[@class='mbr-login-submit']/button")).click();
\t \t driver.findElement(By.xpath("//input[@id='login-passwd']")).sendKeys(password);
\t \t
\t }
\t @DataProvider
\t public Object[][] getData() throws IOException
\t {
\t \t FileInputStream fis = new FileInputStream("C://Users/Gaurav/Documents/testid.xlsx");
\t \t workbook = new XSSFWorkbook(fis);
\t \t sheet = workbook.getSheet("sheet1");
\t \t int rowCount = sheet.getFirstRowNum()+sheet.getLastRowNum()+1;
\t \t int colCount = sheet.getRow(0).getLastCellNum();
\t \t System.out.println("Row count is:" +rowCount+ "Col count is:" +colCount);
\t \t Object[][] data = new Object[rowCount-1][colCount];
\t \t for(int rNum = 2; rNum<=rowCount; rNum++)
\t \t \t for(int cNum = 0; cNum<colCount; cNum++)
\t \t \t {
\t \t \t \t System.out.println(getCellData("sheet1",cNum,rNum));
\t \t \t \t data[rNum-2][cNum]=getCellData("sheet1",cNum,rNum);
\t \t \t \t
\t \t \t }
\t \t return data; \t
\t \t
\t }
\t
\t
\t public String getCellData(String sheetName, int colNum, int rowNum)
\t {
\t \t try{
\t \t if(rowNum<=0)
\t \t \t return "";
\t \t int index = workbook.getSheetIndex(sheetName);
\t \t if(index == -1)
\t \t \t return "";
\t \t sheet =workbook.getSheetAt(index);
\t \t Row = sheet.getRow(rowNum-1);
\t \t if(Row==null)
\t \t \t return "";
\t \t Cell = Row.getCell(colNum);
\t \t if(Cell==null)
\t \t \t return "";
\t \t else if(Cell.getCellType()==Cell.CELL_TYPE_STRING)
\t \t \t return Cell.getStringCellValue();
\t \t
\t \t else if(Cell.getCellType()==Cell.CELL_TYPE_NUMERIC||Cell.getCellType()==Cell.CELL_TYPE_FORMULA)
\t \t \t {String CellText = String.valueOf(Cell.getNumericCellValue());
\t \t return CellText;}
\t \t else if(Cell.getCellType()==Cell.CELL_TYPE_BLANK)
\t \t \t return "";
\t \t else return String.valueOf(Cell.getBooleanCellValue());
\t }
\t \t catch(Exception e)
\t \t {
\t \t \t e.printStackTrace();
\t \t \t return "row"+rowNum+"col"+colNum+"Does not exist";
\t \t }
\t \t
\t \t \t
\t \t
\t \t
\t }
\t
\t \t // TODO Auto-generated method stub
\t \t
\t
}
大衛你好!非常感謝您的幫助,但我仍然遇到同樣的錯誤。 '字段CELL_TYPE_STRING已棄用'。 –
你好Gaurav。這不是編譯錯誤,而是「僅」警告。我剛剛更新了我的答案以解決它。 – davidxxx
非常感謝大衛!我現在得到了他們。 –