best possible way of mapping excel fields with java object

Viewed 6932

I am trying to map an excel field with the java object. What will be the best possible way of doing it rather then being dependent on the column number.

Cell orderId=row.getCell(0);
System.out.println("orderId" +orderId);
Cell trackingId=row.getCell(1);

Cell orderTitle = row.getCell(2);

Cell customerName = row.getCell(3);

Cell customerAddress = row.getCell(5);

How to map the column with the object variable without bieng dependent on column numbers?

i am looking for something where i can map the row header with object and the rest should be independent of the row number. Any suggestions?

3 Answers
package com.jcg.example;
 
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class WriteExcelFileExample {
    private static final String FILE_PATH = "<Path to your excel file>";
   
    private static final WriteExcelFileExample INSTANCE = new WriteExcelFileExample();
 
    public static WriteExcelFileExample getInstance() {
        return INSTANCE;
    }
 
    private WriteExcelFileExample() {
    }
 
    public static void main(String args[]){
 
        List studentList = new ArrayList();
        studentList.add(new Student("Magneto","90","100","80"));
        studentList.add(new Student("Wolverine","60","60","90"));
        studentList.add(new Student("ProfX","100","100","100"));
 
        writeStudentsListToExcel(studentList);
 
    }
 
    public static void writeStudentsListToExcel(List studentList){
 
        // Using XSSF for xlsx format, for xls use HSSF
        Workbook workbook = new XSSFWorkbook();
 
        Sheet studentsSheet = workbook.createSheet("Students");
 
        int rowIndex = 0;
        for(Student student : studentList){
            Row row = studentsSheet.createRow(rowIndex++);
            int cellIndex = 0;
            //first place in row is name
            row.createCell(cellIndex++).setCellValue(student.getName());
 
            //second place in row is marks in maths
            row.createCell(cellIndex++).setCellValue(student.getMaths());
 
            //third place in row is marks in Science
            row.createCell(cellIndex++).setCellValue(student.getScience());
 
            //fourth place in row is marks in English
            row.createCell(cellIndex++).setCellValue(student.getEnglish());
 
        }
 
        //write this workbook in excel file.
        try {
            FileOutputStream fos = new FileOutputStream(FILE_PATH);
            workbook.write(fos);
            fos.close();
 
            System.out.println(FILE_PATH + " i[enter link description here][1]s successfully written");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
 
    }
}


  [1]: https://examples.javacodegeeks.com/core-java/writeread-excel-files-in-java-example/
Related