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

com.viiyue.plugins.excel.ExcelReader Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
/**
 * Copyright (C) 2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.viiyue.plugins.excel;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import com.viiyue.plugins.excel.converter.DefaultReadConverter;
import com.viiyue.plugins.excel.converter.ReadConverter;
import com.viiyue.plugins.excel.metadata.CellInfo;
import com.viiyue.plugins.excel.metadata.ExcelInfo;

/**
 * Excel poi reader
 * 
 * @author tangxbai
 * @email [email protected]
 * @since 2021/05/28
 */
public final class ExcelReader extends ExcelProvider, T> {

	private static final int activeSheetIndex = -1;
	private static final ReadConverter defaultReader = new DefaultReadConverter();

	private Map dataList;

	public static final  ExcelReader of( Class beanType ) {
		return new ExcelReader( beanType, null );
	}

	public static final ExcelReader> of( ExcelInfo> em ) {
		return new ExcelReader>( null, em );
	}

	private ExcelReader( Class beanType, ExcelInfo em ) {
		super( beanType, em );
	}

	public ExcelReader read( String filePath ) throws EncryptedDocumentException, IOException {
		return read( filePath, activeSheetIndex );
	}
	
	public ExcelReader read( String filePath, int sheetIndex ) throws EncryptedDocumentException, IOException {
		return read( new File( filePath ), sheetIndex );
	}
	
	public ExcelReader read( File file ) throws EncryptedDocumentException, IOException {
		return read( file, activeSheetIndex );
	}

	public ExcelReader read( File file, int sheedIndex ) throws EncryptedDocumentException, IOException {
		Objects.requireNonNull( file, "The target Excel file could not be null" );
		if ( !file.exists() ) {
			throw new IOException( "The target Excel file could not be found : \"" + file.getAbsolutePath() + "\"" );
		}
		if ( !file.canRead() ) {
			throw new IOException( "Failed to read the target Excel file : \"" + file.getAbsolutePath() + "\"" );
		}
		try ( Workbook wb = WorkbookFactory.create( file ) ) {
			parseWorkbook( wb, sheedIndex );
		}
		return this;
	}

	public ExcelReader read( InputStream is ) throws EncryptedDocumentException, IOException {
		return read( is, activeSheetIndex );
	}
	
	public ExcelReader read( InputStream is, int sheedIndex ) throws EncryptedDocumentException, IOException {
		Objects.requireNonNull( is, "The Excel input stream is null" );
		try ( Workbook wb = WorkbookFactory.create( is ) ) {
			parseWorkbook( wb, sheedIndex );
		}
		return this;
	}

	public boolean hasAny() {
		return dataList != null && dataList.size() > 0;
	}

	public ExcelReader forEach( java.util.function.BiConsumer consumer ) {
		if ( hasAny() ) {
			dataList.forEach( consumer );
		}
		return this;
	}

	public List getReadList() {
		return dataList == null ? Collections.emptyList() : new ArrayList( dataList.values() );
	}

	private void parseWorkbook( Workbook wb, int sheedIndex ) {
		if ( isBeanType ) {
			parseWorkbookWithBean( wb, sheedIndex );
		} else {
			parseWorkbookWithObject( wb, sheedIndex );
		}
	}

	private void parseWorkbookWithBean( Workbook wb, int sheedIndex ) {
		Objects.requireNonNull( meta, "Excel metadata cannot be null, please initialize first" );

		int activeIndex = sheedIndex == -1 ? sheedIndex : wb.getActiveSheetIndex();
		Sheet sheet = wb.getSheetAt( activeIndex );
		initHeader( sheet );

		for ( int i = meta.getStartIndex(), s = sheet.getPhysicalNumberOfRows(); i <= s; i ++ ) {
			Row row = sheet.getRow( i );
			if ( row != null ) {
				T instance = mp.newInstance( beanType );
				if ( instance != null ) {
					for ( CellInfo cellInfo : meta.getCells() ) {
						Cell cell = row.getCell( cellInfo.getIndex() );
						if ( cell != null ) {
							CellType cellType = cell.getCellType();
							Class fieldType = cellInfo.getField().getType();
							ReadConverter reader = ObjectUtils.defaultIfNull( cellInfo.getReader(), defaultReader );
							Object cellValue = reader.readIt( cellInfo, fieldType, cellType, cell );
							cellInfo.setFieldValue( instance, cellValue );
						}
					}
				}
				addDataElement( instance, i + 1 );
			}
		}
	}

	private void parseWorkbookWithObject( Workbook wb, int sheedIndex ) {
		Objects.requireNonNull( meta,
				"Excel metadata cannot be empty, please call \"#metadata(ExcelMetadata)\" to initialize" );

		int activeIndex = sheedIndex == -1 ? sheedIndex : wb.getActiveSheetIndex();
		Sheet sheet = wb.getSheetAt( activeIndex );
		initHeader( sheet );

		for ( int i = meta.getStartIndex(), s = sheet.getPhysicalNumberOfRows(); i <= s; i ++ ) {
			Row row = sheet.getRow( i );
			if ( row != null ) {
				Map element = new HashMap();
				for ( CellInfo cellInfo : meta.getCells() ) {
					Cell cell = row.getCell( cellInfo.getIndex() );
					if ( cell != null ) {
						ReadConverter reader = ObjectUtils.defaultIfNull( cellInfo.getReader(), defaultReader );
						Object cellValue = reader.readIt( cellInfo, null, cell.getCellType(), cell );
						element.put( cellInfo.getLabel(), cellValue );
					}
				}
				addDataElement( ( T ) element, i + 1 );
			}
		}
	}

	private void initHeader( Sheet sheet ) {
		int headerIndex = meta.getHeaderIndex();
		Row headerRow = sheet.getRow( headerIndex );
		for ( int i = headerIndex, cs = headerRow.getPhysicalNumberOfCells(); i < cs; i ++ ) {
			Cell cell = headerRow.getCell( i );
			CellType cellType = cell.getCellType();
			if ( cellType != CellType._NONE && cellType != CellType.BLANK ) {
				String label = cell.getStringCellValue();
				CellInfo cellInfo = meta.getByLabel( label );
				if ( cellInfo != null ) {
					cellInfo.index( i );
				}
			}
		}
	}

	private void addDataElement( T element, int index ) {
		if ( dataList == null ) {
			this.dataList = new HashMap();
		}
		if ( element != null ) {
			this.dataList.put( index, element );
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy