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

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

Introduction

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

Prototype

@Override
public HSSFCell getCell(int cellnum) 

Source Link

Document

Get the hssfcell representing a given column (logical cell) 0-based.

Usage

From source file:com.vportal.portlet.vdoc.service.util.ReportUtil.java

License:Open Source License

public static void createCellBold(HSSFRow row, short column, String value, HSSFWorkbook wb) {

    HSSFCellStyle style = wb.createCellStyle();
    HSSFFont font = wb.createFont();//from   w  w w. j a v a2  s  .  c  o m
    font.setColor((short) 0xc);
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    style.setFont(font);
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    style.setBorderBottom(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderLeft(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderRight(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderTop(HSSFCellStyle.SOLID_FOREGROUND);

    HSSFCell cell = row.getCell(column);
    if (cell == null)
        cell = row.createCell(column);
    //cell.setEncoding(wb.ENCODING_UTF_16);
    cell.setCellValue(value);
    cell.setCellStyle(style);
}

From source file:com.vportal.portlet.vdoc.service.util.ReportUtil.java

License:Open Source License

public static void createCell(HSSFRow row, short column, int value, HSSFWorkbook wb) {
    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    style.setBorderBottom(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderLeft(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderRight(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderTop(HSSFCellStyle.SOLID_FOREGROUND);

    HSSFCell cell = row.getCell(column);
    if (cell == null)
        cell = row.createCell(column);/*from   w w  w  . j a va2 s.c  o  m*/
    cell.setCellValue(value);
    cell.setCellStyle(style);
}

From source file:com.wangzhu.poi.ExcelToHtmlConverter.java

License:Apache License

/**
 * @return maximum 1-base index of column that were rendered, zero if none
 *//*from   w ww.ja v a 2s  . c  om*/
protected int processRow(CellRangeAddress[][] mergedRanges, HSSFRow row, Element tableRowElement) {
    final HSSFSheet sheet = row.getSheet();
    final short maxColIx = row.getLastCellNum();
    if (maxColIx <= 0) {
        return 0;
    }

    final List emptyCells = new ArrayList(maxColIx);

    if (this.isOutputRowNumbers()) {
        Element tableRowNumberCellElement = this.htmlDocumentFacade.createTableHeaderCell();
        this.processRowNumber(row, tableRowNumberCellElement);
        emptyCells.add(tableRowNumberCellElement);
    }

    int maxRenderedColumn = 0;
    for (int colIx = 0; colIx < maxColIx; colIx++) {
        if (!this.isOutputHiddenColumns() && sheet.isColumnHidden(colIx)) {
            continue;
        }

        CellRangeAddress range = AbstractExcelUtils.getMergedRange(mergedRanges, row.getRowNum(), colIx);

        if ((range != null)
                && ((range.getFirstColumn() != colIx) || (range.getFirstRow() != row.getRowNum()))) {
            continue;
        }

        HSSFCell cell = row.getCell(colIx);

        int divWidthPx = 0;
        if (this.isUseDivsToSpan()) {
            divWidthPx = AbstractExcelConverter.getColumnWidth(sheet, colIx);

            boolean hasBreaks = false;
            for (int nextColumnIndex = colIx + 1; nextColumnIndex < maxColIx; nextColumnIndex++) {
                if (!this.isOutputHiddenColumns() && sheet.isColumnHidden(nextColumnIndex)) {
                    continue;
                }

                if ((row.getCell(nextColumnIndex) != null) && !this.isTextEmpty(row.getCell(nextColumnIndex))) {
                    hasBreaks = true;
                    break;
                }

                divWidthPx += AbstractExcelConverter.getColumnWidth(sheet, nextColumnIndex);
            }

            if (!hasBreaks) {
                divWidthPx = Integer.MAX_VALUE;
            }
        }

        Element tableCellElement = this.htmlDocumentFacade.createTableCell();

        if (range != null) {
            if (range.getFirstColumn() != range.getLastColumn()) {
                tableCellElement.setAttribute("colspan",
                        String.valueOf((range.getLastColumn() - range.getFirstColumn()) + 1));
            }
            if (range.getFirstRow() != range.getLastRow()) {
                tableCellElement.setAttribute("rowspan",
                        String.valueOf((range.getLastRow() - range.getFirstRow()) + 1));
            }
        }

        boolean emptyCell;
        if (cell != null) {
            emptyCell = this.processCell(cell, tableCellElement,
                    AbstractExcelConverter.getColumnWidth(sheet, colIx), divWidthPx, row.getHeight() / 20f);
        } else {
            emptyCell = true;
        }

        if (emptyCell) {
            emptyCells.add(tableCellElement);
        } else {
            for (Iterator iterator = emptyCells.iterator(); iterator.hasNext();) {
                Element emptyCellElement = (Element) iterator.next();
                tableRowElement.appendChild(emptyCellElement);
            }
            emptyCells.clear();

            tableRowElement.appendChild(tableCellElement);
            maxRenderedColumn = colIx;
        }
    }

    return maxRenderedColumn + 1;
}

From source file:com.weibo.datasys.parser.office.extractor.ExcelParse.java

License:Open Source License

private FileData extractor(File filePath) {
    FileData fData = new FileData();
    fData.setName(filePath.getName());//from   w  w w  . j av  a2 s .  c o m
    StringBuffer sBuffer = new StringBuffer();
    HSSFWorkbook workbook = null;
    try {
        workbook = new HSSFWorkbook(new FileInputStream(filePath));
        for (int iSheets = 0; iSheets < workbook.getNumberOfSheets(); ++iSheets) {
            HSSFSheet sheet = workbook.getSheetAt(iSheets);
            for (int iRow = 0; iRow < sheet.getLastRowNum(); ++iRow) {
                HSSFRow row = sheet.getRow(iRow);
                for (int iCell = 0; iCell < row.getLastCellNum(); ++iCell) {
                    HSSFCell cell = row.getCell(iCell);
                    if (null != cell) {
                        if (0 == cell.getCellType()) {
                            sBuffer.append(String.valueOf(cell.getNumericCellValue()));
                            sBuffer.append(SEGMENT_CHAR);
                        } else if (1 == cell.getCellType()) {
                            sBuffer.append(cell.getStringCellValue().trim());
                            sBuffer.append(SEGMENT_CHAR);
                        }
                    }
                }
            }
        }
        fData.setContent(sBuffer.toString());
    } catch (Exception e) {
        LOG.error("", e);
    }
    return fData;
}

From source file:com.xhsoft.framework.common.file.ExcelHandle.java

License:Open Source License

/**
 * ????.xls/*from w w  w . j a  v  a 2  s .  co m*/
 * @params {:,:}
 * @return void
 * @author lijiangwei
 * @since 2012-11-12
 */
private void readXLS() {
    for (int sheetNum = 0; sheetNum < xls.getNumberOfSheets(); sheetNum++) {
        HSSFSheet xls_sheet = xls.getSheetAt(sheetNum);
        if (xls_sheet == null) {
            continue;
        }
        // ??Sheet
        for (int rowNum = 0; rowNum <= xls_sheet.getLastRowNum(); rowNum++) {
            HSSFRow xls_row = xls_sheet.getRow(rowNum);
            if (xls_row == null) {
                continue;
            }
            // ????
            for (int cellNum = 0; cellNum < xls_row.getLastCellNum(); cellNum++) {
                HSSFCell xls_cell = xls_row.getCell(cellNum);
                if (xls_cell == null) {
                    continue;
                }
                System.out.print("   " + getCellValue(xls_cell));
            }
            System.out.println();
        }
    }
}

From source file:com.xpn.xwiki.plugin.lucene.textextraction.MSExcelTextExtractor.java

License:Open Source License

/**
 * It will parse row and return the text
 *//*from  w  w w.j a v a  2s  .com*/
private void parseRow(HSSFRow row, StringBuffer cleanBuffer) {
    short firstCell = row.getFirstCellNum();
    short lastCell = row.getLastCellNum();
    for (short cellIdx = firstCell; cellIdx <= lastCell; cellIdx++) {
        HSSFCell cell = row.getCell(cellIdx);

        if (cell != null) {
            parseCell(cell, cleanBuffer);
        }
    }
}

From source file:com.xx.platform.util.tools.ms.ExcelExtractor.java

License:Apache License

public String extractText(POIFSFileSystem poifs) throws Exception {
    StringBuffer resultText = new StringBuffer();
    HSSFWorkbook wb = new HSSFWorkbook(poifs, true);
    if (wb == null) {
        return "";
    }//w  ww  .  j ava 2s.  com

    HSSFSheet sheet;
    HSSFRow row;
    HSSFCell cell;
    int sNum = 0;
    int rNum = 0;
    int cNum = 0;

    sNum = wb.getNumberOfSheets();

    for (int i = 0; i < sNum; i++) {
        if ((sheet = wb.getSheetAt(i)) == null) {
            continue;
        }
        rNum = sheet.getLastRowNum();

        for (int j = 0; j <= rNum; j++) {
            if ((row = sheet.getRow(j)) == null) {
                continue;
            }
            cNum = row.getLastCellNum();

            for (int k = 0; k < cNum; k++) {
                if ((cell = row.getCell((short) k)) != null) {
                    /*if(HSSFDateUtil.isCellDateFormatted(cell) == true) {
                        resultText += cell.getDateCellValue().toString() + " ";
                      } else
                     */
                    if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                        resultText.append(cell.getStringCellValue());
                    } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                        Double d = new Double(cell.getNumericCellValue());
                        resultText.append(d.toString());
                    }
                    /* else if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){
                         resultText += cell.getCellFormula() + " ";
                       }
                     */
                }
            }
        }
    }
    return resultText.toString();
}

From source file:com.xx.platform.util.tools.ms.ExcelExtrator.java

License:Apache License

public String extractText(POIFSFileSystem poifs) throws Exception {
    List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    String text = null;//from w  w  w  .j ava 2s. c om
    try {
        HSSFWorkbook wb = new HSSFWorkbook(poifs, true);
        if (wb == null) {
            return null;
        }

        HSSFSheet sheet;
        HSSFRow row;
        HSSFCell cell;
        int sNum = 0;
        int rNum = 0;
        int cNum = 0;

        sNum = wb.getNumberOfSheets();
        for (int i = 0; i < sNum; i++) {
            if ((sheet = wb.getSheetAt(i)) == null) {
                continue;
            }
            String[] key = null; //field
            boolean init = false; //key 
            rNum = sheet.getLastRowNum();
            for (int j = 0; j <= rNum; j++) {
                if ((row = sheet.getRow(j)) == null) {
                    continue;
                }
                Map<String, String> rowdata = new HashMap<String, String>();
                cNum = row.getLastCellNum();
                if (!init)
                    key = new String[cNum];
                String value = "";
                StringBuffer content = new StringBuffer();
                for (int k = 0; k < cNum; k++) {
                    if ((cell = row.getCell((short) k)) != null) {
                        if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                            value = cell.getStringCellValue();
                        } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                            Double d = new Double(cell.getNumericCellValue());
                            value = d.toString();
                        } else
                            value = "";
                        if (init) {
                            content.append(value);
                        } else {
                            key[k] = value;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        text = "";
    }
    return text;

}

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

private static List<List<Object>> readXLSWithHeader(InputStream inputStream) throws IOException {
    //        InputStream is = new FileInputStream(file);
    HSSFWorkbook wb = new HSSFWorkbook(inputStream);
    HSSFSheet sheet = wb.getSheetAt(0);//w w w  .j a  v a  2 s . c  o m

    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++) {
        HSSFRow 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>> readXLS(InputStream inputStream) throws IOException {
    //        InputStream is = new FileInputStream(file);
    HSSFWorkbook wb = new HSSFWorkbook(inputStream);
    HSSFSheet sheet = wb.getSheetAt(0);//from w w w . j  a v a2s  . com

    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++) {
        HSSFRow 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;
}