Home » excel » Excel data used in Java?

Excel data used in Java?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I made a randomly generated list of people with names, ages, weight, height, etc. in Excel. I would like to know how I can use the information in Excel to create a “Person” in Java so that each file would contain the information of each person created in Excel. There are about 200,000 “People”. For example a Java template: “name = [INSERT FROM EXCEL CELL A1]” then goes to “age = [INSERT FROM EXCEL CELL A2]” and it does this for the first row then saves the file and uses the empty variable template again and does this for the 200,000 individuals. Is this possible or will I have to do this by hand? Can this be automated?

How to&Answers:

You can use opencsv for convert each row to bean. You need to save your excel as a csv file.

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));

ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
strat.setType(Person.class);

// the fields to bind do in your JavaBean
String[] columns = new String[] {"name", "age"}; 
strat.setColumnMapping(columns);

CsvToBean csv = new CsvToBean();
List list = csv.parse(strat, reader); // the people

Answer:

Save the excel file in CSV format. CSV is relatively easy to read and write, and is an industry recognized format.

Answer:

Save your excel file as .xml and then parsing that data in java. As others have said you can also save it as a .csv file which you can also parse in java.