All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.featherfly.data.office.excel.ExcelDataSet Maven / Gradle / Ivy

There is a newer version: 0.1.8
Show newest version

package cn.featherfly.data.office.excel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import cn.featherfly.common.lang.Lang;
import cn.featherfly.data.core.DataSet;

/**
 * 

* Excel数据集 *

* * @param 数据记录 * @author 钟冀 */ public class ExcelDataSet implements DataSet { private List records = new ArrayList<>(); private int index = -1; private Sheet sheet; private ExcelDataMapper mapper; /** * @param sheet Sheet * @param evaluator FormulaEvaluator * @param mapper ExcelDataMapper */ public ExcelDataSet(Sheet sheet, FormulaEvaluator evaluator, ExcelDataMapper mapper) { // this.sheet = sheet; if (sheet == null) { throw new IllegalArgumentException("sheet 不能为空"); } if (evaluator == null) { throw new IllegalArgumentException("evaluator 不能为空"); } index = sheet.getWorkbook().getSheetIndex(sheet); this.sheet = sheet; this.mapper = mapper; init(sheet, evaluator, mapper); } private void init(Sheet sheet, FormulaEvaluator evaluator, ExcelDataMapper mapper) { int rowNum = sheet.getLastRowNum() + 1; for (int rIndex = 0; rIndex < rowNum; rIndex++) { Row row = sheet.getRow(rIndex); if (row != null) { records.add(mapper.mapRecord(row, rowNum)); } } } /** * {@inheritDoc} */ @Override public Collection getDataRecords() { return records; } /** * {@inheritDoc} */ @Override public R getDataRecord(int index) { return records.get(index); } /** * {@inheritDoc} */ @Override public int getDataRecordsNumber() { return records.size(); } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public > D addRecord(R record) { Row row = null; if (sheet.getLastRowNum() == -1) { row = sheet.createRow(sheet.getLastRowNum() + 1); } else { row = sheet.getRow(sheet.getLastRowNum()); if (row == null) { row = sheet.createRow(sheet.getLastRowNum()); } else { row = sheet.createRow(sheet.getLastRowNum() + 1); } } mapper.fillData(row, record, row.getRowNum()); records.add(record); return (D) this; } /** * {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public > D addRecord(R... records) { if (Lang.isNotEmpty(records)) { for (R record : records) { addRecord(record); } } return (D) this; } /** * {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public > D addRecords(Collection records) { if (Lang.isNotEmpty(records)) { for (R record : records) { addRecord(record); } } return (D) this; } /** * {@inheritDoc} */ @Override public int getIndex() { return index; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy