https:\/\/mvnrepository.com\/artifact\/org.apache.poi\/poi<\/a><\/p>\n<dependency>\r\n<groupId>org.apache.poi<\/groupId>\r\n<artifactId>poi<\/artifactId>\r\n<version>3.12<\/version>\r\n<\/dependency>\r\n<dependency>\r\n<groupId>org.apache.poi<\/groupId>\r\n<artifactId>poi-ooxml<\/artifactId>\r\n<version>3.12<\/version>\r\n<\/dependency><\/pre>\nStep#4:\u00a0<\/strong>Create a new class say ExcelDatatoHashMap <\/b>in the src\/main\/java folder with the same package com.sheetstips.<\/p>\nStep#5:\u00a0<\/strong>Write the below code in the above ExcelDatatoHashMap <\/b>class<\/p>\nApproach:<\/strong><\/p>\n\n- Import all the required libraries using the import keyword.<\/li>\n
- Import HashMap library using the import keyword.<\/li>\n
- Import apache poi library using the import keyword.<\/li>\n
- Create a class say ExcelDatatoHashMap.<\/li>\n
- Inside the Main driver method, Create an object for the FileInputStream by passing the input excel file path as an argument to it (Here it reads the given Excel file).<\/li>\n
- Select the worksheet to work with from the above workbook(excel file) and<\/li>\n
- Store it in a variable<\/li>\n
- Create a HashMap object using the new keyword.<\/li>\n
- Iterate till the number of the rows of the sheet using the for loop (Here we use getLastRowNum() function to get number of rows in the excel file).<\/li>\n
- Get the cell value of the current row and convert it to String Value using the getRow(), getCell(), getNumericCellValue() functions.<\/li>\n
- Here this value is the key for the hashmap<\/li>\n
- Get the cell value of the current row and convert it to String Value using the getRow(),getCell(),getStringCellValue() functions.<\/li>\n
- Here this value is the value for the hashmap.<\/li>\n
- Store the above key and value to the hashmap using the put() function<\/li>\n
- Create an iterator to print all the entries of the hashmap<\/li>\n
- Loop till the hashmap iterator has next value using the hasNext() function<\/li>\n
- Get the hashmap corresponding key and value pairs using the next() function<\/li>\n
- Print the corresponding key and value of the hashmap using the getKey() and getValue() functions<\/li>\n
- Close the above workbook using the close() function<\/li>\n
- Close the above-opened input file using the close() function.<\/li>\n
- The Exit of the Program<\/li>\n<\/ul>\n
Below is the implementation:<\/strong><\/p>\npackage com.sheetstips;\r\n\r\n\/\/Import all the required libraries using the import keyword\r\nimport java.io.FileInputStream;\r\nimport java.io.IOException;\r\n\/\/Import HashMap library using the import keyword\r\nimport java.util.HashMap;\r\nimport java.util.Iterator;\r\nimport java.util.Map;\r\nimport java.util.Map.Entry;\r\n\r\n\/\/Import apache poi library using the import keyword\r\nimport org.apache.poi.xssf.usermodel.XSSFSheet;\r\nimport org.apache.poi.xssf.usermodel.XSSFWorkbook;\r\n\r\n\/\/Create a class say ExcelDatatoHashMap\r\npublic class ExcelDatatoHashMap {\r\n \/\/ Main driver method\r\n public static void main(String[] args) throws IOException {\r\n \r\n \/\/ Create an object for the FileInputStream by passing the input excel file\r\n \/\/ path as an argument to it (Here it reads the given Excel file)\r\n FileInputStream gvn_excelfile = new FileInputStream(\"sampleExcelFile.xlsx\");\r\n\r\n XSSFWorkbook workbokObj = new XSSFWorkbook(gvn_excelfile);\r\n System.out.println(workbokObj);\r\n \/\/ Select the worksheet to work with from the above workbook(excel file) and\r\n \/\/ store it in a variable\r\n XSSFSheet worksheet = workbokObj.getSheet(\"Sheet1\");\r\n\r\n \/\/ Create an HashMap object using the new keyword.\r\n HashMap<String, String> map = new HashMap<String, String>();\r\n\r\n \/\/ Iterate till the number of the rows of the sheet using the for loop\r\n \/\/ (Here we use getLastRowNum() function to get number of rows in the excel\r\n \/\/ file)\r\n for (int r = 0; r <= worksheet.getLastRowNum(); r++) {\r\n \/\/ Get the cell value of the current row and convert it to String Value using\r\n \/\/ the getRow(),getCell(),getNumericCellValue() functions\r\n \/\/ Here this value is the key for the hashmap\r\n String key = worksheet.getRow(r).getCell(0).getStringCellValue();\r\n \/\/ Get the cell value of the current row and convert it to String Value using\r\n \/\/ the getRow(),getCell(),getStringCellValue() functions\r\n \/\/ Here this value is the value for the hashmap\r\n String value = worksheet.getRow(r).getCell(1).getStringCellValue();\r\n \/\/ Store the above key and value to the hashmap using the put() function\r\n map.put(key, value);\r\n }\r\n\r\n \/\/ Create an iterator to print all the entries of the hashmap\r\n Iterator<Entry<String, String>> new_Iterator = map.entrySet().iterator();\r\n\r\n\/\/Loop till the hashmap iterator has next value using the hasNext() function\r\n while (new_Iterator.hasNext()) {\r\n \/\/ Get the hashmap corresponding key and value pairs using the next() function\r\n Map.Entry<String, String> new_Map = (Map.Entry<String, String>) new_Iterator.next();\r\n\/\/Print the corresponding key and value of the hashmap using the getKey() and getValue() functions\r\n System.out.println(new_Map.getKey() + \"|\" + new_Map.getValue());\r\n }\r\n\r\n \/\/ Close the above workbook using the close() function\r\n workbokObj.close();\r\n \/\/ Close the above opened input file using the close() function\r\n gvn_excelfile.close();\r\n }\r\n}\r\n\r\n<\/pre>\nStep#6:\u00a0<\/strong>Now run the code by Right-clicking on the project and click on run \u2013> Run as spring boot App<\/p>\nOutput:<\/strong><\/p>\n1|Sheetstips\r\n2|Btechgeeks\r\n3|Python-Programs\r\n4|PythonArrays<\/pre>\n<\/pre>\n <\/p>\n","protected":false},"excerpt":{"rendered":"
In this article, let us look at how to write data from an excel file into a HashMap using Apache POI. Apache POI library in Java: ApachePOI is an abbreviation for Poor Obfuscation Implementation, a Java API for reading and writing Microsoft documents. It includes the Wordbook, Sheet, Row, and Cell classes and interfaces. Apache … Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":4028,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182],"tags":[],"jetpack_featured_media_url":"https:\/\/sheetstips.com\/wp-content\/uploads\/2022\/07\/How-to-Write-Data-from-Excel-File-into-a-HashMap-using-Java-and-Apache-POI.jpg","jetpack-related-posts":[{"id":3895,"url":"https:\/\/sheetstips.com\/how-to-write-data-from-hashmap-to-excel-using-java-in-apache-poi\/","url_meta":{"origin":3898,"position":0},"title":"How to Write Data from HashMap to Excel using Java in Apache POI?","author":"David","date":"November 29, 2022","format":false,"excerpt":"In this article, let us look at how to write data from HashMap to an excel sheet using Apache POI. Apache POI library in Java: ApachePOI is an abbreviation for Poor Obfuscation Implementation, a Java API for reading and writing Microsoft documents. It includes the Wordbook, Sheet, Row, and Cell\u2026","rel":"","context":"In "MS Excel"","block_context":{"text":"MS Excel","link":"https:\/\/sheetstips.com\/category\/ms-excel\/"},"img":{"alt_text":"How to Write Data from HashMap to Excel using Java in Apache POI","src":"https:\/\/i0.wp.com\/sheetstips.com\/wp-content\/uploads\/2022\/07\/How-to-Write-Data-from-HashMap-to-Excel-using-Java-in-Apache-POI.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3848,"url":"https:\/\/sheetstips.com\/java-program-to-create-blank-excel-sheet\/","url_meta":{"origin":3898,"position":1},"title":"Java Program to Create blank Excel Sheet","author":"David","date":"November 29, 2022","format":false,"excerpt":"In this article, let us see how to create a blank excel sheet using java. Here we make use of the Apache POI library in Java for this purpose. Classes Used: XSSFWorkbook: XSSFWorkbook. It is a class that represents both high-level and low-level Excel file formats. It is part of\u2026","rel":"","context":"In "MS Excel"","block_context":{"text":"MS Excel","link":"https:\/\/sheetstips.com\/category\/ms-excel\/"},"img":{"alt_text":"Java Program to Create blank Excel Sheet","src":"https:\/\/i0.wp.com\/sheetstips.com\/wp-content\/uploads\/2022\/07\/Java-Program-to-Create-blank-Excel-Sheet.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3889,"url":"https:\/\/sheetstips.com\/how-to-create-formula-cell-in-excel-sheet-using-java-and-apache-poi\/","url_meta":{"origin":3898,"position":2},"title":"How to Create Formula Cell in Excel Sheet using Java and Apache POI?","author":"David","date":"July 10, 2022","format":false,"excerpt":"In this post, let us look at how to create a formula cell in an excel sheet using Apache POI. Here, we will create an Excel file with three columns of values, and the last column will be the formula cell, which will be calculated from the other cells by\u2026","rel":"","context":"In "MS Excel"","block_context":{"text":"MS Excel","link":"https:\/\/sheetstips.com\/category\/ms-excel\/"},"img":{"alt_text":"How to Create Formula Cell in Excel Sheet using Java and Apache POI","src":"https:\/\/i0.wp.com\/sheetstips.com\/wp-content\/uploads\/2022\/07\/How-to-Create-Formula-Cell-in-Excel-Sheet-using-Java-and-Apache-POI.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3854,"url":"https:\/\/sheetstips.com\/creating-a-cell-at-specific-position-in-excel-file-using-java\/","url_meta":{"origin":3898,"position":3},"title":"How to Create a Cell at Specific Position in Excel file using Java","author":"David","date":"July 10, 2022","format":false,"excerpt":"In this article, let us look at how to create a cell at a specific position in an excel file using Java. Apache POI library in Java: ApachePOI is an abbreviation for Poor Obfuscation Implementation, a Java API for reading and writing Microsoft documents. It includes the Wordbook, Sheet, Row,\u2026","rel":"","context":"In "MS Excel"","block_context":{"text":"MS Excel","link":"https:\/\/sheetstips.com\/category\/ms-excel\/"},"img":{"alt_text":"How to Create a Cell at Specific Position in Excel file using Java","src":"https:\/\/i0.wp.com\/sheetstips.com\/wp-content\/uploads\/2022\/07\/How-to-Create-a-Cell-at-Specific-Position-in-Excel-file-using-Java.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3942,"url":"https:\/\/sheetstips.com\/how-to-add-hyperlink-to-the-contents-of-a-cell-using-java\/","url_meta":{"origin":3898,"position":4},"title":"How to Add Hyperlink to the Contents of a Cell In Excel using Java?","author":"David","date":"July 12, 2022","format":false,"excerpt":"Using Java and Apache POI, we can add a hyperlink to the cell content in an Excel File. Apache POI is a Java library for handling Microsoft Office documents. Apache POI library in Java: ApachePOI is an abbreviation for Poor Obfuscation Implementation, a Java API for reading and writing Microsoft\u2026","rel":"","context":"In "MS Excel"","block_context":{"text":"MS Excel","link":"https:\/\/sheetstips.com\/category\/ms-excel\/"},"img":{"alt_text":"How to Add Hyperlink to the Contents of a Cell In Excel using Java","src":"https:\/\/i0.wp.com\/sheetstips.com\/wp-content\/uploads\/2022\/07\/How-to-Add-Hyperlink-to-the-Contents-of-a-Cell-In-Excel-using-Java.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3944,"url":"https:\/\/sheetstips.com\/how-to-apply-fonts-to-the-contents-of-a-cell-using-java\/","url_meta":{"origin":3898,"position":5},"title":"How to Apply Fonts to the Contents of a Cell Using Java?","author":"David","date":"July 12, 2022","format":false,"excerpt":"In this post, let us look at how to use Java and Apache POI to apply the custom font and numerous styles connected with it. A Java API is very useful to handle Microsoft Documents. Apache POI library in Java: ApachePOI is an abbreviation for Poor Obfuscation Implementation, a Java\u2026","rel":"","context":"In "MS Excel"","block_context":{"text":"MS Excel","link":"https:\/\/sheetstips.com\/category\/ms-excel\/"},"img":{"alt_text":"How to Apply Fonts to the Contents of a Cell Using Java","src":"https:\/\/i0.wp.com\/sheetstips.com\/wp-content\/uploads\/2022\/07\/How-to-Apply-Fonts-to-the-Contents-of-a-Cell-Using-Java.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/sheetstips.com\/wp-json\/wp\/v2\/posts\/3898"}],"collection":[{"href":"https:\/\/sheetstips.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sheetstips.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sheetstips.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/sheetstips.com\/wp-json\/wp\/v2\/comments?post=3898"}],"version-history":[{"count":8,"href":"https:\/\/sheetstips.com\/wp-json\/wp\/v2\/posts\/3898\/revisions"}],"predecessor-version":[{"id":4898,"href":"https:\/\/sheetstips.com\/wp-json\/wp\/v2\/posts\/3898\/revisions\/4898"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sheetstips.com\/wp-json\/wp\/v2\/media\/4028"}],"wp:attachment":[{"href":"https:\/\/sheetstips.com\/wp-json\/wp\/v2\/media?parent=3898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sheetstips.com\/wp-json\/wp\/v2\/categories?post=3898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sheetstips.com\/wp-json\/wp\/v2\/tags?post=3898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}