I need to read the data from Excel sheet and need to convert the data in to key value pair.
I have written the below code.
Here is my code:
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class XLSXReaderExample {
public static void main(String[] args) {
try {
File file = new File("C:\\demo\\employee.xlsx"); //creating a new file instance
FileInputStream fis = new FileInputStream(file); //obtaining bytes from the file
//creating Workbook instance that refers to .xlsx file
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0); //creating a Sheet object to retrieve object
Iterator<Row> itr = sheet.iterator(); //iterating over excel file
while (itr.hasNext()){
Row row = itr.next();
Iterator<Cell> cellIterator = row.cellIterator(); //iterating over each column
while (cellIterator.hasNext()){
Cell cell = cellIterator.next();
switch (cell.getCellType()){
case Cell.CELL_TYPE_STRING: //field that represents string cell type
System.out.print(cell.getStringCellValue() + "\t\t\t");
break;
case Cell.CELL_TYPE_NUMERIC: //field that represents number cell type
System.out.print(cell.getNumericCellValue() + "\t\t\t");
break;
case Cell.CELL_TYPE_Date: //field that represents Date cell type
System.out.print(cell.getDateCellValue() + "\t\t\t");
break;
default:
}
}
System.out.println("");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
I get output as below:
Employee ID Employee Name Salary Designation Department
1223.0 Harsh Marketing Manager Marketing
3213.0 Vivek 15000.0 Financial Advisor Finance
However, I need the output as, all the header columns should come in the map key and the corresponding data should come as value. and one more thing is I should not hardcode the column names, I need to read from Excel dynamically each time the column headers can be different.
I need to set my data something like below format kindly help. Map<String, List<String>> myMaps = new HashMap<String, List<String>>();
As wrote in comment, since you cannot know if a cell contains header value or a data value what you want to do is impossible.
At first sight you have 2 way to achieve what you want:
<H> </H>
, but of course if the files are used in visualization mode, this solution is not the bestEDIT:
You added a comment where confirm that the header is always at first line, and in file there is only 1 table, so the next code should work.
public static void main(String[] args) {
try {
File file = new File("C:\\demo\\employee.xlsx"); //creating a new file instance
FileInputStream fis = new FileInputStream(file); //obtaining bytes from the file
//creating Workbook instance that refers to .xlsx file
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0); //creating a Sheet object to retrieve object
Iterator<Row> itr = sheet.iterator(); //iterating over excel file
// CAREFUL HERE! use LinkedHashMap to guarantee the insertion order!
Map<String, List<String>> myMap = new LinkedHashMap<>();
// populate map with headers and empty list
if (itr.hasNext()) {
Row row = itr.next();
Iterator<Cell> headerIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
myMap.put(getCellValue(cell), new ArrayList<>());
}
}
Iterator<List<String>> columnsIterator;
// populate lists
while (itr.hasNext()) {
// get the list iterator every row to start from first list
columnsIterator = myMap.values().iterator();
Row row = itr.next();
Iterator<Cell> cellIterator = row.cellIterator(); //iterating over each column
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
// here don't check hasNext() because if the file not contains problems
// the # of columns is same as # of headers
columnsIterator.next().add(getCellValue(cell));
}
}
// here your map should be filled with data as expected
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getCellValue(Cell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING: //field that represents string cell type
return cell.getStringCellValue() + "\t\t\t";
case Cell.CELL_TYPE_NUMERIC: //field that represents number cell type
return cell.getNumericCellValue() + "\t\t\t";
case Cell.CELL_TYPE_Date: //field that represents Date cell type
return cell.getDateCellValue() + "\t\t\t";
default:
return "";
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.