org.apache.poi.ss.usermodel.Row.getCell()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(11.4k)|赞(0)|评价(0)|浏览(398)

本文整理了Java中org.apache.poi.ss.usermodel.Row.getCell方法的一些代码示例,展示了Row.getCell的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Row.getCell方法的具体详情如下:
包路径:org.apache.poi.ss.usermodel.Row
类名称:Row
方法名:getCell

Row.getCell介绍

[英]Get the cell representing a given column (logical cell) 0-based. If you ask for a cell that is not defined....you get a null.
[中]从0获取表示给定列(逻辑单元)的单元。如果你要一个未定义的单元格。。。。你会得到一个空值。

代码示例

代码示例来源:origin: org.apache.poi/poi

/**
 * Return the cell, without taking account of merged regions.
 * <p>
 * Use {@link #getCellWithMerges(Sheet, int, int)} if you want the top left
 * cell from merged regions instead when the reference is a merged cell.
 * <p>
 * Use this where you want to know if the given cell is explicitly defined
 * or not.
 *
 * @param sheet The workbook sheet to look at.
 * @param rowIx The 0-based index of the row.
 * @param colIx The 0-based index of the cell.
 * @return cell at the given location, or null if not defined
 * @throws NullPointerException if sheet is null
 */
public static Cell getCell(Sheet sheet, int rowIx, int colIx) {
  Row r = sheet.getRow(rowIx);
  if (r != null) {
    return r.getCell(colIx);
  }
  return null;
}

代码示例来源:origin: pentaho/pentaho-kettle

public KCell getCell( int colnr, int rownr ) {
  Row row = sheet.getRow( rownr );
  if ( row == null ) {
   return null;
  }
  Cell cell = row.getCell( colnr );
  if ( cell == null ) {
   return null;
  }
  return new PoiCell( cell );
 }
}

代码示例来源:origin: stackoverflow.com

InputStream inp = new FileInputStream("wb.xls");
 Workbook wb = WorkbookFactory.create(inp);
 Sheet sheet = wb.getSheetAt([sheet index]);
 Row row = sheet.getRow([row index]);
 Cell cell = row.getCell([cell index]);
 String cellContents = cell.getStringCellValue(); 
 //Modify the cellContents here
 // Write the output to a file
 cell.setCellValue(cellContents); 
 FileOutputStream fileOut = new FileOutputStream("wb.xls");
 wb.write(fileOut);
 fileOut.close();

代码示例来源:origin: looly/hutool

/**
 * 获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回<code>null</code>
 * 
 * @param x X坐标,从0计数,既列号
 * @param y Y坐标,从0计数,既行号
 * @param isCreateIfNotExist 单元格不存在时是否创建
 * @return {@link Cell}
 * @since 4.0.6
 */
public Cell getCell(int x, int y, boolean isCreateIfNotExist) {
  final Row row = isCreateIfNotExist ? RowUtil.getOrCreateRow(this.sheet, y) : this.sheet.getRow(y);
  if (null != row) {
    return isCreateIfNotExist ? CellUtil.getOrCreateCell(row, x) : row.getCell(x);
  }
  return null;
}

代码示例来源:origin: looly/hutool

/**
 * 获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回<code>null</code>
 * 
 * @param x X坐标,从0计数,既列号
 * @param y Y坐标,从0计数,既行号
 * @param isCreateIfNotExist 单元格不存在时是否创建
 * @return {@link Cell}
 * @since 4.0.6
 */
public Cell getCell(int x, int y, boolean isCreateIfNotExist) {
  final Row row = isCreateIfNotExist ? RowUtil.getOrCreateRow(this.sheet, y) : this.sheet.getRow(y);
  if (null != row) {
    return isCreateIfNotExist ? CellUtil.getOrCreateCell(row, x) : row.getCell(x);
  }
  return null;
}

代码示例来源:origin: stackoverflow.com

Sheet sheet = workbook.getSheet("MyInterestingSheet");
CellReference ref = new CellReference("B12");
Row r = sheet.getRow(ref.getRow());
if (r != null) {
 Cell c = r.getCell(ref.getCol());
}

代码示例来源:origin: org.apache.poi/poi

public void copyUpdatedCells(Sheet sheet) {
  RowColKey[] keys = new RowColKey[_sharedCellsByRowCol.size()];
  _sharedCellsByRowCol.keySet().toArray(keys);
  Arrays.sort(keys);
  for (int i = 0; i < keys.length; i++) {
    RowColKey key = keys[i];
    Row row = sheet.getRow(key.getRowIndex());
    if (row == null) {
      row = sheet.createRow(key.getRowIndex());
    }
    Cell destCell = row.getCell(key.getColumnIndex());
    if (destCell == null) {
      destCell = row.createCell(key.getColumnIndex());
    }
    ForkedEvaluationCell srcCell = _sharedCellsByRowCol.get(key);
    srcCell.copyValue(destCell);
  }
}

代码示例来源:origin: kiegroup/optaplanner

private void readRegionList() throws IOException {
  Sheet sheet = readSheet(1, "Regions");
  Row headerRow = sheet.getRow(0);
  assertCellConstant(headerRow.getCell(0), "Name");
  assertCellConstant(headerRow.getCell(1), "Quantity maximum");
  List<Region> regionList = new ArrayList<>();
  regionMap = new LinkedHashMap<>();
  long id = 0L;
  for (Row row : sheet) {
    if (row.getRowNum() < 1) {
      continue;
    }
    if (row.getCell(0) == null && row.getCell(1) == null) {
      continue;
    }
    Region region = new Region();
    region.setId(id);
    id++;
    region.setName(readStringCell(row.getCell(0)));
    region.setQuantityMillisMaximum(parsePercentageMillis(readDoubleCell(row.getCell(1))));
    regionList.add(region);
    regionMap.put(region.getName(), region);
  }
  solution.setRegionList(regionList);
}

代码示例来源:origin: kiegroup/optaplanner

private void readSectorList() throws IOException {
  Sheet sheet = readSheet(2, "Sectors");
  Row headerRow = sheet.getRow(0);
  assertCellConstant(headerRow.getCell(0), "Name");
  assertCellConstant(headerRow.getCell(1), "Quantity maximum");
  List<Sector> sectorList = new ArrayList<>();
  sectorMap = new LinkedHashMap<>();
  long id = 0L;
  for (Row row : sheet) {
    if (row.getRowNum() < 1) {
      continue;
    }
    if (row.getCell(0) == null && row.getCell(1) == null) {
      continue;
    }
    Sector sector = new Sector();
    sector.setId(id);
    id++;
    sector.setName(readStringCell(row.getCell(0)));
    sector.setQuantityMillisMaximum(parsePercentageMillis(readDoubleCell(row.getCell(1))));
    sectorList.add(sector);
    sectorMap.put(sector.getName(), sector);
  }
  solution.setSectorList(sectorList);
}

代码示例来源:origin: org.apache.poi/poi

protected CellValue getCellValueAt(int index) {
    if (index < 0 || index >= numOfCells) {
      throw new IndexOutOfBoundsException("Index must be between 0 and " +
          (numOfCells - 1) + " (inclusive), given: " + index);
    }
    int firstRow = cellRangeAddress.getFirstRow();
    int firstCol = cellRangeAddress.getFirstColumn();
    int lastCol = cellRangeAddress.getLastColumn();
    int width = lastCol - firstCol + 1;
    int rowIndex = firstRow + index / width;
    int cellIndex = firstCol + index % width;
    Row row = sheet.getRow(rowIndex);
    return (row == null) ? null : evaluator.evaluate(row.getCell(cellIndex));
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

public KCell[] getRow( int rownr ) {
 if ( rownr < sheet.getFirstRowNum() ) {
  return new KCell[] {};
 } else if ( rownr > sheet.getLastRowNum() ) {
  throw new ArrayIndexOutOfBoundsException( "Read beyond last row: " + rownr );
 }
 Row row = sheet.getRow( rownr );
 if ( row == null ) { // read an empty row
  return new KCell[] {};
 }
 int cols = row.getLastCellNum();
 if ( cols < 0 ) { // this happens if a row has no cells, POI returns -1 then
  return new KCell[] {};
 }
 PoiCell[] xlsCells = new PoiCell[cols];
 for ( int i = 0; i < cols; i++ ) {
  Cell cell = row.getCell( i );
  if ( cell != null ) {
   xlsCells[i] = new PoiCell( cell );
  }
 }
 return xlsCells;
}

代码示例来源:origin: kiegroup/optaplanner

private void readAssetClassList() throws IOException {
  Sheet sheet = readSheet(3, "AssetClasses");
  final int ASSET_CLASS_PROPERTIES_COUNT = 6;
  Row groupHeaderRow = sheet.getRow(0);
  assertCellConstant(groupHeaderRow.getCell(0), "Asset class");
  assertCellConstant(groupHeaderRow.getCell(ASSET_CLASS_PROPERTIES_COUNT), "Correlation");
  Row headerRow = sheet.getRow(1);
  assertCellConstant(headerRow.getCell(0), "ID");
  assertCellConstant(headerRow.getCell(1), "Name");
  assertCellConstant(headerRow.getCell(2), "Region");
  assertCellConstant(headerRow.getCell(3), "Sector");
  assertCellConstant(headerRow.getCell(4), "Expected return");
  assertCellConstant(headerRow.getCell(5), "Standard deviation");
  for (int i = 0; i < assetClassListSize; i++) {
    AssetClass assetClass = new AssetClass();
    assetClass.setId(readLongCell(headerRow.getCell(ASSET_CLASS_PROPERTIES_COUNT + i)));
    assetClassList.add(assetClass);
    AssetClass old = idToAssetClassMap.put(assetClass.getId(), assetClass);
    if (row.getCell(0) == null && row.getCell(1) == null && row.getCell(2) == null
        && row.getCell(3) == null && row.getCell(4) == null && row.getCell(5) == null) {
      continue;
    long id = readLongCell(row.getCell(0));
    AssetClass assetClass = idToAssetClassMap.get(id);
    if (assetClass == null) {
    assetClass.setName(readStringCell(row.getCell(1)));
    String regionName = readStringCell(row.getCell(2));

代码示例来源:origin: stackoverflow.com

Row row = sheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getCol());

代码示例来源:origin: org.apache.poi/poi

final Row row = sheet.getRow(r);
if (row == null) {
  continue; // no cells to check
  final Cell cell = row.getCell(c);
  if (cell == null) {
    continue;

代码示例来源:origin: kiegroup/optaplanner

private void readParametrization() throws IOException {
  Sheet sheet = readSheet(0, "Parametrization");
  assertCellConstant(sheet.getRow(0).getCell(0), "Investment parametrization");
  InvestmentParametrization parametrization = new InvestmentParametrization();
  parametrization.setId(0L);
  parametrization.setStandardDeviationMillisMaximum(
      parsePercentageMillis(readDoubleParameter(sheet.getRow(1), "Standard deviation maximum")));
  solution.setParametrization(parametrization);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
@SuppressWarnings("resource")
public void testXlsxStreamingView() throws Exception {
  View excelView = new AbstractXlsxStreamingView() {
    @Override
    protected void buildExcelDocument(Map<String, Object> model, Workbook workbook,
        HttpServletRequest request, HttpServletResponse response) throws Exception {
      Sheet sheet = workbook.createSheet("Test Sheet");
      Row row = sheet.createRow(0);
      Cell cell = row.createCell(0);
      cell.setCellValue("Test Value");
    }
  };
  excelView.render(new HashMap<>(), request, response);
  Workbook wb = new XSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
  assertEquals("Test Sheet", wb.getSheetName(0));
  Sheet sheet = wb.getSheet("Test Sheet");
  Row row = sheet.getRow(0);
  Cell cell = row.getCell(0);
  assertEquals("Test Value", cell.getStringCellValue());
}

代码示例来源:origin: org.apache.poi/poi

final Row row = sheet.getRow(ref.getRow());
if (row != null) {
  cell = row.getCell(ref.getCol());

代码示例来源:origin: spring-projects/spring-framework

@Test
@SuppressWarnings("resource")
public void testXls() throws Exception {
  View excelView = new AbstractXlsView() {
    @Override
    protected void buildExcelDocument(Map<String, Object> model, Workbook workbook,
        HttpServletRequest request, HttpServletResponse response) throws Exception {
      Sheet sheet = workbook.createSheet("Test Sheet");
      Row row = sheet.createRow(0);
      Cell cell = row.createCell(0);
      cell.setCellValue("Test Value");
    }
  };
  excelView.render(new HashMap<>(), request, response);
  Workbook wb = new HSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
  assertEquals("Test Sheet", wb.getSheetName(0));
  Sheet sheet = wb.getSheet("Test Sheet");
  Row row = sheet.getRow(0);
  Cell cell = row.getCell(0);
  assertEquals("Test Value", cell.getStringCellValue());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
@SuppressWarnings("resource")
public void testXlsxView() throws Exception {
  View excelView = new AbstractXlsxView() {
    @Override
    protected void buildExcelDocument(Map<String, Object> model, Workbook workbook,
        HttpServletRequest request, HttpServletResponse response) throws Exception {
      Sheet sheet = workbook.createSheet("Test Sheet");
      Row row = sheet.createRow(0);
      Cell cell = row.createCell(0);
      cell.setCellValue("Test Value");
    }
  };
  excelView.render(new HashMap<>(), request, response);
  Workbook wb = new XSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
  assertEquals("Test Sheet", wb.getSheetName(0));
  Sheet sheet = wb.getSheet("Test Sheet");
  Row row = sheet.getRow(0);
  Cell cell = row.getCell(0);
  assertEquals("Test Value", cell.getStringCellValue());
}

代码示例来源:origin: pentaho/pentaho-kettle

/**
 * @param reference
 * @return the cell the refernce points to
 */
private Cell getCellFromReference( String reference ) {
 CellReference cellRef = new CellReference( reference );
 String sheetName = cellRef.getSheetName();
 Sheet sheet = data.sheet;
 if ( !Utils.isEmpty( sheetName ) ) {
  sheet = data.wb.getSheet( sheetName );
 }
 if ( sheet == null ) {
  return null;
 }
 // reference is assumed to be absolute
 Row xlsRow = sheet.getRow( cellRef.getRow() );
 if ( xlsRow == null ) {
  return null;
 }
 Cell styleCell = xlsRow.getCell( cellRef.getCol() );
 return styleCell;
}

相关文章