Open In App

How to Write Data from HashMap to Excel using Java in Apache POI?

Improve
Improve
Like Article
Like
Save
Share
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.

HashMap

 

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 Explanation

  • In 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.

Output

 

Output

 



Last Updated : 12 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads