packagewebui.xUtils;importjava.io.File;importorg.testng.Reporter;importjxl.Cell;importjxl.Sheet;importjxl.Workbook;public classExcelReadJXL {/***jxl读取Excel
*指定文档路径及名称
* 指定开始及结束行,开始及结束列
*@paramsheet_name 为sheet工作表名称,也可以用整型数字,从0开始。
*@paramstart_row 开始行,0开始
*@paramend_row 结束行,0开始
*@paramstart_col 开始列,0开始
*@paramend_col 结束列,0开始
*@paramsourcefile .xls文件路径
*@returnObject[][]*/
private staticlogUtil log;public static Object[][] caseDataExcel(String sheet_name,int start_row,int end_row,int start_col,intend_col,String sourcefile){
String cell_value= null;
Cell cell= null;int row_length = end_row - start_row +1;int col_length = end_col - start_col +1;
String [][] testcase_data= newString [row_length][col_length];
Workbook testcase_data_book= null;try{
testcase_data_book= Workbook.getWorkbook(newFile(sourcefile));
Sheet testcase_data_sheet=testcase_data_book.getSheet(sheet_name);//判断输入的数值是否超出范围值
if (end_row-start_row+1>row_length) {
log.error("选择的行数超出实际数据范围!请修改起始结束行数..");
Reporter.log("选择的行数超出实际数据范围!请修改起始结束行数..");
}if (end_col-start_col+1>col_length) {
log.error("选择的数据列数超出实际数据范围内!请修改起始结束列数..");
Reporter.log("选择的数据列数超出实际数据范围内!请修改起始结束列数..");
}//整体思想为:按每行为一组数据进行读取//外循环--行数
for(int row = start_row,i = 0;row <= end_row || i < testcase_data.length ; row++,i++) {//单独一行时,读取每一列的数据//内循环--列数
for(int col = start_col,j = 0;col <= end_col || j < col_length ; col++,j++) {
cell=testcase_data_sheet.getCell(col,row);
cell_value=cell.getContents();
testcase_data[i][j]=cell_value;
}
}
}catch(Exception e) {
e.printStackTrace();
}//log.info("读取Excel数据完成!");
Reporter.log("读取Excel数据完成!");returntestcase_data;
}//读取全部sheet页数据
public staticObject[][] case_data_excel_jxl(String sheet_name,String sourcefile){
String cell_value= null;
Cell cell= null;
String[][] testcase_data= null;
Workbook testcase_data_book= null;try{
testcase_data_book= Workbook.getWorkbook(newFile(sourcefile));
Sheet testcase_data_sheet=testcase_data_book.getSheet(sheet_name);int rows =testcase_data_sheet.getRows();int cols =testcase_data_sheet.getColumns();
testcase_data= newString[rows][cols];//整体思想仍然是按行获取//外循环--行数
for(int i = 0 ; i < rows ; i++) {//内循环--列数,取每一行中每一列的数据
for(int j = 0 ; j < cols ; j++) {//数据形式为(列号,行号)
cell =testcase_data_sheet.getCell(j, i);
cell_value=cell.getContents();//存入字符串数组的形式为(行号,列号)
testcase_data[i][j] =cell_value;
}
}
}catch(Exception e) {
e.printStackTrace();
}returntestcase_data;
}}