com.myjeeva.poi.demo.Excel2JavaDemo.java Source code

Java tutorial

Introduction

Here is the source code for com.myjeeva.poi.demo.Excel2JavaDemo.java

Source

/**
 * The MIT License
 *
 * Copyright (c) 2012 www.myjeeva.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE. 
 * 
 */
package com.myjeeva.poi.demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.util.IOUtils;

import com.myjeeva.poi.ExcelReader;
import com.myjeeva.poi.ExcelWorkSheetHandler;
import com.myjeeva.poi.demo.vo.PersonVO;

/**
 * Demonstration of Generic Excel File (XLSX) Reading using Apache POI
 * 
 * @author <a href="mailto:jeeva@myjeeva.com">Jeevanandam Madanagopal</a>
 */
public class Excel2JavaDemo {
    private static final Log LOG = LogFactory.getLog(Excel2JavaDemo.class);

    /**
     * @param args
     * @throws FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException {
        String SAMPLE_PERSON_DATA_FILE_PATH = "src/main/resources/Sample-Person-Data.xlsx";

        // Input File initialize
        File file = new File(SAMPLE_PERSON_DATA_FILE_PATH);
        InputStream inputStream = new FileInputStream(file);

        // Excel Cell Mapping
        Map<String, String> cellMapping = new HashMap<String, String>();
        cellMapping.put("HEADER", "Person Id,Name,Height,Email Address,DOB,Salary");
        cellMapping.put("A", "personId");
        cellMapping.put("B", "name");
        cellMapping.put("C", "height");
        cellMapping.put("D", "emailId");
        cellMapping.put("E", "dob");
        cellMapping.put("F", "salary");

        // The package open is instantaneous, as it should be.
        OPCPackage pkg = null;
        try {

            ExcelWorkSheetHandler<PersonVO> workSheetHandler = new ExcelWorkSheetHandler<PersonVO>(PersonVO.class,
                    cellMapping);

            pkg = OPCPackage.open(inputStream);
            ExcelReader excelReader = new ExcelReader(pkg, workSheetHandler);
            excelReader.process();

            if (workSheetHandler.getValueList().isEmpty()) {
                // No data present
                LOG.error("sHandler.getValueList() is empty");
            } else {

                LOG.info(workSheetHandler.getValueList().size()
                        + " no. of records read from given excel worksheet successfully.");

                // Displaying data ead from Excel file
                displayPersonList(workSheetHandler.getValueList());
            }

        } catch (RuntimeException are) {
            LOG.error(are.getMessage(), are.getCause());
        } catch (InvalidFormatException ife) {
            LOG.error(ife.getMessage(), ife.getCause());
        } catch (IOException ioe) {
            LOG.error(ioe.getMessage(), ioe.getCause());
        } finally {
            IOUtils.closeQuietly(inputStream);
            try {
                if (null != pkg) {
                    pkg.close();
                }
            } catch (IOException e) {
                // just ignore IO exception
            }
        }
    }

    private static void displayPersonList(List<PersonVO> persons) {
        System.out.println("Id\tName\tHeight\tEmail Address\t\tDOB\t\tSalary");
        System.out.println("--\t----\t------\t-------------\t\t---\t\t------");
        for (PersonVO p : persons) {
            System.out.println(String.format("%s\t%s\t%s\t%s\t%s\t%s", p.getPersonId(), p.getName(), p.getHeight(),
                    p.getEmailId(), p.getDob(), p.getSalary()));
        }
    }
}