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();
}
}
}
答案 0 :(得分:0)
使用Jxl获取Excel文件中的已用行.....
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);
}
}