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;
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;
}
}
}
- HashMap<String, String> headers = null;
ArrayList<String> columns = new ArrayList<String>();
row = sheet.getRow(0);
for (int c = 0; c < cols; c++){
}
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) {
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();