How to Write Data from HashMap to Excel using Java in Apache POI? Last Updated : 12 May, 2022 Comments Improve Suggest changes Like Article Like Report Apache POI is an open-source java library to create and manipulate various file formats based on Microsoft Office. Using POI, one should be able to perform create, modify and display/read operations on the following file formats. For Example, Java doesn’t provide built-in support for working with excel files, so we need to look for open-source APIs for the job. In this article, we are going to discuss how to write data from HashMap to Excel using the Apache POI library. HashMap is a type of collection in Java that consist of a key-value pair. Apache POI is an open-source java library that is used to perform many operations on Microsoft Excel, MS office, etc.  Let us consider the above diagram as the example of a HashMap containing the key-value pair, we have to read the data from the HashMap and write the key in a column and the value in a separate column. Pre-Requisite To work with this example, we need the following in our system: Make sure that your system has Java JDK if not installed from here.Install the Eclipse IDE from here.Create a maven project. For creating a Maven project refer to this How to Create a Selenium Maven Project with Eclipse to Open Chrome Browser? Add the dependency for Apache POI and Apache-ooxml from the MVN repository to the pom.xml file.  Copy this dependency from the MVN repository and paste them into the POM.xml file Now we are going to create an Excel file using Apache POI and create a HashMap in java and add the student's data to the Map. After that, we are going to write the student data from the HashMap to the Excel file. Let's recreate this example in the coding. Program for Writing data from HashMap to Excel using Apache POI Java import org.testng.annotations.Test; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class Geeks { @Test public void geekforgeeks() throws IOException{ XSSFWorkbook workbook=new XSSFWorkbook(); XSSFSheet sheet=workbook.createSheet("sheet1"); // Creating HashMap and entering data HashMap<String,String> map=new HashMap<String,String>(); map.put("1", "John"); map.put("2", "Joey"); map.put("3", "Ross"); map.put("4", "Rachel"); int rowno=0; for(HashMap.Entry entry:map.entrySet()) { XSSFRow row=sheet.createRow(rowno++); row.createCell(0).setCellValue((String)entry.getKey()); row.createCell(1).setCellValue((String)entry.getValue()); } FileOutputStream file = new FileOutputStream("C:\\Users\\ADMIN\\Desktop\\Hash.xlsx"); workbook.write(file); file.close(); System.out.println("Data Copied to Excel"); } } Code ExplanationIn the initial step, we have to create the workbook and sheet for that workbook. XSSFWorkbook workbook=new XSSFWorkbook();XSSFSheet sheet=workbook.createSheet("sheet1"); Now create the HashMap and put the data into the Map.Declaring the Row number to zero.Iterate through the HashMap and create the row.Now set the value of the cell, and get the Key and the Value from the HashMap. row.createCell(0).setCellValue((String)entry.getKey()); row.createCell(1).setCellValue((String)entry.getValue()); After that create the File output Stream and specify the location for creating the Excel and write the data into it.Output After the code is executed, the console print the output as "Data copied to Excel" and an Excel file "Hash.xlsx"  is created in the defined location in your system.   Comment More infoAdvertise with us Next Article How to Write Data from HashMap to Excel using Java in Apache POI? A allwink45 Follow Improve Article Tags : Java Software Testing selenium Apache-POI Practice Tags : Java Similar Reads How to Write Data from Excel File into a HashMap using Java and Apache POI? Apache POI is an open-source java library to create and manipulate various file formats based on Microsoft Office. Using POI, one should be able to perform create, modify and display/read operations on the following file formats. For Example, Java doesnât provide built-in support for working with ex 3 min read Reading and Writing Data to Excel File in Java using Apache POI In Java, reading an Excel file is not similar to reading a Word file because of cells in an Excel file. JDK does not provide a direct API to read data from Excel files for which we have to toggle to a third-party library that is Apache POI. Apache POI is an open-source java library designed for read 5 min read How to Create Formula Cell in Excel Sheet using Java and Apache POI? In the previous article, we have seen how to read data from the formula cell, here we are going to create the formula cell using Apache POI.  In this article, we are creating an Excel file with three columns consisting of values and the last column is the formula cell which is calculated from the o 2 min read How to Create a Formula in Excel using Java? Apache POI is a popular open-source Java library that provides programmers with APIs for creating, modifying, and editing MS Office files. Excel is very excellent at calculating formulas. And perhaps most Excel documents have formulas embedded. Therefore, itâs trivial that on a fine day, you have to 3 min read How to Create Pivot Chart from Pivot Table in Excel using Java? A Pivot Chart is used to analyze data of a table with very little effort (and no formulas) and it gives you the big picture of your raw data. It allows you to analyze data using various types of graphs and layouts. It is considered to be the best chart during a business presentation that involves hu 4 min read How to Create Pivot Table in Excel using Java? A pivot table is needed to quickly analyze data of a table with very little effort (and no formulas) and sometimes not everyone has time to look at the data in the table and see whatâs going on and use it to build good-looking reports for large data sets in an Excel worksheet. Let's discuss a step-b 5 min read Creating Sheets in Excel File in Java using Apache POI Apache POI is an open-source java library to create and manipulate various file formats based on Microsoft Office. Using POI, one should be able to perform create, modify and display/read operations on the following file formats. For Example, java doesnât provide built-in support for working with ex 3 min read How to Write JSON Array to CSV File using Java? We will see how to read a JSONArray from a JSON file and write the contents to a CSV file using Java. JavaScript Object Notation (JSON) is a standard text-based format for representing structured data that is based on JavaScript object syntax. It is commonly used for transmitting data in web applica 3 min read How to Read Data From Formula Cell in Excel Sheet using Selenium and Apache POI? The Apache POI is one of the most popular java libraries for selenium data-driven testing. It is mainly used for handling all types of Microsoft documents such as MS Word, and MS Excel. MS word and others. Apache POI is an open-source library and POI stands for "Poor Obfuscation Implementation". Get 2 min read How to Convert HashMap to ArrayList in Java? In Java a HashMap is a collection that stores key-value pairs on the other hand, an ArrayList is a collection that stores dynamic arrays. There are some scenarios where we need to convert a HashMap into an ArrayList such as:Extracting only the keys or values in the list form.Converting key-value pai 2 min read Like