Results 1 to 8 of 8
  1. #1
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Using Apache POI with HashMap

    I am trying to make a HashMap with excel data.
    To extract the excel data I use the Java Api Apache POI.

    Now I have read in the data, so the Api works fine.
    But I don't get how to use HashMap with this program.
    I did it before on an text file, but that was completly diffrent.
    Never done it with an excel file before.

    Java Code:
    		if (e.getSource() == button1) {
    			// Open JFileChooser. 
    			chooser1.showOpenDialog(null); 
    			// Get the selected file. 
    			java.io.File file1 = chooser1.getSelectedFile();
    			//
    			FileInputStream fileInput1 = null; 
    			// Create a FileInputStream that will be used to read excel files. 
    			try {
    				fileInput1 = new FileInputStream(file1);
    				// Create an excel workbook. 
    				XSSFWorkbook workbook = new XSSFWorkbook(fileInput1); 
    				// Get the first sheet. 
    				XSSFSheet fileSheet1 = workbook.getSheetAt(0);
    				// Create Iterator. 
    				Iterator<Row> rows1 = fileSheet1.rowIterator();
    				
    				int row1  = fileSheet1.getPhysicalNumberOfRows();
    				for(int r = 1; r < row1; r++) {
    					System.out.println("1");
    					//r++; 
    					while (rows1.hasNext()) {
    						//XSSF = .xlsx files, HSSF = .xls files. 
    						XSSFRow row = (XSSFRow) rows1.next();
    						Iterator<Cell> cells = row.cellIterator();
    						//ArrayList fileData1 = new ArrayList();
    						while (cells.hasNext()) {
    							XSSFCell cell = (XSSFCell) cells.next();
    							//Print statement, Testing.
    							System.out.print(cell); 
    							fileData1.add(cell); 
    						}
    					}
    				}
    				
    			} catch (IOException e1) {
    				System.out.println("Error in file 1"); 
    			} 
    		}
    My output now looks like this:
    Java Code:
    1
    ColText NextColText ThirdColText MoreText1 MoreText2 MoreText3
    1
    1
    1
    1
    1
    1
    The excel file looks like this:
    [code]
    ColText NextColText ThirdColText
    MoreText1 MoreText2 MoreText3
    [code]

    I probebly made a small mistake somewhere.
    But for some reason i just don't see it. :(
    What I would like is in HasMap:

    - ColText
    - MoreText1

    And if you say then print ColText you can print: ColText NextColText ThirdColText.

    I know this question is not totaly about the apache POI but since there is a lot of code about that in it.
    I posted it over here.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Using Apache POI with HashMap

    What are you mapping to what?
    That is what is the HashMap key and what it the value?

    Because I don't see any HashMap up there in your code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Re: Using Apache POI with HashMap

    I have this code:
    Java Code:
    Map mapFile1 = new HashMap();
    But I have not realy set up that the HashMap does things.

    What I am stuck at is the point the I need to seperated the first column from the rest of the data.

    Java Code:
    ColText NextColText ThirdColText
    MoreText1 MoreText2 MoreText3
    How do I sperate the:
    ColText
    MoreText1
    from the file?

    So I can put it in HashMap.

  4. #4
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Re: Using Apache POI with HashMap

    I was hoping tho print as output:
    1 ColText
    NextColText ThirdColText
    1 MoreText1
    MoreText2 MoreText3

    with the code in my first post.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Using Apache POI with HashMap

    OK, so ColText is the key?
    And the value is the rest of the text in the next 'x' columns?

    So either define the Map as:
    Java Code:
    Map<String, List<String>> mapFile1 = new HashMap<String, List<String>>();
    Or have a data class containing the other columns data and:
    Java Code:
    Map<String, ColumnData> mapFile1 = new HashMap<String, ColumnData>();
    then add to the map?

    I will comment on this bit:
    Java Code:
            int row1  = fileSheet1.getPhysicalNumberOfRows(); 
            for(int r = 1; r < row1; r++) { 
                System.out.println("1"); 
                //r++;  
                while (rows1.hasNext()) { 
    etc etc.
    You don't need to know the number of rows, just loop using hasNext().
    That outer for loop is entirely unecessary.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Re: Using Apache POI with HashMap

    Thank you for the replies.
    I will take that code part out, and try to get HashMap working now.

    Thank. :)

  7. #7
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Re: Using Apache POI with HashMap

    I added some code.

    Java Code:
     
    					button1Amount = button1Amount +1; 
    					columnNumbers1 = "A" +button1Amount; 
    					//System.out.println(columnNumbers1);
    
    					CellReference cellReference1 = new CellReference(columnNumbers1);
    					Cell cellDataFirstColumn1 = row.getCell(cellReference1.getCol()); 
    					System.out.println(cellDataFirstColumn1);
    And this one does what I would like it to do.
    It takes a value from a certain cell in the excel file.

    But I was wondering how I can let ik take every cell that begins with a "A", and not just "A1". (Like A2 and A3 etc. maybe up to A3000)
    Because if I fill in "A". I get an error.

    Also I have now that it counts the number of lines in the file.
    It does that correct, but then it only grabs the first A1.
    And A2 he does not take anymore, while the counting works.
    Any suggestions on that?

    (HashMap works also now, thanks for that idea.)
    Last edited by Lund01; 09-10-2012 at 10:20 AM.

  8. #8
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Re: Using Apache POI with HashMap

    I got everything working now. :)
    Thanks for the advice on the HashMap.

Similar Threads

  1. HashMap
    By Shukaib_khan in forum New To Java
    Replies: 4
    Last Post: 12-25-2011, 12:51 PM
  2. final HashMap hm=new HashMap();
    By sangramkeshari.jena in forum New To Java
    Replies: 4
    Last Post: 07-21-2011, 09:44 PM
  3. Hashmap to TXT and TXT to Hashmap
    By elvinny in forum Advanced Java
    Replies: 4
    Last Post: 02-16-2011, 11:12 PM
  4. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  5. Replies: 0
    Last Post: 11-15-2008, 07:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •