Example usage for org.apache.poi.hssf.usermodel HSSFRow getFirstCellNum

List of usage examples for org.apache.poi.hssf.usermodel HSSFRow getFirstCellNum

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFRow getFirstCellNum.

Prototype

@Override
public short getFirstCellNum() 

Source Link

Document

get the number of the first cell contained in this row.

Usage

From source file:com.zhu.action.CarAction.java

/**
 * excel /*  ww  w.j a v a 2  s .c o  m*/
 */
public boolean isBlankRow(HSSFRow row) {
    if (row == null)
        return true;
    for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
        HSSFCell hcell = row.getCell(i);
        if (!isBlankCell(hcell))
            return false;
    }
    return true;
}

From source file:edu.ku.brc.specify.tasks.subpane.wb.XLSImport.java

License:Open Source License

/**
 * @param headerRow/*from   ww  w  .java2 s  .c o  m*/
 */
private void getSystemCols(final HSSFRow headerRow) {
    for (int c = headerRow.getFirstCellNum(); c <= headerRow.getLastCellNum(); c++) {
        HSSFCell cell = headerRow.getCell(c);
        int nulls = 0;
        if (cell != null) {
            String header = cell.getRichStringCellValue().getString();
            if (header != null) {
                if (header.equals(IMAGE_PATH_HEADING)) {
                    cardImageCols.add(c - nulls);
                }
                if (header.equals(GEO_DATA_HEADING)) {
                    geoCol = c - nulls;
                }
            }
        } else {
            nulls++;
        }
    }
}

From source file:file.open.util.parse.XlsParser.java

License:Open Source License

public String[] splitLine() throws Exception {
    if (m_iCurrentRow == m_iNbRows) {
        return null;
    }//from   www.  ja v a2  s.co  m

    HSSFRow row = m_sheet.getRow(m_iCurrentRow);
    if (row == null) {
        return null;
    } else {
        int cellIndex = 0;
        int noOfCells = row.getPhysicalNumberOfCells();
        String[] values = new String[noOfCells];
        short firstCellNum = row.getFirstCellNum();
        short lastCellNum = row.getLastCellNum();

        if (firstCellNum >= 0 && lastCellNum >= 0) {
            for (short iCurrent = firstCellNum; iCurrent < lastCellNum; iCurrent++) {
                HSSFCell cell = (HSSFCell) row.getCell((int) iCurrent);
                if (cell == null) {
                    values[iCurrent] = "";
                    cellIndex++;
                    continue;
                } else {
                    switch (cell.getCellType()) {
                    case HSSFCell.CELL_TYPE_NUMERIC:
                        double value = cell.getNumericCellValue();
                        if (HSSFDateUtil.isCellDateFormatted(cell)) {
                            if (HSSFDateUtil.isValidExcelDate(value)) {
                                Date date = HSSFDateUtil.getJavaDate(value);
                                SimpleDateFormat dateFormat = new SimpleDateFormat(JAVA_TOSTRING);
                                values[iCurrent] = dateFormat.format(date);
                            } else {
                                throw new Exception("Invalid Date value found at row number " + row.getRowNum()
                                        + " and column number " + cell.getNumericCellValue());
                            }
                        } else {
                            values[iCurrent] = value + "";
                        }
                        break;

                    case HSSFCell.CELL_TYPE_STRING:
                        values[iCurrent] = cell.getStringCellValue();
                        break;

                    case HSSFCell.CELL_TYPE_BLANK:
                        values[iCurrent] = null;
                        break;

                    default:
                        values[iCurrent] = null;
                    }
                }
            }
        }
        m_iCurrentRow++;
        return values;
    }

}

From source file:FormatConvert.exceloperation.Excel2csv.java

public static void copySheets2CSV(HSSFSheet sheet, String csvfile) {
    int maxColumnNum = 0;

    try {//  w w w. j  a  va 2  s  . c o m
        FileWriter fw = new FileWriter(csvfile);

        String str = "";
        for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
            HSSFRow srcRow = sheet.getRow(i);

            if (srcRow != null) {
                for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) {
                    if (j != srcRow.getLastCellNum()) {
                        str = str + srcRow.getCell(j).getStringCellValue() + ",";
                    } else {
                        str = str + srcRow.getCell(j).getStringCellValue() + "\r\n";
                    }

                }
                fw.append(str);
            }
            str = "";
        }

        fw.flush();
        fw.close();
    } catch (IOException ex) {

    }
    //Util.copyPictures(newSheet,sheet) ;
}

From source file:Import.Utils.XSSFConvert.java

/**
 * @param srcSheet the sheet to copy./* w ww.ja  va2  s  .com*/
 * @param destSheet the sheet to create.
 * @param srcRow the row to copy.
 * @param destRow the row to create.
 * @param styleMap -
 */
public static void copyRow(HSSFSheet srcSheet, XSSFSheet destSheet, HSSFRow srcRow, XSSFRow destRow,
        Map<Integer, HSSFCellStyle> styleMap) {
    // manage a list of merged zone in order to not insert two times a
    // merged zone
    Set<CellRangeAddressWrapper> mergedRegions = new TreeSet<CellRangeAddressWrapper>();
    destRow.setHeight(srcRow.getHeight());
    // pour chaque row
    for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) {
        HSSFCell oldCell = srcRow.getCell(j); // ancienne cell
        XSSFCell newCell = destRow.getCell(j); // new cell
        if (oldCell != null) {
            if (newCell == null) {
                newCell = destRow.createCell(j);
            }
            // copy chaque cell
            copyCell(oldCell, newCell, styleMap);
            // copy les informations de fusion entre les cellules
            // System.out.println("row num: " + srcRow.getRowNum() +
            // " , col: " + (short)oldCell.getColumnIndex());
            CellRangeAddress mergedRegion = getMergedRegion(srcSheet, srcRow.getRowNum(),
                    (short) oldCell.getColumnIndex());

            if (mergedRegion != null) {
                // System.out.println("Selected merged region: " +
                // mergedRegion.toString());
                CellRangeAddress newMergedRegion = new CellRangeAddress(mergedRegion.getFirstRow(),
                        mergedRegion.getLastRow(), mergedRegion.getFirstColumn(), mergedRegion.getLastColumn());
                // System.out.println("New merged region: " +
                // newMergedRegion.toString());
                CellRangeAddressWrapper wrapper = new CellRangeAddressWrapper(newMergedRegion);
                if (isNewMergedRegion(wrapper, mergedRegions)) {
                    mergedRegions.add(wrapper);
                    destSheet.addMergedRegion(wrapper.range);
                }
            }
        }
    }

}

From source file:it.filippovitale.fineco2qif.logic.ExcelSheetAnalysisLogic.java

License:Apache License

private static void dumpSheet(HSSFSheet sheet) {
    if (sheet == null) {
        log.debug("The sheet to dump is null!");
        return;//from w  ww  .  ja v a 2s.c o  m
    }

    for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
        log.debug("row#" + i + "=");
        HSSFRow row = sheet.getRow(i);
        if (row == null) {
            log.debug("\t| " + NULL_RAPPRESENTATION);
            continue;
        }
        for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
            HSSFCell cell = row.getCell((short) j);
            String cellValue = getCellValue(cell);
            log.debug("\t| " + cellValue);
        }
        log.debug("\n");
    }
}

From source file:javaexecelprocess.JavaExecelProcess.java

public long[] getFieldsLen() {

    //        Integer dat = new Integer(7);
    int iCols = getColumns();
    long[] fieldsLen = new long[iCols];
    for (int i = 0; i < iCols; i++) {
        fieldsLen[i] = -1;//w w w . j a v a 2 s.c o  m
    }
    HSSFSheet activeSheet = wb.getSheetAt(0);
    int iFirstRow = activeSheet.getFirstRowNum();
    int iLastRow = activeSheet.getLastRowNum();
    for (int i = iFirstRow + 1; i <= iLastRow; i++) {
        HSSFRow row = activeSheet.getRow(i);
        int iFirstCol = row.getFirstCellNum();
        int iLastCol = row.getLastCellNum();
        for (int j = iFirstCol; j < iLastCol; j++) {
            HSSFCell cell = row.getCell(j);
            int cellType = cell.getCellType();
            if (HSSFCell.CELL_TYPE_STRING == cellType) {
                long tmpLen = cell.getStringCellValue().length();
                if (fieldsLen[j - iFirstCol] < tmpLen) {
                    fieldsLen[j - iFirstCol] = tmpLen;
                }
            } else if (HSSFCell.CELL_TYPE_NUMERIC == cellType) {
                fieldsLen[j - iFirstCol] = -1;
            } else {

            }
        }
    }

    return fieldsLen;
}

From source file:javaexecelprocess.JavaExecelProcess.java

public boolean isDBFormat() {
    boolean ret = true;
    HSSFSheet activeSheet = wb.getSheetAt(0);
    int iFirstRow = activeSheet.getFirstRowNum();
    int iLastRow = activeSheet.getLastRowNum();
    List<Integer> fieldsType = getFieldsType();
    if (null == fieldsType) {
        ret = false;//from   w  w  w  . ja v  a 2s  . c o m
        return ret;
    }
    for (int i = iFirstRow + 1; i <= iLastRow; i++) {
        HSSFRow row = activeSheet.getRow(i);
        int iFirstCol = row.getFirstCellNum();
        int iLastCol = row.getLastCellNum();
        for (int j = iFirstCol; j < iLastCol; j++) {
            HSSFCell cell = row.getCell(j);
            //                String cessStr = cell.toString();
            int cellType = cell.getCellType();
            //                if(HSSFCell.CELL_TYPE_BLANK == cellType
            //                        || HSSFCell.CELL_TYPE_ERROR == cellType){
            Integer colType = fieldsType.get(j);
            if (colType.intValue() != cellType) {
                ret = false;
                break;
            }
        }

        if (false == ret) {
            break;
        }
    }

    return ret;
}

From source file:javaexecelprocess.JavaExecelProcess.java

private List<Integer> getFieldsType() {
    List<Integer> fieldsType = null;

    HSSFSheet activeSheet = wb.getSheetAt(0);
    int iFirstRow = activeSheet.getFirstRowNum();
    int iLastRow = activeSheet.getLastRowNum();
    HSSFRow row = activeSheet.getRow(iFirstRow + 1);
    int iFirstCol = row.getFirstCellNum();
    int iLastCol = row.getLastCellNum();
    int iCols = row.getPhysicalNumberOfCells();
    if (0 != iCols) {
        fieldsType = new ArrayList<>();
    }//w  w  w.  j  av a  2s  .  c  om
    for (int j = iFirstCol; j < iLastCol; j++) {
        HSSFCell cell = row.getCell(j);
        int cellType = cell.getCellType();
        fieldsType.add(cellType);
    }

    return fieldsType;
    //        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

From source file:javaexecelprocess.JavaExecelProcess.java

public void insertDataFromExcel() {
    String templateInsert = "INSERT INTO `mytbl1` (`id`, `myf1`, `myf2`) VALUES (NULL, 'h111', 'h222');";
    //        String templateInsert = "INSERT INTO `mytbl1` (`id`, `myf1`, `myf2`) VALUES ('2', 'h111', 'h222');";
    String sqlHead = "";
    sqlHead += "INSERT INTO `" + fields.get(0) + "` (`id`,";
    int i = 0;//from   ww  w .j a  v a2 s  . c o  m
    for (i = 0; i < fields.size() - 2; i++) {
        sqlHead += " `" + fields.get(i + 1) + "`,";
    }
    sqlHead += " `" + fields.get(i + 1) + "`) VALUES (NULL,";

    PreparedStatement pst = null;
    HSSFSheet activeSheet = wb.getSheetAt(0);
    int iFirstRow = activeSheet.getFirstRowNum();
    int iLastRow = activeSheet.getLastRowNum();
    for (i = iFirstRow + 1; i <= iLastRow; i++) {
        String sql = sqlHead;
        HSSFRow row = activeSheet.getRow(i);
        int iFirstCol = row.getFirstCellNum();
        int iLastCol = row.getLastCellNum();
        int j = 0;
        for (j = iFirstCol; j < iLastCol - 1; j++) {
            HSSFCell cell = row.getCell(j);
            String cessStr = cell.toString();
            sql += " '" + cessStr + "',";
        }
        HSSFCell cell = row.getCell(j);
        String cessStr = cell.toString();
        sql += " '" + cessStr + "');";
        try {
            pst = mysqlConn.prepareStatement(sql);
            pst.execute();
        } catch (SQLException ex) {
            Logger.getLogger(JavaExecelProcess.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("insert data exception.");
        }
    }
    //        sql += "INSERT INTO `mytbl1` (`id`, `myf1`, `myf2`) VALUES (NULL, 'f1111', 'f2222');";

}