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

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

Introduction

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

Prototype

int getPhysicalNumberOfRows();

Source Link

Document

Returns the number of physically defined rows (NOT the number of rows in the sheet)

Usage

From source file:gtu._work.ui.SqlCreaterUI.java

License:Open Source License

private void firstRowMakeInsertSqlBtn(ActionEvent evt) {
    try {/*from w  w  w  .ja  v a  2  s  . c  om*/
        String tableName = Validate.notBlank(tableNameText.getText(), "??");
        File srcFile = JCommonUtil.filePathCheck(excelFilePathText2.getText(), "?", "xlsx");
        File saveFile = JCommonUtil._jFileChooser_selectFileOnly_saveFile();
        if (saveFile == null) {
            JCommonUtil._jOptionPane_showMessageDialog_error("?");
            return;
        }

        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(saveFile), "utf8"));

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(bis);
        Sheet sheet = xssfWorkbook.getSheetAt(0);

        LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();
        for (int ii = 0; ii < sheet.getRow(0).getLastCellNum(); ii++) {
            valueMap.put(formatCellType(sheet.getRow(0).getCell(ii)), "");
        }

        for (int j = 0; j < sheet.getPhysicalNumberOfRows(); j++) {
            Row row = sheet.getRow(j);
            LinkedHashMap<String, String> valueMap2 = (LinkedHashMap<String, String>) valueMap.clone();
            int ii = 0;
            for (String key : valueMap2.keySet()) {
                valueMap2.put(key, formatCellType(row.getCell(ii)));
                ii++;
            }
            appendLog("" + valueMap2);
            String insertSql = this.fetchInsertSQL(tableName, valueMap2);
            appendLog("" + insertSql);
            writer.write(insertSql);
            writer.newLine();
        }
        bis.close();

        writer.flush();
        writer.close();

        JCommonUtil._jOptionPane_showMessageDialog_info("? : \n" + saveFile);
    } catch (Exception ex) {
        JCommonUtil.handleException(ex);
    }
}

From source file:guru.qas.martini.report.MainTest.java

License:Apache License

@Test
public void testMultipleInput() throws Exception {
    getInputFile();// w  ww  .j a v a  2 s .com
    getInputFile();

    String inputResource = tmpDir.toURI().resolve("**/*.json").toURL().toExternalForm();

    File outputFile = getOutputFile();
    String outputResource = outputFile.toURI().toURL().toExternalForm();

    String[] args = new String[] { "-i", inputResource, "-o", outputResource };
    Main.main(args);
    checkState(outputFile.exists(), "output file does not exist");

    Workbook workbook = getWorkbook(outputFile);
    assertReportContents(workbook);
    Sheet sheet = workbook.getSheetAt(1);
    int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
    checkState(2 == physicalNumberOfRows, "worksheet 'Suite' should contain two rows but contains %s",
            physicalNumberOfRows);
}

From source file:hu.webhejj.commons.io.table.excel.ExcelSheet.java

License:Apache License

@Override
public List<Row> getRows() {
    return new AbstractList<TableReader.Row>() {

        @Override/*w  ww.  j a  va  2s.  co m*/
        public Row get(int index) {
            return getRow(index);
        }

        @Override
        public int size() {
            Sheet sheet = getSheet();
            int i = sheet.getLastRowNum();
            if (i == 0) {
                return sheet.getPhysicalNumberOfRows() == 0 ? 0 : 1;
            }
            return i + 1;
        }
    };
}

From source file:ie.cmrc.tabular.excel.ExcelTable.java

License:Apache License

/**
 * Extracts the header of a given sheet in the form of a {@code FieldMap<Integer>} that stores the indexes of each field.
 * The header must be in the first row of the sheet.
 * @param sheet Excel Sheet ({@code org.apache.poi.ss.usermodel.Sheet}) to extract the header from
 * @return A hash map mapping each colum name with its index. If the sheet is null or empty or the first row is empty, then and empty {@link ie.cmrc.tabular.FieldMapHeader} is returned.
 *//*w  w w .  j av a  2  s  .  co  m*/
private FieldMapHeader<Integer> parseHeader(Sheet sheet) {
    FieldMapHeader<Integer> sheetHeader = new FieldMapHeader<Integer>();
    if (sheet != null) {

        int rows = sheet.getPhysicalNumberOfRows();

        if (rows > 0) {
            Row row = sheet.getRow(0);
            if (row != null) {
                int n = row.getPhysicalNumberOfCells();

                if (n > 0) {
                    for (int i = 0; i < n; i++) {
                        Cell cell = row.getCell(i);
                        if (cell != null) {
                            ExcelTableCell sc = new ExcelTableCell(cell);
                            String colName = sc.getStringValue();
                            if (colName != null)
                                colName = colName.trim();

                            Term field = new Term(colName);
                            sheetHeader.put(field, i);

                        }
                    }
                }
            }
        }
    }
    return sheetHeader;
}

From source file:it.greenvulcano.excel.reader.ToCSVReader.java

License:Open Source License

@Override
protected boolean processSheet(Sheet sheet, int sNum) throws ExcelException {
    return sheet.getPhysicalNumberOfRows() > 0;
}

From source file:it.greenvulcano.excel.reader.ToXMLReader.java

License:Open Source License

@Override
protected boolean processSheet(Sheet sheet, int sNum) throws ExcelException {
    if (sheet.getPhysicalNumberOfRows() > 0) {
        shE = parser.createElement(doc, "sheet");
        root.appendChild(shE);/*from  w  w w  . j a  va 2  s  .  c  o  m*/
        parser.setAttribute(shE, "n", String.valueOf(sNum));
        Node name = shE.appendChild(parser.createElement(doc, "name"));
        name.appendChild(doc.createTextNode(sheet.getSheetName()));

        return true;
    }
    return false;
}

From source file:it.vige.greenarea.file.ImportaXLSFile.java

License:Apache License

@Override
public List<RichiestaXML> prelevaDati(InputStream inputStream, List<Filtro> filtri) throws Exception {
    if (filtri != null)
        acceptedRoundCodes.addAll(filtri);
    XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
    Sheet sheet = workbook.getSheetAt(0);

    int rowsCount = sheet.getPhysicalNumberOfRows();
    List<RichiestaXML> richiesteXML = new ArrayList<RichiestaXML>();
    for (int i = 1; i < rowsCount; i++) {
        Row row = sheet.getRow(i);/*w  w  w .ja  v a  2  s.  co m*/
        int colCounts = row.getLastCellNum();
        RichiestaXML richiestaXML = new RichiestaXML();
        for (int j = 0; j < colCounts; j++) {
            Cell cell = row.getCell(j);
            if (cell != null)
                aggiungiCampoARichiestaXML(richiestaXML, cell, j);
        }
        String roundCode = richiestaXML.getRoundCode();
        if (acceptRoundCode(roundCode))
            richiesteXML.add(richiestaXML);
    }
    workbook.close();
    return richiesteXML;
}

From source file:itpreneurs.itp.report.archive.ToCSV.java

License:Apache License

/**
 * Called to convert the contents of the currently opened workbook into
 * a CSV file.//from www. j a v  a2s.c om
 */
private void convertToCSV() {
    Sheet sheet = null;
    Row row = null;
    int lastRowNum = 0;
    this.csvData = new ArrayList<ArrayList<String>>();

    System.out.println("Converting files contents to CSV format.");

    // Discover how many sheets there are in the workbook....
    int numSheets = this.workbook.getNumberOfSheets();
    // and then iterate through them.
    for (int i = 0; i < numSheets; i++) {

        // Get a reference to a sheet and check to see if it contains
        // any rows.
        sheet = this.workbook.getSheetAt(i);

        if (sheet.getPhysicalNumberOfRows() > 0) {

            // Note down the index number of the bottom-most row and
            // then iterate through all of the rows on the sheet starting
            // from the very first row - number 1 - even if it is missing.
            // Recover a reference to the row and then call another method
            // which will strip the data from the cells and build lines
            // for inclusion in the resylting CSV file.
            lastRowNum = sheet.getLastRowNum();
            for (int j = 0; j <= lastRowNum; j++) {
                row = sheet.getRow(j);
                this.rowToCSV(row);
            }
        }
    }
}

From source file:javafxapplication12.FXMLDocumentController.java

public void Search_to_file() throws FileNotFoundException, IOException {
    // File ?      ?

    Map staff = new HashMap<Integer, String>();
    ArrayList count_list = new ArrayList();
    for (int i = 0; i < temp_file_name.size(); i++) {
        InputStream in = new FileInputStream("Version/" + (String) temp_file_name.get(i));
        HSSFWorkbook wb = new HSSFWorkbook(in);
        Sheet s = wb.getSheetAt(0);
        int count = 0;
        boolean input_str = false;
        for (int j = 0; j <= s.getPhysicalNumberOfRows() - 1; j++) {
            String condition = wb.getSheetAt(0).getRow(j).getCell(1).getStringCellValue();
            String result = wb.getSheetAt(0).getRow(j).getCell(2).getStringCellValue();
            String date = wb.getSheetAt(0).getRow(j).getCell(3).getStringCellValue();
            String tester = wb.getSheetAt(0).getRow(j).getCell(4).getStringCellValue();
            ///??  ??  ?  ? Exel  
            if (tester.indexOf(Search_people.getText()) != -1) {
                System.out.println(" 1 ? ? 2");
                input_str = true;//w  ww . j  ava2 s  . com
                count++;
            }
            if (date.indexOf(Search_people.getText()) != -1) {
                System.out.println(" 1 ? ? 2");
                input_str = true;
                count++;
            }
            if (result.indexOf(Search_people.getText()) != -1) {
                System.out.println(" 1 ? ? 2");
                input_str = true;
                count++;
            }
            if (condition.indexOf(Search_people.getText()) != -1) {
                System.out.println(" 1 ? ? 2");
                input_str = true;
                count++;
            }

            System.out.println("Count of " + count);
        }

        if (input_str) {
            System.out.println("input str work!!!");
            staff.put(count, (String) temp_file_name.get(i));
            count_list.add(count);
        }
        in.close();
    }

    TreeMap<Integer, String> sorted = new TreeMap<Integer, String>(new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o2.compareTo(o1);
        }
    });

    sorted.putAll(staff);
    ver_list.clear();
    File_name.clear();
    for (int i = 0; i < count_list.size(); i++) {
        String date = (String) sorted.get(count_list.get(i));
        System.out.println("Date:" + date);
        System.out.println("Date_sub:" + date.substring(0, 8) + "Time_sub:" + date.substring(9, 17));
        Version_file ch = new Version_file(i + 1, date.substring(0, 8), date.substring(9, 17));
        ver_list.add(ch);
        File_name.add(date);
    }

    //File_name.add(staff.get(25));
    //System.out.println(count_list.get(i));
    //;
    /*
    int i=0; 
    while(i != list.size()){ 
    Row row = sheet.createRow(i);
    row.createCell(0).setCellValue("asdasd");
    }*/
}

From source file:javafxapplication12.FXMLDocumentController.java

public void OpenFile() throws IOException {
    TablePosition pos = FileTable.getSelectionModel().getSelectedCells().get(0);
    int row_file = pos.getRow();
    System.out.println(File_name.get(row_file));
    add.setVisible(false);/*from   ww  w . j  a  v  a2  s.  co m*/
    delete.setVisible(false);
    save.setVisible(false);

    data.clear();

    InputStream in = new FileInputStream("Version/" + (String) File_name.get(row_file));
    HSSFWorkbook wb = new HSSFWorkbook(in);
    Sheet s = wb.getSheetAt(0);
    for (int i = 0; i <= s.getPhysicalNumberOfRows() - 1; i++) {
        String condition = wb.getSheetAt(0).getRow(i).getCell(1).getStringCellValue();
        String result = wb.getSheetAt(0).getRow(i).getCell(2).getStringCellValue();
        String date = wb.getSheetAt(0).getRow(i).getCell(3).getStringCellValue();
        String tester = wb.getSheetAt(0).getRow(i).getCell(4).getStringCellValue();

        Check ch = new Check(data.size() + 1, condition, result, date, tester);
        data.add(ch);
    }
}