bugfix for ExcelUtils excel line reader
[cdmlib.git] / cdmlib-commons / src / main / java / eu / etaxonomy / cdm / common / ExcelUtils.java
index 140fcb648b67a4dd83bc058a41202338d55363ce..c03f695f39de8e18db996933380e1dbaa780ad3a 100644 (file)
 
 package eu.etaxonomy.cdm.common;
 
-import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.net.URI;
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.io.FileNotFoundException;
 
 import org.apache.log4j.Logger;
 import org.apache.poi.hssf.usermodel.HSSFCell;
@@ -31,14 +31,25 @@ public class ExcelUtils {
        private static final Logger logger = Logger.getLogger(ExcelUtils.class);
        
     /** Reads all rows of an Excel worksheet */
-    public static ArrayList<HashMap<String, String>> parseXLS(String fileName) throws FileNotFoundException {
+    public static ArrayList<HashMap<String, String>> parseXLS(URI uri) throws FileNotFoundException {
+       return parseXLS(uri, null);
+    }
+
+    
+       /** Reads all rows of an Excel worksheet */
+    public static ArrayList<HashMap<String, String>> parseXLS(URI uri, String worksheetName) throws FileNotFoundException {
        
        ArrayList<HashMap<String, String>> recordList = new ArrayList<HashMap<String, String>>();
 
        try {
-               POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName));
+               POIFSFileSystem fs = new POIFSFileSystem(UriUtils.getInputStream(uri));
                HSSFWorkbook wb = new HSSFWorkbook(fs);
-               HSSFSheet sheet = wb.getSheetAt(0);
+               HSSFSheet sheet;
+               if (worksheetName == null){
+                       sheet = wb.getSheetAt(0);       
+               }else{
+                       sheet = wb.getSheet(worksheetName);
+               }
                HSSFRow row;
                HSSFCell cell;
 
@@ -61,7 +72,6 @@ public class ExcelUtils {
                                }
                        }
                }
-               HashMap<String, String> headers = null;
                ArrayList<String> columns = new ArrayList<String>();
                row = sheet.getRow(0);
                for (int c = 0; c < cols; c++){
@@ -75,12 +85,11 @@ public class ExcelUtils {
                }
                for(int r = 1; r < rows; r++) {
                        row = sheet.getRow(r);
-                       headers = new HashMap<String, String>();
+                       HashMap<String, String> headers = new HashMap<String, String>();
                        boolean notEmpty = false;
-                       for (int j = 0; j<row.getRowNum(); j++){
+                       for (int j = 0; j<row.getLastCellNum(); j++){
                                if (row.getCell(j) != null){
                                        notEmpty = true;
-                                       break;
                                }
                        }
                        if(row != null && notEmpty) {
@@ -101,7 +110,7 @@ public class ExcelUtils {
                        recordList.add(headers);
                }
        } catch(FileNotFoundException fne) {
-               throw new FileNotFoundException(fileName);
+               throw new FileNotFoundException(uri.toString());
        } catch(Exception ioe) {
                logger.error("Error reading the Excel file.");
                ioe.printStackTrace();