Tuesday, February 19, 2013

Reading an XLS file with Java and Apache POI

A class I wrote that reads an excel XLS file and returns a Collection of some Object. The following jars maybe all be necessary:
  • poi
  • poi-ooxml
  • xbean
  • poi-ooxml-schemas
  • dom4j
Below this class I will show the
--------------------------------------------------------------------------------------------------------------------------------------------------------
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class ExcelParser {

    private static Log log = LogFactory.getLog(ExcelParser.class);
  
    public static List<Product> parseSheet(File outputFilePath) {

        List sheetData = new ArrayList();
        FileInputStream file = null;

        try {

            // Create a FileInputStream that will be use to read the excel file.
            file = new FileInputStream(outputFilePath);

            // Create an excel workbook from the file system.
            Workbook workbook = new HSSFWorkbook(file);
          
            // Get the first sheet on the workbook.
            Sheet sheet = workbook.getSheetAt(0);

            // When we have a sheet object in hand we can iterator on each
            // sheet's rows and on each row's cells. We store the data read
            // on an ArrayList so that we can printed the content of the excel
            // to the console.
            Iterator rows = sheet.rowIterator();
          
            while (rows.hasNext())
            {
                Row row = (HSSFRow) rows.next();
                Iterator cells = row.cellIterator();

                List data = new ArrayList();
                while (cells.hasNext()) {
                    Cell cell = (HSSFCell) cells.next();
                    data.add(cell);
                }
                sheetData.add(data);
            }
          
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sheetData;

    }

---------------------------------------------------------------------------------------------------------------------------------------------------------


    public static void buildProductList(List sheetData, List<Product> productList ) {

        for (int i = 1; i < 5; i++) {

            List list = (List) sheetData.get(i);
            Product p = new Product();
          
            Cell cell1 = (HSSFCell) list.get(0);
            p.setProductId((long)cell1.getNumericCellValue());

            Cell cell2 = (HSSFCell) list.get(1);
            p.setCreatedOn(new Date());

            // methods
            Cell cell3 = (HSSFCell) list.get(2);
            p.setDescription(cell3.getRichStringCellValue().getString());

            injectList.add(inject);
        }
    }

No comments:

Post a Comment