Package Org.webdriver.autotest.data;import JXL. Workbook;import JXL. Sheet;import JXL. Cell;import java.util.arraylist;import java.util.list;import Java.io.file;import java.util.HashMap;public class Read _case_data{static String sourcefile= ". \\testcase\\testcasedemo.xls "; Public object[][] Case_data_excel (int sheet_id, int start_row, int end_row, int start_col, int end_col) {String cell_value=null; Arraylist<object> testcase_data_list=new arraylist<object> (); String[][] Testcase_data_array=null; try {Workbook Testcase_data_workbook=workbook.getworkbook (new File (sourcefile)); Sheet Testcase_data_sheet=testcase_data_workbook.getsheet (sheet_id); int rows=testcase_data_sheet.getrows (); int Cols=testcase_data_sheet.getcolumns (); if (end_row>rows) {end_row=rows; }//Get each row of use case data for (int row = Start_row; row <=end_row; row++) {//Use an array to store each row of data. One row per loop, initialize an array once, release the original array memory//special note, the length of the array must be initialized correctly when you take only a few columns of data from the table string[] row_array=new string[end_col-start_col+1]; for (int col=start_col-1;col<=end_col-1;col++) {Cell_value=testcase_data_sheet.getcell (col, row). GetConte NTS (); Assigns each column value of each row to the row array, and the loop array is assigned a value of row_array[col]=cell_value; }//each obtained row of data will be deposited, the use case list Testcase_data_list.add (Row_array); } string[][] Testcase_data_array_try=new string[testcase_data_list.size ()][cols]; Testcase_data_array_try=testcase_data_list.toarray (Testcase_data_array_try); Testcase_data_array=testcase_data_array_try; } catch (Exception e) {//Todo:handle Exception e.printstacktrace (); } object[][] testcase_data_object= (object[][]) Testcase_data_array; return testcase_data_object; }}
The Java Excel data read method in its own package