Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 写入excel文件_Selenium_Webdriver - Fatal编程技术网

Selenium 写入excel文件

Selenium 写入excel文件,selenium,webdriver,Selenium,Webdriver,}使用Jxl获取Excel文件中的行 public class ExcelLibrary{ /** * @param args */ //method which accepts a sheet name, row number and cell number // and returns the string value present in that cell public String getExcelData(String sheetName,int rowNum, int ce

}

使用Jxl获取Excel文件中的行

public class ExcelLibrary{

/**
 * @param args
 */ 
//method which accepts a sheet name, row number and cell number
// and returns the string value present in that cell
public String getExcelData(String sheetName,int rowNum, int cellNum){
    String retVal=null;
    try {
    FileInputStream fis = new FileInputStream("D:\\seleniumbsw9\\data.xlsx"); 
        //get the workbook object
        Workbook wb = WorkbookFactory.create(fis);
        //get the sheet [a particular sheet in the workbook]
        Sheet s = wb.getSheet(sheetName);
        //get the row [a particular row in the sheet]
        Row r = s.getRow(rowNum);
        //get the cell [a particular cell of the row obtained above]
        Cell c = r.getCell(cellNum);
        //get the string cell value from the cell
        retVal = c.getStringCellValue();            
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return retVal;
}
public int getRowCount(String sheetName){
    int lastRow=0;
    try {
    FileInputStream fis = new FileInputStream("D:\\seleniumbsw9\\data.xlsx");
        //get the workbook object
        Workbook wb = WorkbookFactory.create(fis);
        //get the sheet [a particular sheet in the workbook]
        Sheet s = wb.getSheet(sheetName);
        //return the last row in which data is present counted form 0
        lastRow = s.getLastRowNum();        
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return lastRow;
}
public void writeDataToExcel(String sheetName, int rowNum, int cellNum, String data){
    try {
    FileInputStream fis = new FileInputStream("D:\\seleniumbsw9\\data.xlsx");
        //get the workbook object
        Workbook wb = WorkbookFactory.create(fis);
        //get the sheet [a particular sheet in the workbook]
        Sheet s = wb.getSheet(sheetName);
        //get the row where data needs to be written. This step 
        //assumes that we are writing to a cell in an existing row
        Row r = s.getRow(rowNum);           
        //Create the cell in that row. If we are trying to write to
        //a cell which has not been created, it will throw error. First
        //we need to create a cell, then set the value of the cell
        //This step is not needed if we are writing to an existing cell
        Cell c = r.createCell(cellNum);         
        c.setCellValue(data);
    FileOutputStream fos=new FileOutputStream("D:\\seleniumbsw9\\data.xlsx");
        wb.write(fos);
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }       
}

请指定您想做什么,并正确设置问题格式。不可能读懂你的问题!这甚至不是一个问题!添加到评论中是否有使用jxl的特定原因?因为这个API有很多限制,所以最好使用ApachePOIAPI。
package Testpackage;

import java.io.FileInputStream;
import java.io.IOException;
import jxl.Workbook;
import jxl.Sheet;
import jxl.read.biff.BiffException;

public class Rowcount {
    public static void main(String[] args) throws BiffException, IOException {
        FileInputStream exo=new FileInputStream("E:\\test1.xls");
        Workbook wbo=Workbook.getWorkbook(exo);
        Sheet wso=wbo.getSheet(0);
        int lastrownum;
        lastrownum= wso.getRows();
        System.out.println(lastrownum);
    }
}