下面列出了org.apache.poi.ss.usermodel.Sheet#getWorkbook ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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());
}
/**
* 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);
}
}
}
/**
* 指定した範囲の名前を登録する。
* <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;
}
@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);
}
}
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);
}
@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);
}
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);
}
}
}
@XlsPostSave
public void onSave(final Sheet sheet) {
Workbook workbook = sheet.getWorkbook();
int index = workbook.getSheetIndex(sheet.getSheetName());
workbook.setActiveSheet(index);
workbook.setFirstVisibleTab(index);
}