View Javadoc
1   /*
2    * The baseCode project
3    * 
4    * Copyright (c) 2007-2019 University of British Columbia
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   */
19  package ubic.basecode.io.excel;
20  
21  import org.apache.poi.hssf.usermodel.*;
22  import org.apache.poi.poifs.filesystem.POIFSFileSystem;
23  import org.apache.poi.ss.usermodel.CellType;
24  
25  import java.io.FileInputStream;
26  import java.io.IOException;
27  import java.util.HashSet;
28  import java.util.LinkedList;
29  import java.util.List;
30  import java.util.Set;
31  
32  /**
33   * Utilities for dealign with Microsoft Excel spreadsheets as implemented in commons-poi.
34   * 
35   * @author lfrench
36   * 
37   */
38  public class ExcelUtil {
39  
40      /**
41       * @param filename
42       * @param sheetName
43       * @return
44       * @throws IOException
45       */
46      @SuppressWarnings("resource")
47      public static HSSFSheet getSheetFromFile( String filename, String sheetName ) throws IOException {
48          POIFSFileSystem fs = new POIFSFileSystem( new FileInputStream( filename ) );
49          HSSFWorkbook wb = new HSSFWorkbook( fs );
50          return wb.getSheet( sheetName );
51      }
52  
53      /**
54       * @param sheet
55       * @param row
56       * @param col
57       * @return
58       */
59      public static String getValue( HSSFSheet sheet, int row, int col ) {
60          if ( col > 255 ) {
61              throw new RuntimeException( "Column position is over 255" );
62          }
63          if ( sheet.getRow( row ) == null ) return null;
64          HSSFCell cell = sheet.getRow( row ).getCell( col );
65          if ( cell == null ) {
66              return null;
67          }
68  
69          if ( cell.getCellType() == CellType.STRING ) return cell.getRichStringCellValue().getString();
70  
71          if ( cell.getCellType() == CellType.NUMERIC ) {
72              // WARNING not ideal for numbers.
73              return Double.toString( cell.getNumericCellValue() );
74          }
75          if ( cell.getCellType() == CellType.FORMULA ) return cell.getCellFormula();
76  
77          return "";
78      }
79  
80      /**
81       * @param sheet
82       * @param column
83       * @param header
84       * @param clean
85       * @return
86       */
87      public static Set<String> grabColumnValues( HSSFSheet sheet, int column, boolean header, boolean clean ) {
88          return new HashSet<String>( grabColumnValuesList( sheet, column, header, clean ) );
89      }
90  
91      /**
92       * Gets all the strings from a column, possibly excluding header and possibly trimming and lowercasing
93       * 
94       * @param sheet
95       * @param column
96       * @param header true if it has a header
97       * @param clean if true it will trim and lowercase the strings
98       * @return
99       */
100     public static Set<String> grabColumnValues( HSSFSheet sheet, int column, boolean header, boolean clean,
101             SpreadSheetFilter f ) {
102         return new HashSet<String>( grabColumnValuesList( sheet, column, header, clean, f ) );
103     }
104 
105     /**
106      * @param sheet
107      * @param column
108      * @param header
109      * @param clean
110      * @return
111      */
112     public static List<String> grabColumnValuesList( HSSFSheet sheet, int column, boolean header, boolean clean ) {
113         return grabColumnValuesList( sheet, column, header, clean, new SpreadSheetFilter() {
114             @Override
115             public boolean accept( HSSFSheet s, int row ) {
116                 return true;
117             }
118         } );
119     }
120 
121     /**
122      * @param sheet
123      * @param column the index of the column to get
124      * @param header if there is a header row to be skipped
125      * @param clean lower case
126      * @param f
127      * @return
128      */
129     public static List<String> grabColumnValuesList( HSSFSheet sheet, int column, boolean header, boolean clean,
130             SpreadSheetFilter f ) {
131         List<String> result = new LinkedList<String>();
132 
133         int rows = sheet.getLastRowNum() + 1;
134         for ( int i = 0; i < rows; i++ ) {
135             if ( header && i == 0 ) continue;
136             String term = ExcelUtil.getValue( sheet, i, column );
137             if ( term == null ) continue;
138 
139             if ( f.accept( sheet, i ) ) {
140                 term = term.trim();
141                 if ( clean ) term = term.toLowerCase();
142                 result.add( term );
143             }
144         }
145         return result;
146     }
147 
148     public static void main( String args[] ) {
149         HSSFWorkbook workbook = new HSSFWorkbook();
150         HSSFSheet spreadsheet = workbook.createSheet();
151         ExcelUtil.setFormula( spreadsheet, 1, 1, "HYPERLINK(\"x\",\"x\")" );
152 
153     }
154 
155     /**
156      * @param sheet
157      * @param row
158      * @param col
159      * @param value
160      */
161     public static void setFormula( HSSFSheet sheet, int row, int col, String value ) {
162         HSSFRow r = sheet.getRow( row );
163         if ( r == null ) {
164             r = sheet.createRow( row );
165         }
166         HSSFCell c = r.createCell( col );
167         c.setCellFormula( value );
168     }
169 
170     /**
171      * @param sheet
172      * @param row
173      * @param col
174      * @param value
175      */
176     public static void setValue( HSSFSheet sheet, int row, int col, double value ) {
177         HSSFRow r = sheet.getRow( row );
178         if ( r == null ) {
179             r = sheet.createRow( row );
180         }
181         HSSFCell c = r.createCell( col );
182         c.setCellType( CellType.NUMERIC );
183         c.setCellValue( value );
184 
185     }
186 
187     /**
188      * @param sheet
189      * @param row
190      * @param col
191      * @param value
192      */
193     public static void setValue( HSSFSheet sheet, int row, int col, int value ) {
194         setValue( sheet, row, col, ( double ) value );
195     }
196 
197     /**
198      * @param sheet
199      * @param row
200      * @param col
201      * @param value
202      */
203     public static void setValue( HSSFSheet sheet, int row, int col, String value ) {
204         HSSFRow r = sheet.getRow( row );
205         if ( r == null ) {
206             r = sheet.createRow( row );
207         }
208         HSSFCell c = r.createCell( col );
209         c.setCellType( CellType.STRING );
210         c.setCellValue( new HSSFRichTextString( value ) );
211     }
212 }