jxl读取html格式excel,基于Java+Selenium的WebUI自动化测试框架(十)-----读取Excel文件(JXL)...
packagewebui.xUtils;importjava.io.File;importorg.testng.Reporter;importjxl.Cell;importjxl.Sheet;importjxl.Workbook;public classExcelReadJXL {/***jxl讀取Excel
*指定文檔路徑及名稱
* 指定開始及結(jié)束行,開始及結(jié)束列
*@paramsheet_name 為sheet工作表名稱,也可以用整型數(shù)字,從0開始。
*@paramstart_row 開始行,0開始
*@paramend_row 結(jié)束行,0開始
*@paramstart_col 開始列,0開始
*@paramend_col 結(jié)束列,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);//判斷輸入的數(shù)值是否超出范圍值
if (end_row-start_row+1>row_length) {
log.error("選擇的行數(shù)超出實際數(shù)據(jù)范圍!請修改起始結(jié)束行數(shù)..");
Reporter.log("選擇的行數(shù)超出實際數(shù)據(jù)范圍!請修改起始結(jié)束行數(shù)..");
}if (end_col-start_col+1>col_length) {
log.error("選擇的數(shù)據(jù)列數(shù)超出實際數(shù)據(jù)范圍內(nèi)!請修改起始結(jié)束列數(shù)..");
Reporter.log("選擇的數(shù)據(jù)列數(shù)超出實際數(shù)據(jù)范圍內(nèi)!請修改起始結(jié)束列數(shù)..");
}//整體思想為:按每行為一組數(shù)據(jù)進(jìn)行讀取//外循環(huán)--行數(shù)
for(int row = start_row,i = 0;row <= end_row || i < testcase_data.length ; row++,i++) {//單獨(dú)一行時,讀取每一列的數(shù)據(jù)//內(nèi)循環(huán)--列數(shù)
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數(shù)據(jù)完成!");
Reporter.log("讀取Excel數(shù)據(jù)完成!");returntestcase_data;
}//讀取全部sheet頁數(shù)據(jù)
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];//整體思想仍然是按行獲取//外循環(huán)--行數(shù)
for(int i = 0 ; i < rows ; i++) {//內(nèi)循環(huán)--列數(shù),取每一行中每一列的數(shù)據(jù)
for(int j = 0 ; j < cols ; j++) {//數(shù)據(jù)形式為(列號,行號)
cell =testcase_data_sheet.getCell(j, i);
cell_value=cell.getContents();//存入字符串?dāng)?shù)組的形式為(行號,列號)
testcase_data[i][j] =cell_value;
}
}
}catch(Exception e) {
e.printStackTrace();
}returntestcase_data;
}}
總結(jié)
以上是生活随笔為你收集整理的jxl读取html格式excel,基于Java+Selenium的WebUI自动化测试框架(十)-----读取Excel文件(JXL)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python hook_python_理
- 下一篇: zynq linux网口不通,已解决: