diff --git a/common/source/java/ch/systemsx/cisd/common/parser/ExcelRowTokenizer.java b/common/source/java/ch/systemsx/cisd/common/parser/ExcelRowTokenizer.java index 9d9ceab0ca1a4d8111f69bcc4c6610fe097f92a9..4ba1ff38fb24f4e0d3a0ee0fdcc7a27b9f8aff58 100644 --- a/common/source/java/ch/systemsx/cisd/common/parser/ExcelRowTokenizer.java +++ b/common/source/java/ch/systemsx/cisd/common/parser/ExcelRowTokenizer.java @@ -19,6 +19,7 @@ package ch.systemsx.cisd.common.parser; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.util.CellReference; +import org.apache.poi.ss.util.NumberToTextConverter; import ch.systemsx.cisd.common.shared.basic.utils.StringUtils; @@ -86,7 +87,7 @@ public class ExcelRowTokenizer implements ILineTokenizer<Row> case Cell.CELL_TYPE_BOOLEAN: return Boolean.toString(cell.getBooleanCellValue()); case Cell.CELL_TYPE_NUMERIC: - return Double.toString(cell.getNumericCellValue()); + return NumberToTextConverter.toText(cell.getNumericCellValue()); case Cell.CELL_TYPE_STRING: return cell.getStringCellValue(); case Cell.CELL_TYPE_FORMULA: diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/parser/ExcelRowTokenizerTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/parser/ExcelRowTokenizerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..e43211d7a236d03feb532bd00dcd83f0c272a9ad --- /dev/null +++ b/common/sourceTest/java/ch/systemsx/cisd/common/parser/ExcelRowTokenizerTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2011 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.parser; + +import java.io.InputStream; +import java.util.Arrays; +import java.util.List; + +import org.apache.commons.io.IOUtils; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.apache.poi.ss.usermodel.Row; +import org.testng.AssertJUnit; +import org.testng.annotations.Test; + +/** + * @author Kaloyan Enimanev + */ +public class ExcelRowTokenizerTest extends AssertJUnit +{ + @Test + public void testIntegerValuesParsedCorrectly() throws Exception + { + List<Row> row = getRows(); + + String[] line1 = ExcelRowTokenizer.tokenizeRow(row.get(0)); + assertEquals("4980", line1[0]); + assertEquals("TEST_STRING", line1[1]); + assertEquals("yes", line1[2]); + + String[] line2 = ExcelRowTokenizer.tokenizeRow(row.get(1)); + assertEquals("12.3", line2[0]); + assertEquals("Meh blah & so on", line2[1]); + assertEquals("no", line2[2]); + + } + + private List<Row> getRows() throws Exception + { + final InputStream stream = getClass().getResourceAsStream("excel-row-tokenizer-test.xls"); + try + { + POIFSFileSystem poifsFileSystem = new POIFSFileSystem(stream); + HSSFWorkbook workbook = new HSSFWorkbook(poifsFileSystem); + final HSSFSheet sheet = workbook.getSheetAt(0); + return Arrays.<Row> asList(sheet.getRow(0), sheet.getRow(1)); + } finally + { + IOUtils.closeQuietly(stream); + } + } + +} diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-row-tokenizer-test.xls b/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-row-tokenizer-test.xls new file mode 100644 index 0000000000000000000000000000000000000000..9f9856b9cd84cd5aae9400a7b414f0e5e8fa01dd Binary files /dev/null and b/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-row-tokenizer-test.xls differ