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

x33g5p2x  于2022-01-29 转载在 其他  
字(11.3k)|赞(0)|评价(0)|浏览(273)

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

Sheet.getWorkbook介绍

[英]Return the parent workbook
[中]返回父工作簿

代码示例

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

/**
 * 构造
 * 
 * @param sheet Excel中的sheet
 */
public ExcelBase(Sheet sheet) {
  Assert.notNull(sheet, "No Sheet provided.");
  this.sheet = sheet;
  this.workbook = sheet.getWorkbook();
}

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

/**
 * 构造
 * 
 * @param sheet Excel中的sheet
 */
public ExcelBase(Sheet sheet) {
  Assert.notNull(sheet, "No Sheet provided.");
  this.sheet = sheet;
  this.workbook = sheet.getWorkbook();
}

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

/**
 * AutoSizeColumnTracker constructor. Holds no reference to <code>sheet</code>
 *
 * @param sheet the sheet associated with this auto-size column tracker
 * @since 3.14beta1
 */
public AutoSizeColumnTracker(final Sheet sheet) {
  // If sheet needs to be saved, use a java.lang.ref.WeakReference to avoid garbage collector gridlock.
  defaultCharWidth = SheetUtil.getDefaultCharWidth(sheet.getWorkbook());
}

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

/**
 * 克隆新的{@link CellStyle}
 * 
 * @param cell 单元格
 * @param cellStyle 被复制的样式
 * @return {@link CellStyle}
 */
public static CellStyle cloneCellStyle(Cell cell, CellStyle cellStyle) {
  return cloneCellStyle(cell.getSheet().getWorkbook(), cellStyle);
}

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

/**
 * 克隆新的{@link CellStyle}
 * 
 * @param cell 单元格
 * @param cellStyle 被复制的样式
 * @return {@link CellStyle}
 */
public static CellStyle cloneCellStyle(Cell cell, CellStyle cellStyle) {
  return cloneCellStyle(cell.getSheet().getWorkbook(), cellStyle);
}

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

/**
 * Update formulas.
 */
/*package*/ static void updateFormulas(Sheet sheet, FormulaShifter formulaShifter) {
  //update formulas on the parent sheet
  updateSheetFormulas(sheet,formulaShifter);
  //update formulas on other sheets
  Workbook wb = sheet.getWorkbook();
  for(Sheet sh : wb)
  {
    if (sheet == sh) continue;
    updateSheetFormulas(sh, formulaShifter);
  }
}

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

/**
 * Compute width of a column based on a subset of the rows and return the result
 *
 * @param sheet the sheet to calculate
 * @param column    0-based index of the column
 * @param useMergedCells    whether to use merged cells
 * @param firstRow  0-based index of the first row to consider (inclusive)
 * @param lastRow   0-based index of the last row to consider (inclusive)
 * @return  the width in pixels or -1 if cell is empty
 */
public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells, int firstRow, int lastRow){
  DataFormatter formatter = new DataFormatter();
  int defaultCharWidth = getDefaultCharWidth(sheet.getWorkbook());
  double width = -1;
  for (int rowIdx = firstRow; rowIdx <= lastRow; ++rowIdx) {
    Row row = sheet.getRow(rowIdx);
    if( row != null ) {
      double cellWidth = getColumnWidthForRow(row, column, defaultCharWidth, formatter, useMergedCells);
      width = Math.max(width, cellWidth);
    }
  }
  return width;
}

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

/**
 * Take a cell, and apply a font to it
 *
 * @param cell the cell to set the alignment for
 * @param font The Font that you want to set.
 * @throws IllegalArgumentException if <tt>font</tt> and <tt>cell</tt> do not belong to the same workbook
 */
public static void setFont(Cell cell, Font font) {
  // Check if font belongs to workbook
  Workbook wb = cell.getSheet().getWorkbook();
  final int fontIndex = font.getIndexAsInt();
  if (!wb.getFontAt(fontIndex).equals(font)) {
    throw new IllegalArgumentException("Font does not belong to this workbook");
  }
  // Check if cell belongs to workbook
  // (checked in setCellStyleProperty)
  setCellStyleProperty(cell, FONT, fontIndex);
}

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

protected AreaReference getPivotArea() {
  final Workbook wb = getDataSheet().getWorkbook();
  return getPivotCacheDefinition().getPivotArea(wb);
}

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

/**
 * Return the cell's style.
 *
 * @return the cell's style. Always not-null. Default cell style has zero index and can be obtained as
 * <code>workbook.getCellStyleAt(0)</code>
 * @see org.apache.poi.ss.usermodel.Workbook#getCellStyleAt(int)
 */
@Override
public CellStyle getCellStyle()
{
  if(_style == null){
    SXSSFWorkbook wb = (SXSSFWorkbook)getRow().getSheet().getWorkbook();
    return wb.getCellStyleAt(0);
  } else {
    return _style;
  }
}

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

/**
 *  Looks for text in the cell that should be unicode, like &alpha; and provides the
 *  unicode version of it.
 *
 *@param  cell  The cell to check for unicode values
 *@return       translated to unicode
 */
public static Cell translateUnicodeValues(Cell cell) {
  String s = cell.getRichStringCellValue().getString();
  boolean foundUnicode = false;
  String lowerCaseStr = s.toLowerCase(Locale.ROOT);
  for (UnicodeMapping entry : unicodeMappings) {
    String key = entry.entityName;
    if (lowerCaseStr.contains(key)) {
      s = s.replaceAll(key, entry.resolvedValue);
      foundUnicode = true;
    }
  }
  if (foundUnicode) {
    cell.setCellValue(cell.getRow().getSheet().getWorkbook().getCreationHelper()
        .createRichTextString(s));
  }
  return cell;
}

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

protected AbstractCellRangeDataSource(Sheet sheet, CellRangeAddress cellRangeAddress) {
  this.sheet = sheet;
  // Make copy since CellRangeAddress is mutable.
  this.cellRangeAddress = cellRangeAddress.copy();
  this.numOfCells = this.cellRangeAddress.getNumberOfCells();
  this.evaluator = sheet.getWorkbook().getCreationHelper().createFormulaEvaluator();
}

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

protected void adjustCellReferencesInsideFormula(Cell cell, Sheet destSheet, int deltaX, int deltaY) {
    FormulaRecordAggregate fra = (FormulaRecordAggregate)((HSSFCell)cell).getCellValueRecord();
    int destSheetIndex = destSheet.getWorkbook().getSheetIndex(destSheet);
    Ptg[] ptgs = fra.getFormulaTokens();
    if(adjustInBothDirections(ptgs, destSheetIndex, deltaX, deltaY))
      fra.setParsedExpression(ptgs);
  }
}

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

/**
 * Creates a cell, gives it a value, and applies a style if provided
 *
 * @param  row     the row to create the cell in
 * @param  column  the column index to create the cell in
 * @param  value   The value of the cell
 * @param  style   If the style is not null, then set
 * @return         A new Cell
 */
public static Cell createCell(Row row, int column, String value, CellStyle style) {
  Cell cell = getCell(row, column);
  cell.setCellValue(cell.getRow().getSheet().getWorkbook().getCreationHelper()
      .createRichTextString(value));
  if (style != null) {
    cell.setCellStyle(style);
  }
  return cell;
}

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

private void copyRange(CellRangeAddress sourceRange, int deltaX, int deltaY, Sheet sourceClone) { //NOSONAR, it's a bit complex but monolith method, does not make much sense to divide it
  if(deltaX != 0)
    horizontalFormulaShifter = FormulaShifter.createForColumnCopy(sourceSheet.getWorkbook().getSheetIndex(sourceSheet), 
        sourceSheet.getSheetName(), sourceRange.getFirstColumn(), sourceRange.getLastColumn(), deltaX, sourceSheet.getWorkbook().getSpreadsheetVersion());
  if(deltaY != 0)
    verticalFormulaShifter = FormulaShifter.createForRowCopy(sourceSheet.getWorkbook().getSheetIndex(sourceSheet), 
        sourceSheet.getSheetName(), sourceRange.getFirstRow(), sourceRange.getLastRow(), deltaY, sourceSheet.getWorkbook().getSpreadsheetVersion());
  
  for(int rowNo = sourceRange.getFirstRow(); rowNo <= sourceRange.getLastRow(); rowNo++) {   
    Row sourceRow = sourceClone.getRow(rowNo); // copy from source copy, original source might be overridden in process!
    for (int columnIndex = sourceRange.getFirstColumn(); columnIndex <= sourceRange.getLastColumn(); columnIndex++) {  
      Cell sourceCell = sourceRow.getCell(columnIndex);
      if(sourceCell == null)
        continue;
      Row destRow = destSheet.getRow(rowNo + deltaY);
      if(destRow == null)
        destRow = destSheet.createRow(rowNo + deltaY);
      
      Cell newCell = destRow.getCell(columnIndex + deltaX);
      if(newCell != null)
        newCell.setCellType(sourceCell.getCellType());
      else newCell = destRow.createCell(columnIndex + deltaX, sourceCell.getCellType());
      cloneCellContent(sourceCell, newCell, null);
      if(newCell.getCellType() == CellType.FORMULA)
        adjustCellReferencesInsideFormula(newCell, destSheet, deltaX, deltaY);
    }
  }
}

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

/**
 * Applies the drawn borders to a Sheet. The borders that are applied are
 * the ones that have been drawn by the {@link #drawBorders} and
 * {@link #drawBorderColors} methods.
 *
 * @param sheet
 *            - {@link Sheet} on which to apply borders
 */
public void applyBorders(Sheet sheet) {
  Workbook wb = sheet.getWorkbook();
  for (Map.Entry<CellAddress, Map<String, Object>> entry : _propertyTemplate
      .entrySet()) {
    CellAddress cellAddress = entry.getKey();
    if (cellAddress.getRow() < wb.getSpreadsheetVersion().getMaxRows()
        && cellAddress.getColumn() < wb.getSpreadsheetVersion()
            .getMaxColumns()) {
      Map<String, Object> properties = entry.getValue();
      Row row = CellUtil.getRow(cellAddress.getRow(), sheet);
      Cell cell = CellUtil.getCell(row, cellAddress.getColumn());
      CellUtil.setCellStyleProperties(cell, properties);
    }
  }
}

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

protected void adjustCellReferencesInsideFormula(Cell cell, Sheet destSheet, int deltaX, int deltaY){
    XSSFWorkbook hostWorkbook = (XSSFWorkbook) destSheet.getWorkbook();
    XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(hostWorkbook); 
    Ptg[] ptgs = FormulaParser.parse(cell.getCellFormula(), fpb, FormulaType.CELL, 0);
    int destSheetIndex = hostWorkbook.getSheetIndex(destSheet);
    if(adjustInBothDirections(ptgs, destSheetIndex, deltaX, deltaY))
      cell.setCellFormula(FormulaRenderer.toFormulaString(fpb, ptgs));
  }
}

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

/*package*/ static void updateHyperlinks(Sheet sheet, FormulaShifter formulaShifter) {
  int sheetIndex = sheet.getWorkbook().getSheetIndex(sheet);
  List<? extends Hyperlink> hyperlinkList = sheet.getHyperlinkList();
  for (Hyperlink hyperlink : hyperlinkList) {
    XSSFHyperlink xhyperlink = (XSSFHyperlink) hyperlink;
    String cellRef = xhyperlink.getCellRef();
    CellRangeAddress cra = CellRangeAddress.valueOf(cellRef);
    CellRangeAddress shiftedRange = BaseRowColShifter.shiftRange(formulaShifter, cra, sheetIndex);
    if (shiftedRange != null && shiftedRange != cra) {
      // shiftedRange should not be null. If shiftedRange is null, that means
      // that a hyperlink wasn't deleted at the beginning of shiftRows when
      // identifying rows that should be removed because they will be overwritten
      xhyperlink.setCellReference(shiftedRange.formatAsString());
    }
  }
}

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

/**
 * Updated named ranges
 */
/*package*/
static void updateNamedRanges(Sheet sheet, FormulaShifter formulaShifter) {
  Workbook wb = sheet.getWorkbook();
  XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create((XSSFWorkbook) wb);
  for (Name name : wb.getAllNames()) {
    String formula = name.getRefersToFormula();
    int sheetIndex = name.getSheetIndex();
    final int rowIndex = -1; //don't care, named ranges are not allowed to include structured references
    Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.NAMEDRANGE, sheetIndex, rowIndex);
    if (formulaShifter.adjustFormula(ptgs, sheetIndex)) {
      String shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs);
      name.setRefersToFormula(shiftedFmla);
    }
  }
}

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

/**
 * Shift a formula using the supplied FormulaShifter
 *
 * @param row            the row of the cell this formula belongs to. Used to get a reference to the parent workbook.
 * @param formula        the formula to shift
 * @param formulaShifter the FormulaShifter object that operates on the parsed formula tokens
 * @return the shifted formula if the formula was changed,
 * <code>null</code> if the formula wasn't modified
 */
/*package*/
static String shiftFormula(Row row, String formula, FormulaShifter formulaShifter) {
  Sheet sheet = row.getSheet();
  Workbook wb = sheet.getWorkbook();
  int sheetIndex = wb.getSheetIndex(sheet);
  final int rowIndex = row.getRowNum();
  XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create((XSSFWorkbook) wb);
  try {
    Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.CELL, sheetIndex, rowIndex);
    String shiftedFmla = null;
    if (formulaShifter.adjustFormula(ptgs, sheetIndex)) {
      shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs);
    }
    return shiftedFmla;
  } catch (FormulaParseException fpe) {
    // Log, but don't change, rather than breaking
    logger.log(POILogger.WARN, "Error shifting formula on row ", row.getRowNum(), fpe);
    return formula;
  }
}

相关文章

微信公众号

最新文章

更多