Changeset 1087 for trunk/grails-app/services/dbnp/importer
- Timestamp:
- Nov 5, 2010, 11:55:58 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/services/dbnp/importer/ImporterService.groovy
r1083 r1087 16 16 17 17 package dbnp.importer 18 import org.apache.poi.hssf.usermodel.* 19 import org.apache.poi.poifs.filesystem.POIFSFileSystem 20 import org.apache.poi.ss.usermodel.DataFormatter 18 import org.apache.poi.ss.usermodel.* 21 19 22 20 import dbnp.studycapturing.TemplateFieldType … … 28 26 import dbnp.studycapturing.Sample 29 27 30 import dbnp.data.Term31 32 28 class ImporterService { 33 29 def AuthenticationService … … 39 35 * @return high level representation of the workbook 40 36 */ 41 HSSFWorkbook getWorkbook(InputStream is) { 42 POIFSFileSystem fs = new POIFSFileSystem(is) 43 HSSFWorkbook wb = new HSSFWorkbook(fs); 44 return wb; 37 Workbook getWorkbook(InputStream is) { 38 WorkbookFactory.create(is) 45 39 } 46 40 … … 50 44 * @return header representation as a MappingColumn hashmap 51 45 */ 52 def getHeader( HSSFWorkbook wb, int sheetindex, int headerrow, int datamatrix_start, theEntity=null){46 def getHeader(Workbook wb, int sheetindex, int headerrow, int datamatrix_start, theEntity=null){ 53 47 54 48 def sheet = wb.getSheetAt(sheetindex) … … 59 53 def property = new String() 60 54 61 //for ( HSSFCell c: sheet.getRow(datamatrix_start)) {55 //for (Cell c: sheet.getRow(datamatrix_start)) { 62 56 63 57 (0..sheetrow.getLastCellNum() -1 ).each { columnindex -> 64 58 65 59 //def index = c.getColumnIndex() 66 def datamatrix_celltype = sheet.getRow(datamatrix_start).getCell(columnindex, org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK).getCellType()60 def datamatrix_celltype = sheet.getRow(datamatrix_start).getCell(columnindex,Row.CREATE_NULL_AS_BLANK).getCellType() 67 61 def datamatrix_celldata = df.formatCellValue(sheet.getRow(datamatrix_start).getCell(columnindex)) 68 62 def datamatrix_cell = sheet.getRow(datamatrix_start).getCell(columnindex) … … 74 68 75 69 switch (datamatrix_celltype) { 76 case HSSFCell.CELL_TYPE_STRING:70 case Cell.CELL_TYPE_STRING: 77 71 //parse cell value as double 78 72 def doubleBoolean = true … … 94 88 95 89 break 96 case HSSFCell.CELL_TYPE_NUMERIC:90 case Cell.CELL_TYPE_NUMERIC: 97 91 def fieldtype = TemplateFieldType.INTEGER 98 92 def doubleBoolean = true … … 116 110 } 117 111 118 if ( HSSFDateUtil.isCellDateFormatted(datamatrix_cell)) fieldtype = TemplateFieldType.DATE112 if (DateUtil.isCellDateFormatted(datamatrix_cell)) fieldtype = TemplateFieldType.DATE 119 113 120 114 header[columnindex] = new dbnp.importer.MappingColumn(name:df.formatCellValue(headercell), … … 124 118 property:property); 125 119 break 126 case HSSFCell.CELL_TYPE_BLANK:120 case Cell.CELL_TYPE_BLANK: 127 121 header[columnindex] = new dbnp.importer.MappingColumn(name:df.formatCellValue(headercell), 128 122 templatefieldtype:TemplateFieldType.STRING, … … 150 144 * @param sheetindex sheet index used 151 145 * @param rows amount of rows returned 152 * @return two dimensional array (matrix) of HSSFCell objects146 * @return two dimensional array (matrix) of Cell objects 153 147 */ 154 148 155 HSSFCell[][] getDatamatrix(HSSFWorkbook wb, header, int sheetindex, int datamatrix_start, int count) {149 Cell[][] getDatamatrix(Workbook wb, header, int sheetindex, int datamatrix_start, int count) { 156 150 def sheet = wb.getSheetAt(sheetindex) 157 151 def rows = [] … … 164 158 165 159 // walk through every cell 166 /*for ( HSSFCell c: sheet.getRow(rowindex)) {160 /*for (Cell c: sheet.getRow(rowindex)) { 167 161 row.add(c) 168 162 println c.getColumnIndex() + "=" +c … … 170 164 171 165 (0..header.size()-1).each { columnindex -> 172 def c = sheet.getRow(rowindex).getCell(columnindex, org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK)166 def c = sheet.getRow(rowindex).getCell(columnindex, Row.CREATE_NULL_AS_BLANK) 173 167 //row.add(df.formatCellValue(c)) 174 168 row.add(c) … … 222 216 * @see dbnp.importer.MappingColumn 223 217 */ 224 def importData(template_id, HSSFWorkbook wb, int sheetindex, int rowindex, mcmap) {218 def importData(template_id, Workbook wb, int sheetindex, int rowindex, mcmap) { 225 219 def sheet = wb.getSheetAt(sheetindex) 226 220 def table = [] … … 237 231 * 238 232 * @param datamatrix two dimensional array containing entities and possibly also failed cells 239 * @return array of failed cells in [mappingcolumn, hssfcell] format233 * @return array of failed cells in [mappingcolumn, cell] format 240 234 * */ 241 235 def getFailedCells(datamatrix) { … … 259 253 * 260 254 * @param datamatrix two dimensional array containing entities and possibly also failed cells 261 * @param failedcells map of failed cells in [mappingcolumn, hssfcell] format255 * @param failedcells map of failed cells in [mappingcolumn, cell] format 262 256 * @param correctedcells map of corrected cells 263 257 **/ … … 273 267 274 268 record.each { entity -> 275 // LinkedHashMap means a "mappingcolumn: hssfcell" object is inside the record (= failed to map to entity)269 // LinkedHashMap means a "mappingcolumn:cell" object is inside the record (= failed to map to entity) 276 270 if (!entity.getClass().getName().equals('java.util.LinkedHashMap')) 277 271 newrecord.add(entity) … … 435 429 * @param mcmap map containing MappingColumn objects 436 430 */ 437 def createRecord(template_id, HSSFRow excelrow, mcmap) {431 def createRecord(template_id, Row excelrow, mcmap) { 438 432 def df = new DataFormatter() 439 433 def template = Template.get(template_id) … … 449 443 450 444 // Go through the Excel row cell by cell 451 for ( HSSFCell cell: excelrow) {445 for (Cell cell: excelrow) { 452 446 // get the MappingColumn information of the current cell 453 447 def mc = mcmap[cell.getColumnIndex()]
Note: See TracChangeset
for help on using the changeset viewer.