Example usage for org.apache.poi.ss.usermodel Sheet getSheetName

List of usage examples for org.apache.poi.ss.usermodel Sheet getSheetName

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Sheet getSheetName.

Prototype

String getSheetName();

Source Link

Document

Returns the name of this sheet

Usage

From source file:com.movielabs.availslib.AvailSS.java

License:Open Source License

/**
 * Add a sheet from an Excel spreadsheet to a spreadsheet object
 * @param wb an Apache POI workbook object
 * @param sheet an Apache POI sheet object
 * @return created sheet object/*w  ww  . j a v a  2s.  c o  m*/
 */
private AvailsSheet addSheetHelper(Workbook wb, Sheet sheet) throws Exception {
    AvailsSheet as = new AvailsSheet(this, sheet.getSheetName());

    //        int qq = 0;
    for (Row row : sheet) {
        //           qq++;
        int len = row.getLastCellNum();
        if (len < 0)
            continue;
        String[] fields = new String[len];
        for (int i = 0; i < len; i++) // XXX: don't want nulls
            fields[i] = "";
        for (Cell cell : row) {
            int idx = cell.getColumnIndex();
            int type = cell.getCellType();
            switch (type) {
            case 0: // Numeric
                double v = cell.getNumericCellValue();
                if (v < 0.5) { // XXX hack: assume TotalRunTime
                    java.util.Date d = cell.getDateCellValue();
                    fields[idx] = String.format("%02d:%02d:%02d", d.getHours(), d.getMinutes(), d.getSeconds());
                    //System.out.println("run=" + tmp);
                } else {
                    fields[idx] = cell.toString();
                }
                break;
            case 1: // String
            case 3: // Blank
                fields[idx] = cell.getStringCellValue().trim();
                break;
            default:
                //logger.warn("Cell[" + i + "," + idx + "]: invalid type (" + type + ")");
                fields[idx] = cell.toString();
                break;
            }
        } /* cell */
        if (as.isAvail(fields))
            as.addRow(fields, row.getRowNum() + 1);
    } /* row */
    sheets.add(as);
    return as;
}

From source file:com.ncc.excel.ExcelUtil.java

License:Apache License

public List<Row> readExcel(Workbook wb) {
    Sheet sheet = null;

    if (onlyReadOneSheet) {//??sheet

        System.out.println("selectedSheetName:" + selectedSheetName);
        // ??sheet(?????)  
        sheet = selectedSheetName.equals("") ? wb.getSheetAt(selectedSheetIdx) : wb.getSheet(selectedSheetName);
        System.out.println(sheet.getSheetName());

    } else {/*from   www .  j  a  v  a 2s  . com*/
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {//??Sheet

            sheet = wb.getSheetAt(i);
            logger.info(sheet.getSheetName());

            for (int j = 0; j < sheet.getPhysicalNumberOfRows(); j++) {//??
                Row row = sheet.getRow(j);
                for (int k = 0; k < row.getPhysicalNumberOfCells(); k++) {//???
                    System.out.print(row.getCell(k) + "\t");
                }
                System.out.println("---Sheet" + i + "?---");
            }
        }
    }
    return null;
}

From source file:com.ncc.excel.test.ExcelUtil.java

License:Apache License

/** 
 * ?Excel //w  ww . j  a  v a2 s  . c  o  m
 *  
 * @Title: readExcel 
 * @Date : 2014-9-11 ?11:26:53 
 * @param wb 
 * @return 
 */
private List<Row> readExcel(Workbook wb) {
    List<Row> rowList = new ArrayList<Row>();

    int sheetCount = 1;//??sheet?  

    Sheet sheet = null;
    if (onlyReadOneSheet) { //??sheet  
        // ??sheet(?????)  
        sheet = selectedSheetName.equals("") ? wb.getSheetAt(selectedSheetIdx) : wb.getSheet(selectedSheetName);
    } else { //?sheet  
        sheetCount = wb.getNumberOfSheets();//????  
    }

    // ?sheet  
    for (int t = startSheetIdx; t < sheetCount + endSheetIdx; t++) {
        // ??sheet  
        if (!onlyReadOneSheet) {
            sheet = wb.getSheetAt(t);
        }

        //???  
        int lastRowNum = sheet.getLastRowNum();

        if (lastRowNum > 0) { //>0?  
            out("\n????" + sheet.getSheetName() + "");
        }

        Row row = null;
        // ?  
        for (int i = startReadPos; i <= lastRowNum + endReadPos; i++) {
            row = sheet.getRow(i);
            if (row != null) {
                rowList.add(row);
                out("" + (i + 1) + "", false);
                // ???  
                for (int j = 0; j < row.getLastCellNum(); j++) {
                    String value = getCellValue(row.getCell(j));
                    if (!value.equals("")) {
                        out(value + " | ", false);
                    }
                }
                out("");
            }
        }
    }
    return rowList;
}

From source file:com.netxforge.netxstudio.models.importer.MasterDataImporter_xssf.java

License:Open Source License

private boolean setEClassFromSheetName(Sheet sheet) {
    String name = sheet.getSheetName();
    for (int i = 0; i < this.ePackagesToImport.length; i++) {
        final EPackage ePackage = ePackagesToImport[i];
        for (EClassifier classifier : ePackage.getEClassifiers()) {
            if (classifier instanceof EClass) {
                String cName = ((EClass) classifier).getName();
                if (cName.equalsIgnoreCase(name)) {

                    eClassToImport = (EClass) classifier;

                    if (ImportActivator.DEBUG) {
                        ImportActivator.TRACE.trace(ImportActivator.TRACE_IMPORT_OPTION,
                                "CLASS REF: " + eClassToImport.getName());
                    }/*ww w  .j a v  a2  s .  c  o  m*/
                }
                cName = cName + "_refs";
                if (cName.equalsIgnoreCase(name)) {
                    eClassToImport = (EClass) classifier;
                    if (ImportActivator.DEBUG) {
                        ImportActivator.TRACE.trace(ImportActivator.TRACE_IMPORT_OPTION,
                                "CLASS MULTI_REF: " + eClassToImport.getName());
                    }
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:com.opendoorlogistics.core.tables.io.PoiIO.java

License:Open Source License

public static ODLDatastoreAlterable<ODLTableAlterable> importExcel(InputStream stream, ExecutionReport report) {
    //tmpFileBugFix();

    ODLDatastoreAlterable<ODLTableAlterable> ds = ODLFactory.createAlterable();

    Workbook wb = null;/*w w  w . j  a  v  a2 s  .co  m*/
    try {
        wb = WorkbookFactory.create(stream);

        String author = getAuthor(wb);
        if (author != null && Strings.equalsStd(author, AppConstants.ORG_NAME)) {
            ds.setFlags(ds.getFlags() | ODLDatastore.FLAG_FILE_CREATED_BY_ODL);
        }

    } catch (Throwable e) {
        throw new RuntimeException(e);
    }

    // look for the schema; remove it from the workbook to simplify the later workbook updating code
    // (the schema gets held by the datastore structure anyway)
    SchemaSheetInformation info = null;
    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        Sheet sheet = wb.getSheetAt(i);
        if (Strings.equalsStd(sheet.getSheetName(), SCHEMA_SHEET_NAME)) {
            info = importSchemaTables(sheet, report);
            wb.removeSheetAt(i);
            break;
        }
    }

    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        Sheet sheet = wb.getSheetAt(i);
        ODLTableAlterable table = ds.createTable(sheet.getSheetName(), -1);
        importSheet(table, sheet, info != null ? info.schema : null, false);
    }

    return ds;
}

From source file:com.opendoorlogistics.core.tables.io.PoiIO.java

License:Open Source License

/**
 * Schema table can contain multiple tables...
 * @param sheet// w w w  . ja  v  a  2  s .co  m
 */
private static SchemaSheetInformation importSchemaTables(Sheet sheet, ExecutionReport report) {
    List<ODLTableReadOnly> tables = new ArrayList<>();

    // schema tables are separated by empty rows
    int lastRow = sheet.getLastRowNum();
    int firstRow = sheet.getFirstRowNum();

    int firstNonEmptyRow = -1;
    int nbCols = 0;
    for (int x = firstRow; x <= lastRow; x++) {

        // check for completely empty row
        Row row = sheet.getRow(x);
        boolean isEmptyRow = true;
        for (int y = 0; row != null && y <= row.getLastCellNum(); y++) {
            if (isEmptyCell(row, y) == false) {
                isEmptyRow = false;
            }
        }

        if (isEmptyRow || x == lastRow) {

            // dump table if row was empty or on last row, but we previously had a non empty row
            if (firstNonEmptyRow != -1) {
                ODLDatastoreAlterable<ODLTableAlterable> tmpDs = ODLDatastoreImpl.alterableFactory.create();
                ODLTableAlterable table = tmpDs.createTable(sheet.getSheetName(), -1);
                importSheetSubset(table, sheet, null, true, firstNonEmptyRow, isEmptyRow ? x - 1 : x, nbCols);
                tables.add(table);
            }
            firstNonEmptyRow = -1;
        } else if (firstNonEmptyRow == -1) {
            // initialise table if we've just found the first non empty row
            firstNonEmptyRow = x;
            nbCols = 0;
            for (int y = 0; y <= row.getLastCellNum(); y++) {
                if (isEmptyCell(row, y)) {
                    break;
                } else {
                    nbCols = y + 1;
                }
            }
        }
    }

    return readSchemaFromODLTables(tables, report);
}

From source file:com.opendoorlogistics.core.tables.io.PoiIO.java

License:Open Source License

/**
 * Import the sheet and return key-values if its a schema
 * @param ds//www . j  ava2  s . c om
 * @param sheet
 * @param schema
 * @param isSchemaSheet
 * @return
 */
private static void importSheetSubset(ODLTableAlterable table, Sheet sheet, SchemaIO schema,
        boolean isSchemaSheet, int firstRow, int lastRow, int nbCols) {

    // get column names
    Row header = sheet.getRow(firstRow);
    for (int col = 0; col < nbCols; col++) {

        // try getting schema definition for the column
        String name = null;
        SchemaColumnDefinition dfn = null;
        if (header != null) {
            name = getFormulaSafeTextValue(header.getCell(col));
            if (name != null && schema != null) {
                dfn = schema.findDefinition(sheet.getSheetName(), name);
            }
        }
        name = getValidNewColumnName(name, table);

        // use the schema column definition if we have one
        if (dfn != null) {
            addColumnFromDfn(dfn, name, col, table);
        } else {

            // analyse the other rows for a 'best guess' type
            ODLColumnType chosenType = ODLColumnType.STRING;
            if (isSchemaSheet == false) {
                ColumnTypeEstimator typeEstimator = new ColumnTypeEstimator();
                for (int rowIndx = firstRow + 1; rowIndx <= lastRow; rowIndx++) {
                    Row row = sheet.getRow(rowIndx);
                    String value = getFormulaSafeTextValue(row.getCell(col));
                    typeEstimator.processValue(value);
                }

                chosenType = typeEstimator.getEstimatedType();
            }
            table.addColumn(col, name, chosenType, 0);
        }
    }

    // load all other rows
    for (int rowIndx = firstRow + 1; rowIndx <= lastRow; rowIndx++) {
        Row row = sheet.getRow(rowIndx);
        int outRowIndx = table.createEmptyRow(rowIndx);
        for (int col = 0; col < nbCols; col++) {
            String value = getFormulaSafeTextValue(row.getCell(col));
            table.setValueAt(value, outRowIndx, col);
        }

    }

}

From source file:com.plugin.excel.util.ExcelFileHelper.java

License:Apache License

private static CellStyle getDateStyle(String style, Sheet sheet, Font font) {
    if (StringUtils.isNotBlank(style) && sheet != null) {
        StyleKey cache = new StyleKey(style, sheet.getSheetName());
        if (styleCache.get(cache) == null) {
            CellStyle cellStyle = sheet.getWorkbook().createCellStyle();
            font.setFontHeight((short) 10);
            cellStyle.setFont(font);//from ww w.j a v a 2  s .co  m
            styleCache.put(cache, cellStyle);
        }
        return styleCache.get(cache);
    }
    return null;
}

From source file:com.projectswg.tools.controllers.MainController.java

License:Open Source License

private FileItem getFileItem(String parent, Sheet sheet) {
    String path = parent.substring(0, parent.lastIndexOf("\\") + 1) + sheet.getSheetName() + ".iff";
    return new FileItem(new File(path), sheet);
}

From source file:com.projectswg.tools.ExcelToIff.java

License:Open Source License

private static void convertWorkbook(String path) {
    File file = new File(path);
    if (!file.exists()) {
        System.err.println(String.format("Could not convert %s as it doesn't exist!", path));
        return;//from   ww  w. ja va  2s  . c  om
    }

    try {
        Workbook workbook = WorkbookFactory.create(file);
        System.out.println("Converting sheets from workbook " + file.getAbsolutePath());
        for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
            Sheet sheet = workbook.getSheetAt(i);
            path = file.getAbsolutePath().split("\\.")[0] + "_" + sheet.getSheetName() + ".iff";
            convertSheet(new File(path), sheet);
        }
        System.out.println("Conversion for workbook " + file.getAbsolutePath() + " completed.");
    } catch (IOException | InvalidFormatException e) {
        e.printStackTrace();
    }
}