Example usage for org.apache.poi.ss.usermodel Row getLastCellNum

List of usage examples for org.apache.poi.ss.usermodel Row getLastCellNum

Introduction

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

Prototype

short getLastCellNum();

Source Link

Document

Gets the index of the last cell contained in this row PLUS ONE.

Usage

From source file:misuExcel.excelRead.java

License:Open Source License

public int getSCellNum(int i) {
    if (i > -1) {
        Sheet sheet = (Sheet) wb.getSheetAt(i);
        int max = 0;
        for (int j = 0; j < sheet.getLastRowNum(); j++) {
            Row row = sheet.getRow(j);
            int r = row.getLastCellNum();
            if (r > max) {
                max = r;//from w  w w .  j a  v  a 2 s.  c o m
            }
        }
        return max;
    }
    return 0;
}

From source file:misuExcel.excelWrite.java

License:Open Source License

private void outType01() {
    if (wbSheet != null && names != null && list != null) {
        Log.info("list size:" + list.size());
        String strinfo = "";
        for (int i = 0; i < list.size(); i++) {
            ArrayList<Integer> integers = list.get(i);
            Workbook splitWb = null;//from w  w w . j  a  v a 2s .c om
            if (indexType == 1)
                splitWb = new XSSFWorkbook();
            else if (indexType == 2)
                splitWb = new HSSFWorkbook();
            Sheet sheet = splitWb.createSheet("split");
            for (int j = 0; j < integers.size() + splitJpanel.ignore_Row; j++) {
                Row row = null;
                Row copy = null;
                if (j >= splitJpanel.ignore_Row) {
                    row = sheet.createRow(j);
                    copy = wbSheet.getRow(integers.get(j - splitJpanel.ignore_Row));
                } else {
                    row = sheet.createRow(j);
                    copy = wbSheet.getRow(j);
                }
                for (int k = 0; k < copy.getLastCellNum(); k++) {
                    Cell cell = row.createCell(k);
                    Cell copyCell = copy.getCell(k);
                    if (copyCell != null) {
                        switch (copyCell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                            cell.setCellValue(copyCell.getRichStringCellValue().getString().trim());
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            if (DateUtil.isCellDateFormatted(copyCell)) {
                                cell.setCellValue(copyCell.getDateCellValue());
                            } else {
                                cell.setCellValue(copyCell.getNumericCellValue());
                            }
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            cell.setCellValue(copyCell.getBooleanCellValue());
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            cell.setCellValue(copyCell.getCellFormula());
                            break;
                        default:
                            cell.setCellValue(copyCell.getStringCellValue().trim());
                        }
                    }
                }
            }
            createWB(splitWb, names.get(i));
            Log.info(names.get(i) + ".xlsx?");
            strinfo += names.get(i) + "." + _index + "?;";
            if (i != 0 && i % 3 == 0) {
                strinfo += "\n";
            }
        } //end for
        JOptionPane.showMessageDialog(null, strinfo);
    }
}

From source file:misuExcel.excelWrite.java

License:Open Source License

private void outType02() {
    if (wbSheet != null && names != null && list != null) {
        Log.info("list size:" + list.size());
        Workbook splitWb = null;/*from ww w  .  j a v  a2 s  .  c  o m*/
        if (indexType == 1)
            splitWb = new XSSFWorkbook();
        else if (indexType == 2)
            splitWb = new HSSFWorkbook();
        for (int i = 0; i < list.size(); i++) {
            ArrayList<Integer> integers = list.get(i);
            Sheet sheet = splitWb.createSheet(names.get(i));
            for (int j = 0; j < integers.size() + splitJpanel.ignore_Row; j++) {
                Row row = null;
                Row copy = null;
                if (j >= splitJpanel.ignore_Row) {
                    row = sheet.createRow(j);
                    copy = wbSheet.getRow(integers.get(j - splitJpanel.ignore_Row));
                } else {
                    row = sheet.createRow(j);
                    copy = wbSheet.getRow(j);
                }
                for (int k = 0; k < copy.getLastCellNum(); k++) {
                    Cell cell = row.createCell(k);
                    Cell copyCell = copy.getCell(k);
                    if (copyCell != null) {
                        switch (copyCell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                            cell.setCellValue(copyCell.getRichStringCellValue().getString().trim());
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            if (DateUtil.isCellDateFormatted(copyCell)) {
                                cell.setCellValue(copyCell.getDateCellValue());
                            } else {
                                cell.setCellValue(copyCell.getNumericCellValue());
                            }
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            cell.setCellValue(copyCell.getBooleanCellValue());
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            cell.setCellValue(copyCell.getCellFormula());
                            break;
                        default:
                            cell.setCellValue(copyCell.getStringCellValue().trim());
                        }
                    }
                }
            }
        } //end for
        createWB(splitWb, fileReal + "(cut)");
        JOptionPane.showMessageDialog(null, fileReal + "(cut)." + _index + "?");
    }
}

From source file:misuExcel.excelWrite.java

License:Open Source License

private void outType03() {
    if (wbSheet != null && addWb != null && names != null && list != null) {
        Sheet sheet = addWb.getSheetAt(sheetNum_target);
        for (int i = 0; i < list.size(); i++) {
            ArrayList<Integer> integers = list.get(i);
            Row copy = wbSheet.getRow(i + addJpanel.ignore_Rowtar);
            for (int j = 0; j < integers.size(); j++) {
                Row row = sheet.getRow(integers.get(j));
                int numRow = row.getLastCellNum();
                for (int k = addJpanel.ignore_Celltar; k < copy.getLastCellNum(); k++) {
                    Cell cell = null;/*ww w  .j a  v a2s  .  c  o  m*/
                    Cell copyCell = null;
                    if (k != cellNum_target) {
                        copyCell = copy.getCell(k);
                        if (addJpanel.ignore_Celltar > cellNum_target) {
                            cell = row.createCell(k + numRow - addJpanel.ignore_Celltar);
                        } else {
                            cell = row.createCell(k < cellNum_target ? (k + numRow - addJpanel.ignore_Celltar)
                                    : (k - 1 + numRow - addJpanel.ignore_Celltar));
                        }
                    }
                    if (copyCell != null) {
                        switch (copyCell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                            cell.setCellValue(copyCell.getRichStringCellValue().getString().trim());
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            if (DateUtil.isCellDateFormatted(copyCell)) {
                                cell.setCellValue(copyCell.getDateCellValue());
                            } else {
                                cell.setCellValue(copyCell.getNumericCellValue());
                            }
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            cell.setCellValue(copyCell.getBooleanCellValue());
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            cell.setCellValue(copyCell.getCellFormula());
                            break;
                        default:
                            cell.setCellValue(copyCell.getStringCellValue().trim());
                        }
                    }
                }
            }
        } //end for
        createWB(addWb, fileReal + "(add)");
        JOptionPane.showMessageDialog(null, fileReal + "(add)." + _index + "?");
    }
}

From source file:misuExcel.excelWrite.java

License:Open Source License

private void outType04() {
    if (wbSheet != null && addWb != null && names != null && list != null) {
        Sheet sheet = addWb.getSheetAt(sheetNum_target);
        int numRow = sheet.getLastRowNum() + 1;
        ArrayList<Integer> integers = list.get(0);
        for (int j = addJpanel.ignore_Rowtar; j <= wbSheet.getLastRowNum(); j++) {
            Row row = null;/*from  w  w  w  .jav a 2 s  .c o  m*/
            Row copy = null;
            if (j != cellNum_target) {
                if ((cellNum_target + 1) > addJpanel.ignore_Rowtar)
                    row = sheet.createRow(j < cellNum_target ? (j + numRow - addJpanel.ignore_Rowtar)
                            : (j + numRow - 1 - addJpanel.ignore_Rowtar));
                else
                    row = sheet.createRow(j + numRow - addJpanel.ignore_Rowtar);
                copy = wbSheet.getRow(j);
            }
            if (copy != null) {
                for (int k = 0; k < copy.getLastCellNum(); k++) {
                    Cell cell = null;
                    if (k >= addJpanel.ignore_Celltar)
                        cell = row.createCell(integers.get((k - addJpanel.ignore_Celltar)));
                    else
                        cell = row.createCell(k);
                    Cell copyCell = copy.getCell(k);
                    if (copyCell != null) {
                        switch (copyCell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                            cell.setCellValue(copyCell.getRichStringCellValue().getString().trim());
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            if (DateUtil.isCellDateFormatted(copyCell)) {
                                cell.setCellValue(copyCell.getDateCellValue());
                            } else {
                                cell.setCellValue(copyCell.getNumericCellValue());
                            }
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            cell.setCellValue(copyCell.getBooleanCellValue());
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            cell.setCellValue(copyCell.getCellFormula());
                            break;
                        default:
                            cell.setCellValue(copyCell.getStringCellValue().trim());
                        }
                    }
                }
            }
        } //end for
        createWB(addWb, fileReal + "(add)");
        JOptionPane.showMessageDialog(null, fileReal + "(add)." + _index + "?");
    }
}

From source file:mw.sqlitetool.MainFrame.java

private void innerExportToDb(Sheet sheet) {
    int rowNum = sheet.getLastRowNum();
    if (rowNum < 2) {
        throw new RuntimeException("Empty excel.");
    }/*ww w .jav  a2  s . c o m*/
    // the first row is attribute names.
    Row firstRow = sheet.getRow(1);
    int colNum = firstRow.getLastCellNum();
    Cell cell = null;

    StringBuilder sb = new StringBuilder();
    sb.append("create table [");
    sb.append(_currentFile);
    sb.append("](");
    for (int i = 0; i < colNum; i++) {
        cell = firstRow.getCell(i);
        String attribute = ExcelHelper.getInstance().getCellValue(cell).toString();
        sb.append("[").append(attribute).append("] varchar(100)");
        if (i != colNum - 1) {
            sb.append(", ");
        }
    }
    sb.append(");");
    String sql = sb.toString();
    this.log("Executing sql: " + sql);
    try {
        SqliteHelper.getInstance().executeSql(sql);
    } catch (SQLException ex) {
        this.log("Error: " + ex.getMessage());
    }

    // import the data
    Row row = null;
    sql = "insert into [" + _currentFile + "] values(";
    for (int i = 2; i < rowNum; i++) {
        row = sheet.getRow(i);
        String tmpSql = sql;
        for (int j = 0; j < colNum; j++) {
            cell = row.getCell(j);
            String val = "\"" + ExcelHelper.getInstance().getCellValue(cell).toString().replace("\'", "\'\'")
                    + "\"";
            tmpSql += val;
            if (j != colNum - 1) {
                tmpSql += ", ";
            }
        }
        tmpSql += ");";
        this.log("Executing sql: " + tmpSql);
        try {
            SqliteHelper.getInstance().executeSql(tmpSql);
        } catch (SQLException ex) {
            this.log("Error: " + ex.getMessage());
        }
    }
}

From source file:nc.noumea.mairie.appock.util.StockSpreadsheetExporter.java

License:Open Source License

private static void autosize(Workbook workbook) {
    Row row = workbook.getSheetAt(0).getRow(1);

    for (int colNum = 1; colNum < row.getLastCellNum() - 1; colNum++)
        workbook.getSheetAt(0).autoSizeColumn(colNum);

    int appockStockColumnWidth = workbook.getSheetAt(0).getColumnWidth(row.getLastCellNum() - 1);
    workbook.getSheetAt(0).setColumnWidth(row.getLastCellNum(), appockStockColumnWidth);

}

From source file:net.bafeimao.umbrella.support.data.entity.ExcelEntityParser.java

License:Apache License

private int getColumnIndex(Sheet sheet, String colName) {
    Map<String, Integer> columnIndexesMap = sheetColumnIndexesMap.get(sheet.getSheetName());

    if (columnIndexesMap == null) {
        columnIndexesMap = new HashMap<String, Integer>();
        Row titleRow = sheet.getRow(1);
        int colNum = titleRow.getLastCellNum();
        for (int i = titleRow.getFirstCellNum(); i < colNum; i++) {
            if (titleRow.getCell(i) != null) {
                columnIndexesMap.put(titleRow.getCell(i).getStringCellValue(), i);
            }/*  w w  w. j a va  2 s .c om*/
        }
        sheetColumnIndexesMap.put(sheet.getSheetName(), columnIndexesMap);
    }

    Integer index = columnIndexesMap.get(colName);
    return index == null ? -1 : index;
}

From source file:net.illustrato.ctrl.CtrlCore.java

private Row copyRow(Workbook workbook, Sheet worksheet, int sourceRowNum, int destinationRowNum) {
    // Get the source / new row
    Row newRow = worksheet.getRow(destinationRowNum);
    Row sourceRow = worksheet.getRow(sourceRowNum);

    // If the row exist in destination, push down all rows by 1 else create a new row
    if (newRow != null) {
        worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1);
    } else {/*from w  w  w .  ja  va  2 s . c  o  m*/
        newRow = worksheet.createRow(destinationRowNum);
    }

    // Loop through source columns to add to new row
    for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
        // Grab a copy of the old/new cell
        Cell oldCell = sourceRow.getCell(i);
        Cell newCell = newRow.createCell(i);

        // If the old cell is null jump to next cell
        if (oldCell == null) {
            newCell = null;
            continue;
        }

        // Copy style from old cell and apply to new cell
        CellStyle newCellStyle = workbook.createCellStyle();
        newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
        newCell.setCellStyle(newCellStyle);

        // Set the cell data type
        newCell.setCellType(oldCell.getCellType());

        // Set the cell data value
        switch (oldCell.getCellType()) {
        case HSSFCell.CELL_TYPE_BLANK:
            newCell.setCellValue(oldCell.getStringCellValue());
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
            newCell.setCellFormula(oldCell.getCellFormula());
            //Si tenemos que modificar la formulario lo podemos hacer como string
            //oldCell.getCellFormula().replace("A"+sourceRowNum, "A"+destinationRowNum)
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            newCell.setCellValue(oldCell.getNumericCellValue());
            break;
        case HSSFCell.CELL_TYPE_STRING:
            newCell.setCellValue(oldCell.getRichStringCellValue());
            break;
        }
    }
    return newRow;
}

From source file:net.java.amateras.xlsbeans.xssfconverter.impl.xssf.XssfWSheetImpl.java

License:Apache License

public int getColumns() {
    int minRowIndex = sheet.getFirstRowNum();
    int maxRowIndex = sheet.getLastRowNum();
    int maxColumnsIndex = 0;
    for (int i = minRowIndex; i <= maxRowIndex; i++) {
        Row row = sheet.getRow(i);
        if (row == null) {
            continue;
        }/*  w  w  w.  j av  a2s  .  c o m*/
        int column = row.getLastCellNum();
        if (column > maxColumnsIndex) {
            maxColumnsIndex = column;
        }
    }
    return maxColumnsIndex;
}