How to Write Data from HashMap to Excel using Java in Apache POI?
Last Updated :
12 May, 2022
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 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.
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