1 16 17 package org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements; 18 19 import java.io.IOException ; 20 import java.text.NumberFormat ; 21 import java.text.ParseException ; 22 import java.util.Locale ; 23 24 import org.apache.cocoon.CascadingIOException; 25 26 import org.apache.poi.hssf.usermodel.HSSFCell; 27 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 28 29 35 37 class Cell { 38 private HSSFCell _cell; 39 40 private int _celltype; 42 private Locale locale; 43 44 52 Cell(final HSSFCell cell, final int cellType) { 53 _cell = cell; 54 _celltype = cellType; 55 _cell.setEncoding(HSSFCell.ENCODING_UTF_16); 56 } 57 58 63 void setLocale(Locale locale) { 64 this.locale = locale; 65 } 66 67 74 void setContent(final String content) throws IOException { 75 if (_cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { 76 try { 77 if (_celltype == CellType.CELL_TYPE_FLOAT) { 78 NumberFormat form = null; 81 if (locale == null) { 82 form = NumberFormat.getInstance(); 83 } else { 84 form = NumberFormat.getInstance(locale); 85 } 86 _cell.setCellValue(form.parse(content).doubleValue()); 87 } else { 88 _cell.setCellValue(Integer.parseInt(content)); 89 } 90 } catch (NumberFormatException e) { 91 throw new CascadingIOException("Invalid value for a numeric cell: " + content, e); 92 } catch (ParseException e) { 93 throw new CascadingIOException("Invalid value for a numeric cell: " + content, e); 94 } 95 } else if (_cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { 96 _cell.setCellValue(content); 97 } else if (_cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) { 98 _cell.setCellFormula(content.toUpperCase().substring(1)); 99 } 100 } 101 102 void setStyle(HSSFCellStyle style) { 103 if (style != null) { 104 _cell.setCellStyle(style); 105 } 106 } 107 108 111 int getCellType() { 112 return _cell.getCellType(); 113 } 114 115 118 String getStringValue() { 119 return _cell.getStringCellValue(); 120 } 121 122 125 double getNumericValue() { 126 return _cell.getNumericCellValue(); 127 } 128 } | Popular Tags |