The Java Excel data read method in its own package

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.