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

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

Introduction

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

Prototype

int getLastRowNum();

Source Link

Document

Gets the last row on the sheet Note: rows which had content before and were set to empty later might still be counted as rows by Excel and Apache POI, so the result of this method will include such rows and thus the returned value might be higher than expected!

Usage

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

License:Apache License

/** 
 * Excel? /*from   w  w w  .j a v  a 2s  .  c  om*/
 *  
 * @Title: WriteExcel 
 * @Date : 2014-9-11 ?01:33:59 
 * @param wb 
 * @param rowList 
 * @param xlsPath 
 */
private void writeExcel(Workbook wb, List<Row> rowList, String xlsPath) {

    if (wb == null) {
        out("???");
        return;
    }

    Sheet sheet = wb.getSheetAt(0);// sheet  

    // ???????  
    int lastRowNum = isOverWrite ? startReadPos : sheet.getLastRowNum() + 1;
    int t = 0;//  
    out("???" + rowList.size());
    for (Row row : rowList) {
        if (row == null)
            continue;
        // ???  
        int pos = findInExcel(sheet, row);

        Row r = null;// ??????  
        if (pos >= 0) {
            sheet.removeRow(sheet.getRow(pos));
            r = sheet.createRow(pos);
        } else {
            r = sheet.createRow(lastRowNum + t++);
        }

        //??  
        CellStyle newstyle = wb.createCellStyle();

        //?  
        for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
            Cell cell = r.createCell(i);// ??  
            cell.setCellValue(getCellValue(row.getCell(i)));// ???  
            // cell.setCellStyle(row.getCell(i).getCellStyle());//  
            if (row.getCell(i) == null)
                continue;
            copyCellStyle(row.getCell(i).getCellStyle(), newstyle); // ????  
            cell.setCellStyle(newstyle);// ?  
            // sheet.autoSizeColumn(i);//  
        }
    }
    out("???:" + (rowList.size() - t) + " ?" + t);

    // ??  
    setMergedRegion(sheet);

    try {
        // ??Excel  
        FileOutputStream outputStream = new FileOutputStream(xlsPath);
        wb.write(outputStream);
        outputStream.flush();
        outputStream.close();
    } catch (Exception e) {
        out("Excel?? ");
        e.printStackTrace();
    }
}

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

License:Apache License

/** 
 * ???Excel //from   w  w  w.  ja  va  2 s . c om
 *  
 * @Title: findInExcel 
 * @Date : 2014-9-11 ?02:23:12 
 * @param sheet 
 * @param row 
 * @return 
 */
private int findInExcel(Sheet sheet, Row row) {
    int pos = -1;

    try {
        // ??  
        if (isOverWrite || !isNeedCompare) {
            return pos;
        }
        for (int i = startReadPos; i <= sheet.getLastRowNum() + endReadPos; i++) {
            Row r = sheet.getRow(i);
            if (r != null && row != null) {
                String v1 = getCellValue(r.getCell(comparePos));
                String v2 = getCellValue(row.getCell(comparePos));
                if (v1.equals(v2)) {
                    pos = i;
                    break;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return pos;
}

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

License:Apache License

/** 
 * ??? //from ww  w . java  2 s.c o  m
 *  
 * @param sheet 
 * @param row 
 * @param column 
 * @return 
 */
public void setMergedRegion(Sheet sheet) {
    int sheetMergeCount = sheet.getNumMergedRegions();

    for (int i = 0; i < sheetMergeCount; i++) {
        // ????  
        CellRangeAddress ca = sheet.getMergedRegion(i);
        int firstRow = ca.getFirstRow();
        if (startReadPos - 1 > firstRow) {// ??????  
            continue;
        }
        int lastRow = ca.getLastRow();
        int mergeRows = lastRow - firstRow;// ?  
        int firstColumn = ca.getFirstColumn();
        int lastColumn = ca.getLastColumn();
        // ???????  
        for (int j = lastRow + 1; j <= sheet.getLastRowNum(); j++) {
            // ??  
            sheet.addMergedRegion(new CellRangeAddress(j, j + mergeRows, firstColumn, lastColumn));
            j = j + mergeRows;// ?  
        }

    }
}

From source file:com.netxforge.netxstudio.server.logic.reporting.NodeResourceReportingLogic.java

License:Open Source License

@Override
protected void writeContent(Sheet sheet, Node node, int rowIndex, int nodeTypeCount) {

    if (reportingEngine == null) {
        reportingEngine = new ResourceReportingEngine(this.getPeriod(), this.getWorkBook());
    }//from  ww  w.  j ava2  s.  c  o m

    // We skip reporting for this node, using a static check.
    // if (getModelUtils().ragShouldReport(
    // getModelUtils().ragCountResourcesForNode(service, node,
    // this.getPeriod()))) {

    int newRow = rowIndex == 0 ? NODE_ROW : sheet.getLastRowNum() + 1;

    Row nodeRow = reportingEngine.rowForIndex(sheet, newRow);

    Cell nodeCell = nodeRow.createCell(NODE_COLUMN);
    nodeCell.setCellValue(node.getNodeID());

    // Write the time stamps.
    reportingEngine.writeTS(sheet, ++newRow);

    // TODO, Convert this util for Node only, with no service????
    // markersForNode = this.getModelUtils().markersForNode(service, node,
    // this.getPeriod());

    //
    // markersForNode = this.getModelUtils()
    // .toleranceMarkerMapPerResourceForServiceAndNodeAndPeriod(
    // service, node, this.getPeriod());

    // } else {
    //
    // nodesNotReported++;
    // }
}

From source file:com.netxforge.netxstudio.server.logic.reporting.NodeResourceReportingLogic.java

License:Open Source License

@Override
protected void writeContent(Sheet sheet, Component component) {

    // write each component ina new row.
    if (component.getResourceRefs().size() > 0) {
        // reportingEngine.writeComponentLine(newRow, sheet, component);

        if (LogicActivator.DEBUG) {
            LogicActivator.TRACE.trace(LogicActivator.TRACE_REPORT_OPTION,
                    "-- report component: " + StudioUtils.printModelObject(component));
        }/*  ww  w  .  j a  v  a  2s.  c o  m*/
        reportingEngine.writeFlat(sheet.getLastRowNum(), sheet, component, markersForNode);
    } else {
        this.componentsNotReported++;
    }

}

From source file:com.netxforge.netxstudio.server.reporting.RFSServiceResourceEmitter.java

License:Open Source License

@Override
public void writeContent(Service service, Node node, int rowIndex, int nodeTypeCount) {

    Sheet sheet = this.getSheet();

    // We skip reporting for this node, using a static check.
    // if (getModelUtils().ragShouldReport(
    // getModelUtils().ragCountResourcesForNode(service, node,
    // this.getPeriod()))) {
    if (reportingEngine == null) {
        // queryService.setDataProvider(this.getDataProvider());
        reportingEngine = new ResourceReportingEngine(period, this.getWorkBook());
    }/*ww w .ja v a 2 s. co m*/

    // We skip reporting for this node, using a static check.
    // if (getModelUtils().ragShouldReport(
    // getModelUtils().ragCountResourcesForNode(service, node,
    // this.getPeriod()))) {

    int newRow = rowIndex == 0 ? NODE_ROW : sheet.getLastRowNum() + 1;

    Row nodeRow = reportingEngine.rowForIndex(sheet, newRow);

    Cell nodeCell = nodeRow.createCell(NODE_COLUMN);
    nodeCell.setCellValue(node.getNodeID());

    // Write the time stamps.
    reportingEngine.writeTS(sheet, ++newRow);

    // We need a
    IMonitoringSummary summary = monitoringStateModel.summary(new NullProgressMonitor(), node,
            new IComputationContext[] { new ObjectContext<RFSService>((RFSService) service),
                    new ObjectContext<DateTimeRange>(period)

            });

    if (summary instanceof NodeTypeSummary) {
        markersForNode = ((NodeTypeSummary) summary).markers();
    }
}

From source file:com.netxforge.netxstudio.server.reporting.RFSServiceResourceEmitter.java

License:Open Source License

@Override
public void writeContent(Component component) {

    Sheet sheet = getSheet();

    if (component.getResourceRefs().size() > 0) {
        // reportingEngine.writeComponentLine(newRow, sheet, component);

        if (LogicActivator.DEBUG) {
            LogicActivator.TRACE.trace(LogicActivator.TRACE_REPORT_OPTION,
                    "-- report component: " + StudioUtils.printModelObject(component));
        }/*from w  w  w.ja  v a2  s  . c om*/
        reportingEngine.writeFlat(sheet.getLastRowNum(), sheet, component, markersForNode);
    } else {
        this.componentsNotReported++;
    }
}

From source file:com.ocs.dynamo.importer.impl.BaseXlsImporter.java

License:Apache License

@Override
public int countRows(byte[] bytes, int row, int column) {
    Workbook wb = createWorkbook(bytes);
    if (wb.getNumberOfSheets() == 0) {
        return 0;
    } else {//from www . j av  a 2 s. co m
        Sheet sheet = wb.getSheetAt(0);
        return sheet.getLastRowNum() - sheet.getFirstRowNum() + 1;
    }
}

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

License:Open Source License

private static void addSchema(ODLDatastore<? extends ODLTableDefinition> ds, Workbook wb) {
    ODLTableReadOnly table = SchemaIO.createSchemaTable(ds);
    Sheet sheet = wb.createSheet(SCHEMA_SHEET_NAME);

    // write out key-value table
    Row row = sheet.createRow(0);//from w w w . j a  v a 2  s. com
    row.createCell(0).setCellValue(SchemaIO.KEY_COLUMN);
    row.createCell(1).setCellValue(SchemaIO.VALUE_COLUMN);
    row = sheet.createRow(1);
    row.createCell(0).setCellValue(SchemaIO.APP_VERSION_KEY);
    row.createCell(1).setCellValue(AppConstants.getAppVersion().toString());

    // write schema table
    exportTable(sheet, table, sheet.getLastRowNum() + 2, null, null);

    // hide the sheet from users
    wb.setSheetHidden(wb.getNumberOfSheets() - 1, Workbook.SHEET_STATE_VERY_HIDDEN);
}

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

License:Open Source License

private static Dimension getBoundingBox(Sheet sheet) {
    Dimension ret = new Dimension(0, sheet.getLastRowNum() + 1);
    for (int i = 0; i < ret.height; i++) {
        Row row = sheet.getRow(i);/*  www  .  ja v  a 2s. c o  m*/
        if (row != null) {
            ret.width = Math.max(ret.width, row.getLastCellNum());
        }
    }
    return ret;
}