List of usage examples for org.apache.poi.hssf.usermodel HSSFDataFormatter HSSFDataFormatter
public HSSFDataFormatter(Locale locale)
From source file:org.apache.ofbiz.pricat.sample.SamplePricatParser.java
License:Apache License
/** * Parse pricat excel file in xlsx format. * /*w ww . jav a2 s. co m*/ */ public void parsePricatExcel(boolean writeFile) { XSSFWorkbook workbook = null; try { // 1. read the pricat excel file FileInputStream is = new FileInputStream(pricatFile); // 2. use POI to load this bytes report.print(UtilProperties.getMessage(resource, "ParsePricatFileStatement", new Object[] { pricatFile.getName() }, locale), InterfaceReport.FORMAT_DEFAULT); try { workbook = new XSSFWorkbook(is); report.println(UtilProperties.getMessage(resource, "ok", locale), InterfaceReport.FORMAT_OK); } catch (IOException e) { report.println(e); report.println(UtilProperties.getMessage(resource, "PricatSuggestion", locale), InterfaceReport.FORMAT_ERROR); return; } catch (POIXMLException e) { report.println(e); report.println(UtilProperties.getMessage(resource, "PricatSuggestion", locale), InterfaceReport.FORMAT_ERROR); return; } // 3. only first sheet will be parsed // 3.1 verify the file has a sheet at least formatter = new HSSFDataFormatter(locale); isNumOfSheetsOK(workbook); // 3.2 verify the version is supported XSSFSheet sheet = workbook.getSheetAt(0); if (!isVersionSupported(sheet)) { return; } // 3.3 get currencyId existsCurrencyId(sheet); // 3.4 verify the table header row is just the same as column names, if not, print error and return if (!isTableHeaderMatched(sheet)) { return; } // 3.5 verify the first table has 6 rows at least containsDataRows(sheet); if (UtilValidate.isNotEmpty(errorMessages)) { report.println(UtilProperties.getMessage(resource, "HeaderContainsError", locale), InterfaceReport.FORMAT_ERROR); return; } // 4. parse data // 4.1 parse row by row and store the contents into database parseRowByRow(sheet); if (UtilValidate.isNotEmpty(errorMessages)) { report.println(UtilProperties.getMessage(resource, "DataContainsError", locale), InterfaceReport.FORMAT_ERROR); if (writeFile) { sequenceNum = report.getSequenceNum(); writeCommentsToFile(workbook, sheet); } } // 5. clean up the log files and commented Excel files cleanupLogAndCommentedExcel(); } catch (IOException e) { report.println(e); Debug.logError(e, module); } finally { if (UtilValidate.isNotEmpty(fileItems)) { // remove tmp files FileItem fi = null; for (int i = 0; i < fileItems.size(); i++) { fi = fileItems.get(i); fi.delete(); } } if (workbook != null) { try { workbook.close(); } catch (IOException e) { Debug.logError(e, module); } } } }
From source file:org.talend.dataprep.schema.xls.XlsUtils.java
License:Open Source License
/** * Return the numeric value.// www . ja v a 2 s .c o m * * @param cell the cell to extract the value from. * @return the numeric value from the cell. */ private static String getNumericValue(Cell cell, CellValue cellValue, boolean fromFormula) { // Date is typed as numeric if (HSSFDateUtil.isCellDateFormatted(cell)) { // TODO configurable?? DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH); return sdf.format(cell.getDateCellValue()); } // Numeric type (use data formatter to get number format right) DataFormatter formatter = new HSSFDataFormatter(Locale.ENGLISH); if (cellValue == null) { return formatter.formatCellValue(cell); } return fromFormula ? cellValue.formatAsString() : formatter.formatCellValue(cell); }