How to store values from excel to some collection in java -
i have excel file following,
**method name** **status code** **user** **password** getloggedinuserdetails 400 anto test createrequisition 400 mayank hexgen excelmdm 400 xxxxx hexgen createorder 400 yyyyy hexgen confirmorder 400 zzzzz hexgen i want save above details in collection can access details providing username , details method name, status code,and password.
i tried thing , able store method name , status code
package com.hexgen.tools; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import java.util.iterator; import java.util.linkedhashmap; import java.util.map; import org.apache.poi.hssf.usermodel.hssfsheet; import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.poi.ss.usermodel.cell; import org.apache.poi.ss.usermodel.row; public class testmethodsdetails { public map<string, integer> getknowngoodmap(string filepath) { string key = ""; int value = 0; //filepath="testmethoddetails.xls"; map<string, integer> knowngoodmap = new linkedhashmap<string, integer>(); try { fileinputstream file = new fileinputstream(new file(filepath)); // workbook instance xls file hssfworkbook workbook = new hssfworkbook(file); // first sheet workbook hssfsheet sheet = workbook.getsheetat(0); // iterate through each rows first sheet iterator<row> rowiterator = sheet.iterator(); while (rowiterator.hasnext()) { row row = rowiterator.next(); // each row, iterate through each columns iterator<cell> celliterator = row.celliterator(); while (celliterator.hasnext()) { cell cell = celliterator.next(); switch (cell.getcelltype()) { case cell.cell_type_numeric: value = (int) cell.getnumericcellvalue(); break; case cell.cell_type_string: key = cell.getstringcellvalue(); break; } if (key != null && value != integer.min_value) { knowngoodmap.put(key, value); key = null; value = integer.min_value; } } } file.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return knowngoodmap; } public static void main(string[] args) { testmethodsdetails details = new testmethodsdetails(); system.out.println("method details : "+details.getknowngoodmap("testmethoddetails.xls")); } } the above code prints following :
method details : {method name=0, getloggedinuserdetails=400, createrequisition=400, excelmdm=400, createorder=400, confirmorder=400} frank don't know mechanism use store these details enables easy access me process details, user name , password different, have given here user , password sample kindly me this.
you should use object oriented approach create entities. create class representing excel data row, lets called records , put objects of class in hashmap against username key. here sample class :
public class record { private string methodname; private string statuscode; private string user; private string password; public string getmethodname() { return methodname; } public void setmethodname(string methodname) { this.methodname = methodname; } public string getstatuscode() { return statuscode; } public void setstatuscode(string statuscode) { this.statuscode = statuscode; } public string getuser() { return user; } public void setuser(string user) { this.user = user; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } } define collection store records as
map<string, record> recordsmap = new <string, record>hashmap(); read excel file, create record each row , save in collection. should easy , quick retrieve records map.
Comments
Post a Comment