Java Code Examples for org.apache.poi.ss.usermodel.Sheet#getWorkbook()

The following examples show how to use org.apache.poi.ss.usermodel.Sheet#getWorkbook() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractSheetWriter.java    From Octopus with MIT License 6 votes vote down vote up
@Override
public CellPosition write(Sheet sheet, Collection<T> data) {
    if (!canWrite(sheet, data)) {
        return CellUtils.POSITION_ZERO_ZERO;
    }

    Class dataType = data.iterator().next().getClass();
    if (config.getClassType() != dataType) {
        throw new IllegalArgumentException("class of config is " + config.getClassType().getName() + " but type of data is " + dataType.getName());
    }

    CellPosition end = headerWriter.drawHeader(sheet, startPoint, config.getFields());

    int row = end.getRow() + 1;
    int col = getStartColumn();
    WorkbookContext bookResource = new WorkbookContext(sheet.getWorkbook());

    for (T t : data) {
        for (Field field : config.getFields()) {
            col = draw(sheet, row, col, field, t, bookResource);
        }
        col = getStartColumn();
        row++;
    }
    return new DefaultCellPosition(row, end.getCol());
}
 
Example 2
Source File: PropertyTemplate.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example 3
Source File: POIUtils.java    From xlsmapper with Apache License 2.0 6 votes vote down vote up
/**
 * 指定した範囲の名前を登録する。
 * <p>POI-3.7以上が必要。
 * <p>指定した名前が既に存在する場合は、新しい範囲に書き換える。
 * @param sheet シート
 * @param name 名前
 * @param startPosition 設定するセルの開始位置
 * @param endPosition 設定するセルの終了位置
 * @return
 */
public static Name defineName(final Sheet sheet, final String name,
        final Point startPosition, final Point endPosition) {

    ArgUtils.notNull(sheet, "sheet");
    ArgUtils.notEmpty(name, "name");
    ArgUtils.notNull(startPosition, "startPosition");
    ArgUtils.notNull(endPosition, "endPosition");

    final Workbook workbook = sheet.getWorkbook();
    Name nameObj = workbook.getName(name);
    if(nameObj == null) {
        nameObj = workbook.createName();
        nameObj.setNameName(name);
    }

    final AreaReference areaRef = buildNameArea(sheet.getSheetName(), startPosition, endPosition,
            sheet.getWorkbook().getSpreadsheetVersion());
    nameObj.setRefersToFormula(areaRef.formatAsString());

    return nameObj;

}
 
Example 4
Source File: AnnoFormulaTest.java    From xlsmapper with Apache License 2.0 6 votes vote down vote up
@XlsPostSave
public void handlePostSave(final Sheet sheet) {

    if(!name.equals("平均")) {
        return;
    }

    final Workbook book = sheet.getWorkbook();

    for(Point address : positions.values()) {
        Cell cell = POIUtils.getCell(sheet, address);

        CellStyle style = book.createCellStyle();
        style.cloneStyleFrom(cell.getCellStyle());

        // 塗りつぶし
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        // 罫線の上部を変更
        style.setBorderTop(BorderStyle.DOUBLE);

        cell.setCellStyle(style);
    }

}
 
Example 5
Source File: ExcelReportBuilder.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
private void setCellRangeAddressBorder(CellRangeAddress rangle, Sheet sheet) {
	int border = 1;
	Workbook wb = sheet.getWorkbook();
	RegionUtil.setBorderBottom(border, rangle, sheet, wb);
	RegionUtil.setBorderLeft(border, rangle, sheet, wb);
	RegionUtil.setBorderRight(border, rangle, sheet, wb);
	RegionUtil.setBorderTop(border, rangle, sheet, wb);
}
 
Example 6
Source File: DefaultHeaderWriter.java    From Octopus with MIT License 4 votes vote down vote up
@Override
public CellPosition drawHeader(Sheet sheet, CellPosition startPoint, List<Field> fields) {

    SupportField supportField = new SupportField(fields);

    int row = startPoint.getRow() - 1;
    int col = startPoint.getCol();

    int lastRow = row + supportField.getHeight() - 1;
    int lastCol = col + supportField.getWidth() - 1;

    WorkbookContext bookResource = new WorkbookContext(sheet.getWorkbook());

    drawHeaderImpl(sheet, row, lastRow, col, lastCol, supportField, bookResource);

    return new DefaultCellPosition(lastRow, lastCol);
}
 
Example 7
Source File: ExcelUtils.java    From onetwo with Apache License 2.0 4 votes vote down vote up
public static void copyRow(Sheet worksheet, Row newRow, Row sourceRow) {
Workbook workbook = worksheet.getWorkbook();
      for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
          Cell oldCell = sourceRow.getCell(i);
          Cell newCell = newRow.createCell(i);

          if (oldCell == null) {
              newCell = null;
              continue;
          }

          CellStyle newCellStyle = workbook.createCellStyle();
          newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
          newCell.setCellStyle(newCellStyle);

          if (oldCell.getCellComment() != null) {
              newCell.setCellComment(oldCell.getCellComment());
          }

          if (oldCell.getHyperlink() != null) {
              newCell.setHyperlink(oldCell.getHyperlink());
          }

          newCell.setCellType(oldCell.getCellType());

          switch (oldCell.getCellType()) {
              case Cell.CELL_TYPE_BLANK:
                  newCell.setCellValue(oldCell.getStringCellValue());
                  break;
              case Cell.CELL_TYPE_BOOLEAN:
                  newCell.setCellValue(oldCell.getBooleanCellValue());
                  break;
              case Cell.CELL_TYPE_ERROR:
                  newCell.setCellErrorValue(oldCell.getErrorCellValue());
                  break;
              case Cell.CELL_TYPE_FORMULA:
                  newCell.setCellFormula(oldCell.getCellFormula());
                  break;
              case Cell.CELL_TYPE_NUMERIC:
                  newCell.setCellValue(oldCell.getNumericCellValue());
                  break;
              case Cell.CELL_TYPE_STRING:
                  newCell.setCellValue(oldCell.getRichStringCellValue());
                  break;
          }
      }

      for (int i = 0; i < worksheet.getNumMergedRegions(); i++) {
          CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i);
          if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {
              CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(),
                      (newRow.getRowNum() +
                              (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow()
                                      )),
                      cellRangeAddress.getFirstColumn(),
                      cellRangeAddress.getLastColumn());
              worksheet.addMergedRegion(newCellRangeAddress);
          }
      }
  }
 
Example 8
Source File: SheetForcusListener.java    From xlsmapper with Apache License 2.0 3 votes vote down vote up
@XlsPostSave
public void onSave(final Sheet sheet) {

    Workbook workbook = sheet.getWorkbook();

    int index = workbook.getSheetIndex(sheet.getSheetName());
    workbook.setActiveSheet(index);
    workbook.setFirstVisibleTab(index);

}