Open In App

How to Write Data from HashMap to Excel using Java in 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 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:

For creating a Maven project refer to this How to Create a Selenium Maven Project with Eclipse to Open Chrome Browser?



 

 

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




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

XSSFWorkbook workbook=new XSSFWorkbook();
XSSFSheet sheet=workbook.createSheet(“sheet1”);

row.createCell(0).setCellValue((String)entry.getKey());

row.createCell(1).setCellValue((String)entry.getValue());

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.

 

 


Article Tags :