3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.cdm
.common
;
13 import java
.io
.FileInputStream
;
14 import java
.util
.ArrayList
;
15 import java
.util
.HashMap
;
16 import java
.io
.FileNotFoundException
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.apache
.poi
.hssf
.usermodel
.HSSFCell
;
20 import org
.apache
.poi
.hssf
.usermodel
.HSSFRow
;
21 import org
.apache
.poi
.hssf
.usermodel
.HSSFSheet
;
22 import org
.apache
.poi
.hssf
.usermodel
.HSSFWorkbook
;
23 import org
.apache
.poi
.poifs
.filesystem
.POIFSFileSystem
;
30 public class ExcelUtils
{
31 private static final Logger logger
= Logger
.getLogger(ExcelUtils
.class);
33 /** Reads all rows of an Excel worksheet */
34 public static ArrayList
<HashMap
<String
, String
>> parseXLS(String fileName
) throws FileNotFoundException
{
36 ArrayList
<HashMap
<String
, String
>> recordList
= new ArrayList
<HashMap
<String
, String
>>();
39 POIFSFileSystem fs
= new POIFSFileSystem(new FileInputStream(fileName
));
40 HSSFWorkbook wb
= new HSSFWorkbook(fs
);
41 HSSFSheet sheet
= wb
.getSheetAt(0);
45 int rows
; // No of rows
46 rows
= sheet
.getPhysicalNumberOfRows();
47 if(logger
.isDebugEnabled()) { logger
.debug("Number of rows: " + rows
); }
49 int cols
= 0; // No of columns
52 // This trick ensures that we get the data properly even if it doesn't start from first few rows
53 for(int i
= 0; i
< 10 || i
< rows
; i
++) {
54 row
= sheet
.getRow(i
);
58 tmp
= sheet
.getRow(i
).getPhysicalNumberOfCells();
64 HashMap
<String
, String
> headers
= null;
65 ArrayList
<String
> columns
= new ArrayList
<String
>();
66 row
= sheet
.getRow(0);
67 for (int c
= 0; c
< cols
; c
++){
68 cell
= row
.getCell(c
);
70 columns
.add(cell
.toString());
71 if(logger
.isDebugEnabled()) { logger
.debug("Cell #" + c
+ ": " + cell
.toString()); }
73 if(logger
.isDebugEnabled()) { logger
.debug("Cell #" + c
+ " is null"); }
76 for(int r
= 1; r
< rows
; r
++) {
77 row
= sheet
.getRow(r
);
78 headers
= new HashMap
<String
, String
>();
79 boolean notEmpty
= false;
80 for (int j
= 0; j
<row
.getRowNum(); j
++){
81 if (row
.getCell(j
) != null){
86 if(row
!= null && notEmpty
) {
87 for(int c
= 0; c
< cols
; c
++) {
88 cell
= row
.getCell((short)c
);
90 if (c
>= columns
.size()){
91 logger
.warn("Cell has no header. There are only " + columns
.size() + " headers but more not-null cells in approx. row " + row
.getRowNum() + ". Cell is neglected.");
93 if(logger
.isDebugEnabled()) { logger
.debug("Cell #" + c
+ ": " + cell
.toString()); }
94 headers
.put(columns
.get(c
), cell
.toString());
97 if(logger
.isDebugEnabled()) { logger
.debug("Cell #" + c
+ " is null"); }
101 recordList
.add(headers
);
103 } catch(FileNotFoundException fne
) {
104 throw new FileNotFoundException(fileName
);
105 } catch(Exception ioe
) {
106 logger
.error("Error reading the Excel file.");
107 ioe
.printStackTrace();