001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.jexcelapi;
025    
026    import java.io.File;
027    import java.io.IOException;
028    import java.io.InputStream;
029    
030    import jxl.Cell;
031    import jxl.NumberCell;
032    import jxl.Sheet;
033    import jxl.Workbook;
034    import jxl.read.biff.BiffException;
035    
036    import org.ujmp.core.Matrix;
037    import org.ujmp.core.MatrixFactory;
038    import org.ujmp.core.enums.ValueType;
039    import org.ujmp.core.exceptions.MatrixException;
040    import org.ujmp.core.util.MathUtil;
041    
042    public abstract class ImportMatrixXLS {
043    
044            public static final Matrix fromFile(File file, Object... parameters)
045                            throws MatrixException, IOException {
046                    try {
047                            int sheetNr = 0;
048                            if (parameters != null && parameters.length > 0) {
049                                    sheetNr = MathUtil.getInt(parameters[0]);
050                            }
051                            Workbook workbook = Workbook.getWorkbook(file);
052                            Sheet sheet = workbook.getSheet(sheetNr);
053    
054                            int rows = sheet.getRows();
055                            int columns = sheet.getColumns();
056                            Matrix matrix = MatrixFactory
057                                            .zeros(ValueType.OBJECT, rows, columns);
058                            for (int row = 0; row < rows; row++) {
059                                    for (int column = 0; column < columns; column++) {
060                                            Cell c = sheet.getCell(column, row);
061                                            Object o = null;
062                                            if (c instanceof NumberCell) {
063                                                    o = ((NumberCell) c).getValue();
064                                            } else {
065                                                    o = c.getContents();
066                                            }
067                                            matrix.setAsObject(o, row, column);
068                                    }
069                            }
070                            workbook.close();
071                            return matrix;
072                    } catch (BiffException e) {
073                            throw new MatrixException("could not import from file " + file, e);
074                    }
075            }
076    
077            public static final Matrix fromStream(InputStream inputStream,
078                            Object... parameters) throws IOException, MatrixException {
079                    try {
080                            Workbook workbook = Workbook.getWorkbook(inputStream);
081                            Sheet sheet = workbook.getSheet(0);
082                            int rows = sheet.getRows();
083                            int columns = sheet.getColumns();
084                            Matrix matrix = MatrixFactory
085                                            .zeros(ValueType.OBJECT, rows, columns);
086                            for (int row = 0; row < rows; row++) {
087                                    for (int column = 0; column < columns; column++) {
088                                            Cell c = sheet.getCell(column, row);
089                                            Object o = null;
090                                            if (c instanceof NumberCell) {
091                                                    o = ((NumberCell) c).getValue();
092                                            } else {
093                                                    o = c.getContents();
094                                            }
095                                            matrix.setAsObject(o, row, column);
096                                    }
097                            }
098                            workbook.close();
099                            return matrix;
100                    } catch (BiffException e) {
101                            throw new MatrixException("could not import from stream", e);
102                    }
103            }
104    
105    }