Example usage for org.apache.poi.xssf.usermodel XSSFRow getCell

List of usage examples for org.apache.poi.xssf.usermodel XSSFRow getCell

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFRow getCell.

Prototype

@Override
public XSSFCell getCell(int cellnum) 

Source Link

Document

Returns the cell at the given (0 based) index, with the org.apache.poi.ss.usermodel.Row.MissingCellPolicy from the parent Workbook.

Usage

From source file:com.yanglb.utilitys.codegen.core.reader.impl.DmlReaderImpl.java

License:Apache License

/**
 * ?DBSheet//from  w w  w . jav  a  2 s.c  o  m
 * @throws CodeGenException 
 */
@Override
protected DmlModel onReader(XSSFSheet sheet) throws CodeGenException {
    DmlModel model = super.onReader(sheet, DmlModel.class);
    XSSFRow row = null;

    // ??
    row = sheet.getRow(1);
    model.setNameSpace(this.getCellStringValue(row.getCell(2)));
    model.setAuthor(this.getCellStringValue(row.getCell(4)));
    model.setVersion(this.getCellStringValue(row.getCell(6)));
    model.setDescription(this.getCellStringValue(row.getCell(8)));

    row = sheet.getRow(2);
    model.setName(this.getCellStringValue(row.getCell(2)));
    model.setResponsibility(this.getCellStringValue(row.getCell(4)));
    model.setRenewDate(this.getCellStringValue(row.getCell(6)));

    // ?
    model.setDmlData(this.readerTable(sheet));
    return model;
}

From source file:com.yanglb.utilitys.codegen.core.reader.impl.HashMapReaderImpl.java

License:Apache License

/**
 * ?Sheet//from   www  . jav  a 2 s .c  om
 */
@Override
protected HashMap<String, String> onReader(XSSFSheet sheet) {
    HashMap<String, String> result = new HashMap<String, String>();

    for (int row = this.startRowNo; row < sheet.getLastRowNum(); row++) {
        XSSFRow xssfRow = sheet.getRow(row);
        String key = this.getCellStringValue(xssfRow.getCell(this.startColNo));
        // key?Map
        if (xssfRow.getCell(this.startColNo).getCellType() == XSSFCell.CELL_TYPE_BLANK
                || StringUtility.isNullOrEmpty(key)) {
            continue;
        }
        String value = this.getCellStringValue(xssfRow.getCell(this.startColNo + 1));
        result.put(key, value);
    }
    return result;
}

From source file:com.yanglb.utilitys.codegen.core.reader.impl.TableReaderImpl.java

License:Apache License

/**
 * ?DBSheet/*from  w  w  w .  j a  v  a2s .co m*/
 * @throws CodeGenException
 */
@Override
protected TableModel onReader(XSSFSheet sheet) throws CodeGenException {
    TableModel model = super.onReader(sheet, TableModel.class);

    // KEY
    String[] keys = null;
    List<String> listKey = new ArrayList<String>();
    Map<String, String> curValue = null; // ?

    for (int rowNo = this.startRowNo; rowNo <= sheet.getLastRowNum(); rowNo++) {
        XSSFRow row = sheet.getRow(rowNo);
        // KEY
        if (rowNo == this.startRowNo) {
            keys = new String[row.getLastCellNum()];
        } else {
            curValue = new HashMap<String, String>();
        }

        // ? ??
        boolean allBlank = true;

        // ?
        if (row == null) {
            throw new CodeGenException(String.format("error: sheet(%s), row(%d)", sheet.getSheetName(), rowNo));
        }
        for (int colNo = this.startColNo; colNo < row.getLastCellNum(); colNo++) {
            XSSFCell cell = row.getCell(colNo);
            if (cell == null) {
                throw new CodeGenException(String.format("error: sheet(%s), row(%d), col(%d)",
                        sheet.getSheetName(), rowNo, colNo));
            }
            if (cell.getCellType() != XSSFCell.CELL_TYPE_BLANK
                    && cell.getCellType() != XSSFCell.CELL_TYPE_ERROR) {
                allBlank = false;
            }
            String value = this.getCellStringValue(cell);

            // KEY
            if (rowNo == this.startRowNo) {
                if (StringUtility.isNullOrEmpty(value) || cell.getCellType() == XSSFCell.CELL_TYPE_BLANK
                        || "-".equals(value)) {
                    // /
                    continue;
                }
                keys[colNo] = value;
                listKey.add(value);
            } else {
                if (colNo >= keys.length)
                    continue;

                // 
                String key = keys[colNo];
                if (!StringUtility.isNullOrEmpty(key)) {
                    curValue.put(keys[colNo], value);
                }
            }
        }

        // ?
        if (!allBlank && curValue != null) {
            model.insert(curValue);
        }
    }

    // 
    model.setColumns((String[]) listKey.toArray(new String[0]));
    return model;
}

From source file:com.yyl.common.utils.excel.ExcelTools.java

private static List<List<Object>> readXLSXWithHeader(InputStream inputStream) throws IOException {
    //        InputStream is = new FileInputStream(file);
    XSSFWorkbook wb = new XSSFWorkbook(inputStream);
    XSSFSheet sheet = wb.getSheetAt(0);//from   ww  w. jav  a  2s  .  com

    List<List<Object>> o = new ArrayList();
    List<Object> temp = null;
    if (sheet == null) {
        return null;
    }

    for (int row_index = 0; row_index <= sheet.getLastRowNum(); row_index++) {
        XSSFRow row = sheet.getRow(row_index);
        if (row == null) {
            continue;
        }
        temp = new ArrayList();
        for (int col_index = 0; col_index <= row.getLastCellNum(); col_index++) {
            temp.add(getCellValue(row.getCell(col_index)));
        }
        o.add(temp);
    }

    return o;
}

From source file:com.yyl.common.utils.excel.ExcelTools.java

private static List<List<Object>> readXLSX(InputStream inputStream) throws IOException {
    //        InputStream is = new FileInputStream(file);
    XSSFWorkbook wb = new XSSFWorkbook(inputStream);
    XSSFSheet sheet = wb.getSheetAt(0);/*from   ww w.  j a  v a  2  s .  c  om*/

    List<List<Object>> o = new ArrayList();
    List<Object> temp = null;
    if (sheet == null) {
        return null;
    }

    for (int row_index = 1; row_index <= sheet.getLastRowNum(); row_index++) {
        XSSFRow row = sheet.getRow(row_index);
        if (row == null) {
            continue;
        }
        temp = new ArrayList();
        for (int col_index = 0; col_index <= row.getLastCellNum(); col_index++) {
            temp.add(getCellValue(row.getCell(col_index)));
        }
        o.add(temp);
    }

    return o;
}

From source file:com.yyl.common.utils.excel.ExcelTools.java

private static List<Map<String, String>> readXLSX(InputStream inputStream, Map<String, Integer> keyMaps)
        throws IOException {
    //        InputStream is = new FileInputStream(file);
    XSSFWorkbook wb = new XSSFWorkbook(inputStream);

    List<Map<String, String>> list = new ArrayList();
    Map<String, String> temp = null;
    for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
        XSSFSheet sheet = wb.getSheetAt(sheetIndex);
        if (sheet == null) {
            continue;
        }//from  www.j  a va  2  s.c  o m
        for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
            XSSFRow row = sheet.getRow(rowIndex);
            temp = new HashMap();
            if (row != null) {
                for (Entry<String, Integer> entry : keyMaps.entrySet()) {
                    int index = entry.getValue();
                    XSSFCell cell = row.getCell(index);
                    temp.put(entry.getKey(), getCellValue(cell));
                }
            }
            list.add(temp);
        }
    }
    return list;
}

From source file:control.DocFile.java

public HangHoa layMotHangHoa(int rowNum) {
    XSSFRow row = sheet.getRow(rowNum);
    XSSFCell maHangHoa = row.getCell(SP_MA_HANG_HOA);
    XSSFCell tenHangHoa = row.getCell(SP_TEN_HANG_HOA);
    XSSFCell nhomHangHoa = row.getCell(SP_NHOM_HANG_HOA);
    XSSFCell giaVon = row.getCell(SP_GIA_VON);
    XSSFCell giaBan = row.getCell(SP_GIA_BAN);
    XSSFCell tonKho = row.getCell(SP_TON_KHO);
    XSSFCell dinhMucTonItNhat = row.getCell(SP_DINH_MUC_TON_IT_NHAT);
    XSSFCell dinhMucTonNhieuNhat = row.getCell(SP_DINH_MUC_TON_NHIEU_NHAT);

    return new HangHoa(maHangHoa, tenHangHoa, giaBan, giaVon, tonKho, nhomHangHoa, dinhMucTonItNhat,
            dinhMucTonNhieuNhat);/*  w  ww . j a va2  s. co m*/
}

From source file:control.DocFile.java

public HangNhap layMotHangNhap(int rowNum) {
    XSSFRow row = sheet.getRow(rowNum);
    XSSFCell maHangHoa = row.getCell(0);
    XSSFCell tenHangHoa = row.getCell(1);
    XSSFCell nhomHangHoa = row.getCell(2);
    XSSFCell giaVon = row.getCell(3);/* w  w  w  .j  a v  a  2 s. c o m*/
    XSSFCell soLuong = row.getCell(4);
    return new HangNhap(maHangHoa, tenHangHoa, giaVon, soLuong, nhomHangHoa);
}

From source file:Creator.TaskManagerPanel.java

private void loadDefaultTasks() {

    String path = "/Creator/textFiles/tasks.xlsx";
    InputStream loc = this.getClass().getResourceAsStream(path);
    importedTasks = new ArrayList<>();
    try {// w w w  . java  2  s.com

        XSSFWorkbook wb = new XSSFWorkbook(loc);
        XSSFSheet sheet = wb.getSheetAt(0);
        XSSFRow row;
        XSSFCell cell;
        String[] rowData;
        int rows, cols; // No of rows
        rows = sheet.getPhysicalNumberOfRows();

        for (int i = 1; i < rows; i++) {

            row = sheet.getRow(i);
            if (row != null) {
                cols = row.getPhysicalNumberOfCells();
                rowData = new String[cols];

                for (int j = 0; j < cols; j++) {

                    cell = row.getCell(j);
                    if (cell != null) {
                        switch (cell.getCellType()) {
                        case 1: // string
                            rowData[j] = cell.getStringCellValue();
                            break;
                        case 2: // int
                            rowData[j] = String.valueOf(cell.getNumericCellValue());
                            break;
                        case 3: // blank
                            System.out.println("Blank data @ [" + i + "][" + j + "]");
                            rowData[j] = "no data @ [" + i + "][" + j + "]";
                            break;
                        case 4: // boolean
                            rowData[j] = String.valueOf(cell.getBooleanCellValue());
                            break;
                        case 5: // error
                            rowData[j] = String.valueOf(cell.getErrorCellString());
                            break;
                        default:
                            System.out.println("default @ [" + i + "][" + j + "]");
                            rowData[j] = "default @ [" + i + "][" + j + "]";
                            break;

                        }

                    } else {
                        System.out.println("null @ [" + i + "][" + j + "]");
                        rowData[j] = "nullValue @ [" + i + "][" + j + "]";
                    }
                }
                rowData[5] = "'" + rowData[5] + "'";
                importedTasks.add(rowData);

            }

        }

        wb.close();

    } catch (Exception e) {
        System.out.println("Error reading excel file " + e.getMessage());
    }

}

From source file:Creator.TaskManagerPanel.java

private void loadDefaultAlerts() {

    String path = "/Creator/textFiles/alerts.xlsx";
    InputStream loc = this.getClass().getResourceAsStream(path);
    importedAlerts = new ArrayList<>();
    try {/*from   ww  w .  j a v a  2s.  co m*/

        XSSFWorkbook wb = new XSSFWorkbook(loc);
        XSSFSheet sheet = wb.getSheetAt(0);
        XSSFRow row;
        XSSFCell cell;
        String[] rowData;
        int rows, cols; // No of rows
        rows = sheet.getPhysicalNumberOfRows();

        for (int i = 1; i < rows; i++) {

            row = sheet.getRow(i);
            if (row != null) {
                cols = row.getPhysicalNumberOfCells();
                rowData = new String[cols];

                for (int j = 0; j < cols; j++) {

                    cell = row.getCell(j);
                    if (cell != null) {
                        switch (cell.getCellType()) {
                        case 1: // string
                            rowData[j] = cell.getStringCellValue();
                            break;
                        case 2: // int
                            rowData[j] = String.valueOf(cell.getNumericCellValue());
                            break;
                        case 3: // blank
                            System.out.println("Blank data @ [" + i + "][" + j + "]");
                            rowData[j] = "no data @ [" + i + "][" + j + "]";
                            break;
                        case 4: // boolean
                            rowData[j] = String.valueOf(cell.getBooleanCellValue());
                            break;
                        case 5: // error
                            rowData[j] = String.valueOf(cell.getErrorCellString());
                            break;
                        default:
                            //System.out.println("default @ [" + i + "][" + j + "] = " + String.valueOf(cell.getRawValue()));
                            rowData[j] = String.valueOf(cell.getRawValue());
                            break;
                        }

                    } else {
                        System.out.println("null @ [" + i + "][" + j + "]");
                        rowData[j] = "nullValue @ [" + i + "][" + j + "]";
                    }
                }
                importedAlerts.add(rowData);

            }
        }

        wb.close();

    } catch (Exception e) {
        System.out.println("Error reading excel file " + e.getMessage());
    }

}